I came across a concept in Advanced Linux Programming. Here's a link: refer to 4.5 GNU/Linux Thread Implementation.
I'm clear with the concept what author say's, but I'm confused with the program he has explained for printing processID's for threads.
Here is the code
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void* thread_function (void* arg)
{
fprintf (stderr, "child thread pid is %d\n", (int) getpid ());
/* Spin forever. */
while (1);
return NULL;
}
int main ()
{
pthread_t thread;
fprintf (stderr, "main thread pid is %d\n", (int) getpid ());
pthread_create (&thread, NULL, &thread_function, NULL);
/* Spin forever. */
while (1);
return 0;
}
The output for the above code according to author is
% cc thread-pid.c -o thread-pid -lpthread
% ./thread-pid &
[1] 14608
main thread pid is 14608
child thread pid is 14610
The output I get when I compile is
[1] 3106
main thread pid is 3106
child thread pid is 3106
I understand that to create a thread, linux internally calls clone(most of the cases), same as fork system call does to create a process. The only difference is thread created in process share the same process address space, while process created by a parent process copies the parent process address space. So, what I think is printing process ID in threads result in the same processID. but, its not the same result in book.
Please tellme what is he talking about..? Is the answer wrong in the book/mine..?