Search code examples
cforkpopen

Getting popen and SIGCHLD handler to work in parallel


In our code base we have a part of software that allows to run an arbitrary amount of external programs and monitor their exit codes via the use of fork() and the installation of a SIGCHLD handler. In the unit test cases this piece of software works fine.

However the process that is running this fork "server" is also running a bunch of software modules in several threads. Unfortunately one some parts of this (older) software are using popen() which seems to need to use its own SIGCHLD handler. The result we see is that the program will fail on the call to pclose() with the errno ECHILD.

Is there any way to use a SIGCHLD handler and a call to popen/pclose in parallel?


Solution

  • After fork(), signal handlers are inherited. So perhaps you should reset them to their defaults between fork() and exec(), by using signal() with SIG_DFL in the first child process, before you invoke the legacy software.