With glibc-2.1.92-5, getpid() does not return the pid of the process when called from within a thread. This problem exists in earlier versions of glibc as well. I've mentioned it here: http://sources.redhat.com/ml/libc-alpha/2000-10/msg00081.html Here is a test-case: #include "stdlib.h" #include "pthread.h" void print_message_function (void *ptr); int main (int argc, char **argv) { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int status; pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) message1); pthread_create (&thread2, NULL, (void *) &print_message_function, (void *) message2); pthread_join(thread1, (void **)&status); pthread_join(thread2, (void **)&status); exit (0); } void print_message_function(void *ptr) { int i; for(i=0; i < 20; i++) printf("%s: pid: %d\n", ptr, getpid()); }
That's how threads work in Linux since linuxthreads have been implemented. This can change only if Linus accepts kernel patches which will actually make sharing of pids between threads possible. Linus actually put a small part of the needed changes into 2.4.0-testX (forgot what X it was) but then backed it out. Ben La Haise (bcrl) is working on new set of patches, if Linus accepts them, then glibc will have much faster threads on those kernels and they will share pid etc. But until he does, there is nothing glibc can do about it.