pthreads + fork()
If I start a pthread at the beginning of my program (a multi-process network server app that calls fork() each time a new client connection is received) will that thread start to run in the child process after calling fork(), or will the child process act as if that thread was not started?
I discovered the answer for myself, experimentally.
for those interested, the thread only executes in the parent process, unless it is started again in the child.
Correct - only the thread calling fork() is duplicated in the child.
I'm learning about these now myself. This is how I was going to respond, but I wasn't sure of myself.
Is this all correct?
It's my understanding that fork() starts processing the newly spawned process (the "child") at the point of the fork(). The child is running in it's own process (aka address space) and while the stack and other info is copied into the new process (address space), you do not have access to the pthread that was started prior to the fork() - it's not in the child address space.
Something like that. As I understand it, the thread that calls fork is the only thread that is copied to the new process.
The Rationale section of the pthread_atfork() documentation gives good insight on the kind things to be aware of when calling fork() in a multi-threaded application.
When a thread forks it changes back into a "regular" process, separate from the parent and any thread it contains.
Originally Posted by Elkvis