Link to home
Start Free TrialLog in
Avatar of zuppy
zuppyFlag for Italy

asked on

How to know if a child process is terminated ?

Hi, I?ve a problem  in that I?ve not found a solution takin many ways.
If I fork a process into a child one how can I know in a not blocking way if the child has terminated?

For better explanation:

for (;;)
{
if (process_finished(pid)){do action}
else {continue to do other things}
}

I've tryed with a kill(0,pid) but that doesn't solve the problem since if the child is a zombie the return code will not help telling that the process is present.
My need is to make N child processes and create new ones only when old ones are finished in order not to load the box.
I can't believe that there is not a simple function to look at this.

Any ideas? (I?m blocked with my project for this)

Zuppy
Avatar of BlackDiamond
BlackDiamond

zuppy,
how are you creating the child processes?
Avatar of zuppy

ASKER

with a fork().

Zuppy
In your model, you could do this:

-----
int status;
for (;;)
{
if (waitpid(-1, &status, WNOHANG) >= 0){create another child}
else {continue to do other things}
}
------


You could also consider using pthreads instead of forking.  They are much more efficient (10-15 times faster to create) since you don't make a copy of your codepages every time you create a new thread.  You also get access to the same stack area, for the same reason.  Just need to make sure your code is thread safe (use a pthread_mutex_lock) in any of the shared functions.  It is much easier to keep track of thread groups than forked processes.
Sorry bout the messy post, used my Win machine to post it, :->
Nevermind.  Just my browser acting funny.   sorry.. :->
the child generates a SIGCHLD signal and sends it to its parent.
personally, I'd always have the child process signal that it is ending, rather than have the parent continually checking.   You shouldn't be worried about hanging processes (your code won't hang, right?), but if you are, the parent can always send a message tothe child asking its status & if no reply is received, then kill it.
maybe it'll help


/* install signal handler */
  struct sigaction nact, oact;
 ...
  (void) sigemptyset( &nact.sa_mask );
  nact.sa_flags = SA_SIGINFO;

  nact.sa_sigaction = chldhandler;
  (void) sigaction( SIGCHLD, &nact, &oact );
 ...



/* the code of the signal handler */
void
chldhandler( int sig, siginfo_t *info, void *context)
{
  int status;
  pid_t pid;

  info    = NULL; /* unused */
  context = NULL; /* unused */
  sig     = 0;    /* unused */

  pid = waitpid( (pid_t)-1, &status, WNOHANG );

  if (WIFEXITED(status) )
 /* do smth when child is dead */
  else if (WIFSIGNALED(status) )
. /* child receives a signal */
  else if (WIFSTOPPED(status) )
 /* child receives SIGSTP */
}
child *never* generates a SIGCHLD.
SIGCHLD is generated by the kernel on exit(2) system call.
and SIGCHLD is sent to a parent. wait(2) system call releases the process's audit info and deletes it from process table.
if the actual parent is dead but the child is still alive, kernel changes the parent of a child to init(1), which always has pid 1.
please note that kernel generates SIGCHLD on *child status change*, not on just child's death.
> please note that kernel generates SIGCHLD on *child status change*, not on just child's death.

I was not precice enough, but I think everyone understood what I meant, repectively will happen.
Thanks for being as pedantic as I'm, usually ;-)
ASKER CERTIFIED SOLUTION
Avatar of kgreddy
kgreddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20124924.html
https://www.experts-exchange.com/questions/Q.20182613.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20044946.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange