This is the mail archive of the
newlib@sources.redhat.com
mailing list for the newlib project.
General question about locks and fork
- From: Thomas Pfaff <tpfaff at gmx dot net>
- To: newlib at sources dot redhat dot com
- Date: Thu, 12 Feb 2004 21:48:12 +0100
- Subject: General question about locks and fork
I am trying to include thread safe stdio on cygwin and have a problem
with newlibs locks which are heavy used in stdio.
The problem is how this should work together with fork.
Normally the mutex state is preserved after a fork, a locked mutex is
still locked in the child process. When another thread than the forking
thread is in a file operation the lock never gets unlocked in the child
process.
The easy way would be to recreate the lock after a fork so that they are
unlocked be default. Then the forking thread will have no problems but:
Another thread can be in a file operation during the fork, which could
lead to inconsistent buffers and variables in the child process.
A different approach would be to lock all files before a fork and unlock
it afterwards in both the parent and the child process. This could be
done for example via pthread_atfork on those platforms that support this
function and would the cleanest solution IMHO.
What do you think ?
Thomas