LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get child process return status without blocking the parent (https://www.linuxquestions.org/questions/programming-9/get-child-process-return-status-without-blocking-the-parent-4175432304/)

Deimon 10-15-2012 10:05 AM

get child process return status without blocking the parent
 
Hello,

I need to get the return status from a child process in C.
The problem with wait/waitpid is that I can't afford to interrupt the parent process which is generally on a select loop.
I can try to install a signal handler for SIGCHLD, but I am concerned with concurrency issues with the data structure where I keep data related to the children processes.

Any suggestion is more than welcome.
Thank you.

JohnGraham 10-15-2012 12:39 PM

You can pass wait() the WNOHANG flag, and it will refuse to block if the child hasn't exited - you can tell this has happened by the return value (see man page for details).

sundialsvcs 10-15-2012 03:39 PM

You should simply ensure that only the parent process maintains the data-structure of child process status. Really, no child should assume that this data structure is reliable: only the parent really knows, and then only when it formally executes a rendezvous with that child-pid by means of wait() in the manner that JohnGraham previously described.

Frankly, I would suggest moving the select-loop to a child process, which subsequently sends the incoming requests to its parent by means of a thread-safe queue. The parent's role thus becomes 100% devoted to keeping up with its unruly children. ;) But now it only has to deal with waiting for one type of thing, which is a CPU-initiated not an I/O-initiated event. The child (or children) that are waiting on sockets don't have to wait for anything else. Over a great many years, I have found that this "do-nothing parent" strategy shakes-out a lot of otherwise thorny problems. (Consider, as a prime example, the role of the init process ("process #1") in any Unix/Linux system . . .)

Deimon 10-17-2012 04:50 AM

Thank you for your replies.

Digging into man pages I found signalfd which notifies a signal via a fd. This way I can receive the notification through select.

Cheers.


All times are GMT -5. The time now is 08:03 AM.