UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » Mapping of User level threads and Kernel thread

#1 2008-02-15 09:12 AM

vivek526
Member
Registered: 2006-09-25
Posts: 20
Website

Re: Mapping of User level threads and Kernel thread

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

#2 2008-02-15 09:16 AM

vivek526
Member
Registered: 2006-09-25
Posts: 20
Website

Re: Mapping of User level threads and Kernel thread

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

#3 2008-02-15 01:56 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,761
Website

Re: Mapping of User level threads and Kernel thread

Threads on Linux are one-to-one...  Each thread is its own separate kernel process,
essentially...  Just sharing lots of resources with each other...  Here's a little bit about
the current Linux thread system:

http://en.wikipedia.org/wiki/Native_POS … ad_Library

Offline

#4 2008-02-15 02:04 PM

vivek526
Member
Registered: 2006-09-25
Posts: 20
Website

Re: Mapping of User level threads and Kernel thread

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

#5 2008-02-15 03:06 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,761
Website

Re: Mapping of User level threads and Kernel thread

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

#6 2008-02-15 09:03 PM

vivek526
Member
Registered: 2006-09-25
Posts: 20
Website

Re: Mapping of User level threads and Kernel thread

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

#7 2008-02-15 11:17 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,201

Re: Mapping of User level threads and Kernel thread

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

#8 2009-01-27 10:00 AM

web design
Guest

Re: Mapping of User level threads and Kernel thread

i3839;23814 wrote:

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.

Why its not encouraged and how it portable and batter to use posix thread api...?

  • Index
  • » Threads
  • » Mapping of User level threads and Kernel thread

Board footer

Powered by FluxBB