General question about locks and fork

Thomas Pfaff tpfaff@gmx.net
Wed Mar 3 16:12:00 GMT 2004


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



More information about the Newlib mailing list