Reputation: 707
Recently, I've read in the "Advanced Linux Programming" book (http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf, Chapter 4.5) that on GNU/Linux POSIX threads are implemented as processes and that there is some kind of "manager thread" which does some control work.
When I run the following example from this book:
#include <stdio.h>
#include <pthread.h>
void* thread_func(void *arg)
{
fprintf(stderr, "thread: %d\n", (int)getpid());
while(1);
return NULL;
}
int main()
{
fprintf(stderr, "main: %d\n", (int)getpid());
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
while(1);
return 0;
}
I've received the same PID for the main thread and child thread, while the book says that it can be different & that there is another PID, which corresponds to the so-called "manager thread." I've tried to find some information about this "manager thread", but it's proving difficult.
UPD. I have no doubts about behaivour of my program, but there is some confusion about behaivour, explained on the book - in particular, under which circumstances it can be true?
Upvotes: 5
Views: 1387
Reputation: 11514
i've received the same PID both for main thread and child thread
That is normal and expected behavior. These threads you created co-exist in the same process, so getpid()
returns ID for that process. If you want to distinguish threads, use pthread_self()
(POSIX-compatible, but not system-wide unique) or gettid()
(Linux specific).
Internally, all processes and threads in Linux are managed by universal object called task (and defined by task_struct
), and each task has it's own ID. However, first task is a handle process thus called task group leader. And getpid()
returns PID of that task group leader.
So in your case, thread_func()
thread prints PID of its leader, main()
task, and main()
thread print PID of itself.
I suggest you to dive into kernel internals. It provides cleaner view of such things - try for example Robert Love book "Linux Kernel Development". Some information on threads/processes/etc. may be found here: http://www.win.tue.nl/~aeb/linux/lk/lk-10.html
Upvotes: 3
Reputation: 6335
Just reading the relevant lines from book and the example shared by you, it is clear that it is related to specific implementation of POSIX threads on GNU/Linux
In GNU/Linux, threads are implemented as processes.
Hence, Whenever you call pthread_create
to create a
new thread, Linux creates a new process that runs that thread.
So in example code, when you do pthread_create(&thread, NULL, thread_func, NULL);
the implementation creates a new process to run this newly created thread. This process will have a different PID (which is what getpid()
call displays).
So, now you already have 2 process, one main process which is launched when you run the program and this new process created by system to support thread execution.
And the same implementation is also creating another process (which is internal to its implementation), which is termed as manager thread. And this is created when you invoke pthread_create
Upvotes: 1