views:

229

answers:

1

One resource on Linux mentions pthread-create being implemented with clone system call as against other unix-based platform (which implement the same in some other ways).
This means that under linux two threads created from the same process using pthread_create will have different parent process ids.

$ ./a.out
new thread:  pid 6628 tid 1026 (0x402)
main thread: pid 6626 tid 1024 (0x400)

Question

  • Though clone system call creates a child process that can share a configurable amount of its parent's execution context (such as file descriptors and memory), it looks to me that among all implementation this is perhaps not the most efficient one. Under Linux, for every thread created from pthread_create is there a corresponding process (though it will shares resources with other processes) ? Is this interpretation correct ?
+2  A: 

It looks like you may be using the obsolete LinuxThreads implementation of pthreads, which returned a different pid for each thread. The current implementation is NPTL (Native POSIX Threads Library) which does not do that. It is still implemented using clone(), although clone() has been enhanced to allow a highly efficient POSIX-compliant threads implementation to be built on top of it, and NPTL makes extensive use of these enhancements.

You can determine which implementation you are using with the command getconf GNU_LIBPTHREAD_VERSION. See pthreads(7) for details and a list of differences.

mark4o