LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 0
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.
 
Old 10-15-2012, 12:39 PM   #2
JohnGraham
Member
 
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, 03:39 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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, 04:50 AM   #4
Deimon
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.

Cheers.
 
  


Reply

Tags
child, signal handler, wait



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) lettuce84@naver.com Linux - Newbie 3 07-24-2015 08:37 AM
Sending Signal from Child Process to Parent Process : Not getting desired output thelink123 Linux - General 4 10-26-2012 09:05 PM
[SOLVED] Squid Parent: child process XXXXX exited with status 1 Lexus45 Linux - Server 1 05-24-2010 06:01 AM
return value from child to parent process prabhat ranjan Programming 3 02-12-2008 06:40 PM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 PM.

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