How to make C++ code fork-safe?



Hi all.

I've been programming in C++ for quite some time, but I am new in the
unix world. One thing that keeps surprizing me is fork()...

The software I am working on right now seems to use fork() quite
extensively. Besides the fact that it runs under Apache (which forks,
I think, in rather controlled manner), forks are used left and right
in the module itself. It seems that, when something needs to be done
asynchronously, a fork() is issued, the work is done in the child
process, and then the child is terminated.

First issue I ran into was a thread created in the singleton
constructor not replicated in the forked process. This was quite easy
to workaround once I figured what's happenning, but then it became
worse... The condition variable inited in parent process hanged in
pthread_cond_destroy() at the child process termination. Then we
found pthread_atexit...

Now I think I ran into another related issue. I will not describe it
here, but rather try to provide some generic description:

Supposedly you have a global object (singleton, etc.). To make it
thread-safe you lock some of its operations. In the middle of such
operation the object may be in an inconsistent state. Let's say a
fork is issued from another thread during such an operation (most
likely the thread calling fork() does not lock your global object).
It copies the object into the child process while it is in the middle
of something... Later on it crashes in the destructor when the child
is terminated.

Even better, recall the problem with the double-check pattern
(described in http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf).
Looks like this problem directly applies to fork() if it is issued
from another thread after the pointer is assigned but before the
object is initialized.

Now I am wondering if there exist any rules that can be applied to
make your C++ code fork-safe? Or does the usage of fork() itself
needs to be limited?

Thanks for any advice.

Arkadiy

.



Relevant Pages

  • Re: How to make C++ code fork-safe?
    ... The software I am working on right now seems to use fork() quite ... and then the child is terminated. ... traditional UNIX(*) environment. ... likely the thread calling forkdoes not lock your global object). ...
    (comp.unix.programmer)
  • Re: Problem in Child process
    ... I am using fork and in the child ... > which will invoke an executable. ... have it try to get a lock on the file. ...
    (comp.unix.programmer)
  • Re: timing a fork
    ... if ($kid) { ... What it does is say that the parent isn't interested in ... Once a parent process has forked it can carry on on its own while the child ... In fact what a call to 'system' does is to fork a child ...
    (perl.beginners)
  • Re: Ruby lacks atfork : The evil that lives in fork...
    ... so that there's no way to ensure mutex to share ... Give the resource to the child. ... fork() in a multi-threaded program. ... multi-threaded I/O libraries, which are almost sure to be invoked ...
    (comp.lang.ruby)
  • Re: EPERM from pthread_mutex_unlock after fork using pthread_atfork()
    ... So, if the fork() creates a copy of the parent, the child should get a copy of the mutex as well. ... it should be locked in the child process and the parent process once the forkprocessing is complete. ...
    (comp.programming.threads)