You are not logged in.
Pages: 1
I want to understand how the user level threads are mapped to kernel threads.
Say,
a) i create two user level threads using pthread library and in thread 1 i block on accept system call and in thread 2 i just call sleep
b) thread t1 is created and after 20seconds delay i create the second thread t2
By this time, the thread t1 is blocked on accept (or for that matter any blocking system call)
I always thought that all user level threads created using pthread library were mapped to a single kernel thread, if that is correct then thread1 is already blocked on a system call, and thread 2 should never be executed. Because since thread1 has blocked on a system call, the process should get blocked ..
What thread model is being used in Linux ? what is the mapping b/w userlevel threads and kernel threads ?
How is that the OS is able to schedule thread2 ?
Is there any documentation which i can refer to ?
Best Regards,
Vivek Purushotham
Offline
If i execute the code, i can see that thread t1 is spawned and the thread function gets invoked and is blocked on accept system call ...
after sometime, thread t2 gets invoked, completes the thread function and exits ...thread t1 is still blocked on accept....
If it was Many-to-one model, then thread t2 should never have been created i guess ...
The code is given below
void block()
{
int sock_fd;
int new_sock_fd;
socklen_t sock_len;
struct sockaddr_in address;
memset(&address,0,sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(12345);
address.sin_addr.s_addr = htonl(INADDR_ANY);
sock_fd = socket(AF_INET,SOCK_STREAM,0);
bind(sock_fd,(const struct sockaddr *)&address,sizeof(address));
listen(sock_fd,1);
printf("\n Blocking on accept");
fflush(stdout);
new_sock_fd = accept(sock_fd,NULL,&sock_len);
printf("\n unBlocking on accept");
}
void *thread_func1(void *ptr)
{
printf("\n Executing Thread 1 : Sleep start..\n");
//sleep(100);
block();
printf("\n Executing Thread 1: Sleep end...\n");
}
void *thread_func2(void *ptr)
{
printf("\n Executing Thread 2 : Sleep start..\n");
sleep(20);
printf("\n Executing Thread 2: Sleep end...\n");
}
main()
{
pthread_t t1;
pthread_t t2;
pthread_create(&t1,NULL,thread_func1,NULL);
sleep(25);
pthread_create(&t2,NULL,thread_func2,NULL);
pthread_join(t2,NULL);
pthread_join(t2,NULL);
}
Offline
Thank you for your reply.
In user level threads i can get the thread id in the executing thread, same way is there a way where i can get the thread id of the kernel thread which is being mapped to my user level thread ?
Offline
Huh? I'm not sure what you mean exactly... In the old style of LinuxThreads, each
thread had a separate PID, but in NPTL they all share a PID... If that's what you mean?
For thread ID, there's pthread_self()... I'm not sure what else you might mean...
Offline
Oh okay,
Just to confirm, correct me if i am wrong
If i have two threads with thread id t1 and t2. IN NPTL, t1 and t2 would have a one to one mapping in the kernel, say two threads k1 and k2 ...
As per your reply, k1 and k2 would have the same PID ....is that what you meant in your previous email ??
I was trying to see if i can check the same programatically to see that both t1 and t2 has a separate 1:1 mapping in the kernel
Thank you for all your responses ..it was indeed very useful ..
Offline
Simplified, in Linux threads are just processes that share their memory space,
among other things (and created with a clone call with certain flags). There's a
hidden systemcall gettid() to get the current thread's ID, but it's not encouraged
to use it. More portable and better to use the posix thread API.
Offline
Pages: 1