Solved

How to know if a child process is terminated ?

Posted on 2001-09-13
12
341 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:zuppy
  • 4
  • 2
  • 2
  • +4
12 Comments
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6481102
zuppy,
how are you creating the child processes?
0
 

Author Comment

by:zuppy
ID: 6481106
with a fork().

Zuppy
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6481158
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.
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6481161
Sorry bout the messy post, used my Win machine to post it, :->
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6481163
Nevermind.  Just my browser acting funny.   sorry.. :->
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6484755
the child generates a SIGCHLD signal and sends it to its parent.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Expert Comment

by:graham_k
ID: 6486962
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.
0
 

Expert Comment

by:_serega
ID: 6493972
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 */
}
0
 

Expert Comment

by:_serega
ID: 6494008
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6495738
> 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 ;-)
0
 
LVL 2

Accepted Solution

by:
kgreddy earned 200 total points
ID: 6658468
Zuppy,
try using waitpid() call. This will work.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6918374
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.
http://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.  http://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.
http://www.experts-exchange.com/questions/Q.20124924.html
http://www.experts-exchange.com/questions/Q.20182613.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://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 http://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
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now