Unix Programming - Fork and child process

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2004 > Fork and child process





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Fork and child process
Doug Ly

2004-04-23, 4:33 pm

Hi,
I've learned that to wait for all the children, we put wait(0) in the main()
function.
But my program gives me weird result. It seems like main() exits when there
are children are still running.
Is there a way to check if a program has collected all of its children
before it can exit?

Thanks


Dragan Cvetkovic

2004-04-23, 6:34 pm

"Doug Ly" <dmly@sbcglobal.net> writes:

> Hi,
> I've learned that to wait for all the children, we put wait(0) in the main()
> function.
> But my program gives me weird result. It seems like main() exits when there
> are children are still running.
> Is there a way to check if a program has collected all of its children
> before it can exit?
>


According to wait(3c) man page:

The wait() function will suspend execution of the calling
thread until status information for one of its terminated
child processes is available, or until delivery of a signal
whose action is either to execute a signal-catching function
or to terminate the process. If more than one thread is
suspended in wait(), waitpid(3C), or waitid(2) awaiting ter-
mination of the same process, exactly one thread will return
the process status at the time of the target process termi-
nation. If status information is available prior to the call
to wait(), return will be immediate.

So, if you want to wait for all children, you have to loop (wait sets errno
to ECHILD if there are no existing unwaited-for child processes).

HTH, Dragan


--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com