instruction to wait all childs ?

instruction to wait all childs ?

Post by Emanuele B » Sun, 02 May 2004 18:12:36

Hi there,

I'm new into UNIX programming (I'm learning it into my Operating Systems
course). I wonder if there's a way to wait until all childs are terminated,
instead of waiting each one with the waitpid function. Thanks a lot.

Emanuele Blanco
PGP Key Available

instruction to wait all childs ?

Post by mdank » Sun, 02 May 2004 23:00:04

As waitxxx() family of functions can wait for only one child status
change at a time, there is no such function as wait_all. However, you
never need it. If you need to know all of your child processes died
and just after that your program continues, then whenever you fork(),
you simply remember pid. Then, at a specific moment, wait for all
these pid-s. If you simply want to be sure that none of your child
processes will become zombies. than register a function for SIGCHLD
which would look like this:

void cigchld_handler( int sig )
int ret;
if ( sig != SIGCHLD )
while ( ( ret = waitpid( -1, NULL, WNOHANG ) ) > 0 ) {
if ( ret < 0 ) {
perror( "waitpid" );


instruction to wait all childs ?

Post by David Schw » Mon, 03 May 2004 01:31:32

And what if some library function you called created a child process as
a helper and it won't terminate until after the main process terminates? You
would be waiting forever.


instruction to wait all childs ?

Post by Nils O. Se » Tue, 04 May 2004 05:40:24

Keep track of how many child processes you create and:
waitpid(..); //and check for errors ofcourse

instruction to wait all childs ?

Post by Marc Rochk » Tue, 04 May 2004 09:45:40


If you set SIGCHLD to be ignored (or set the SA_NOCLDWAIT flag), child
processes that terminate won't turn into zombies, and therefore can't be
waited for individually. However, the semantics of wait and waitpid are then
changed: If there are no zombies (unwaited-for children that terminated
before SIGCHLD was set to be ignored), then the calls block until all
children have terminated, which is what you asked for.

However, you're new to this stuff, as you say, and what I just described
involves playing with signals, which I consider fairly advanced, so perhaps
you don't want to go this way. It's simpler, perhaps, to do what some others
have suggested and just keep track of the proceses you've created so you can
wait for them all.

Marc Rochkind
"Advanced UNIX Programming" (published April 2004)