Subject: Re: fork(2) vs. pthread_create()
To: Matthias Scheler <>
From: Nathan J. Williams <>
List: tech-userlevel
Date: 06/08/2004 09:56:26 (Matthias Scheler) writes:

> I found out today that NetBSD's "libpthread" doesn't like it when an
> application uses pthread_create(), fork() and finally pthread_create()
> in the child process.

"don't do that".

   A process shall be created with a single thread. If a multi-threaded
   process calls fork(), the new process shall contain a replica of the
   calling thread and its entire address space, possibly including the
   states of mutexes and other resources. Consequently, to avoid errors,
   the child process may only execute async-signal-safe operations until
   such time as one of the exec functions is called.

Notably, none of the pthread_* functions are async-signal-safe.

In order for this to work, there would have to be a fork handler that
reset all of libpthread's state to initialization values, and the
child program would still suffer severe constraints on what program
mutexes or variables it could use (consider, for example, the state of
stdio mutexes at the moment of fork()).

        - Nathan