Linux: system() + SIGCHLD handling + multithreading

前端 未结 4 1871
北恋
北恋 2021-02-03 14:30

I have a multithreaded application that installs a handler for SIGCHLD that logs and reaps the child processes.
The problem I see starts when I\'m doing a call to syst

4条回答
  •  有刺的猬
    2021-02-03 15:13

    Yes, it's a known (or at least strongly intimated) problem.

    Blocking SIGCHLD while waiting for the child to terminate prevents the application from catching the signal and obtaining status from system()'s child process before system() can get the status itself. .... Note that if the application is catching SIGCHLD signals, it will receive such a signal before a successful system() call returns.

    (From the documentation for system(), emphasis added.)

    So, POSIXly you are out of luck, unless your implementation happens to queue SIGCHLD. If it does, you can of course keep a record of pids you forked, and then only reap the ones you were expecting.

    Linuxly, too, you are out of luck, as signalfd appears also to collapse multiple SIGCHLDs.

    UNIXly, however, you have lots of clever and too-clever techniques available to manage your own children and ignore those of third-party routines. I/O multiplexing of inherited pipes is one alternative to SIGCHLD catching, as is using a small, dedicated "spawn-helper" to do your forking and reaping in a separate process.

提交回复
热议问题