Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-15-2012, 11:05 AM   #1
LQ Newbie
Registered: Dec 2010
Posts: 7

Rep: Reputation: 0
get child process return status without blocking the parent


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.
Old 10-15-2012, 01:39 PM   #2
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
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).
Old 10-15-2012, 04:39 PM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
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 . . .)
Old 10-17-2012, 05:50 AM   #4
LQ Newbie
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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.



child, signal handler, wait

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
killng parent process without killing child process (Linux C programming) Linux - Newbie 3 07-24-2015 09:37 AM
Sending Signal from Child Process to Parent Process : Not getting desired output thelink123 Linux - General 4 10-26-2012 10:05 PM
[SOLVED] Squid Parent: child process XXXXX exited with status 1 Lexus45 Linux - Server 1 05-24-2010 07:01 AM
return value from child to parent process prabhat ranjan Programming 3 02-12-2008 07:40 PM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 06:48 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration