[LCP]wait system call

ianezz at sodalia.it ianezz at sodalia.it
Tue Oct 30 20:32:10 UTC 2001


Santosh.Kawade at eyield.com, pigiando tasti a caso sul citofono, ha scritto:

 >         while(wait((int*)0 != ret)
 >             printf("\n ** Parent dead \n");
 >                     break;

Well, it should be "*** A child terminated ***", since this is
the parent process waiting for its children to terminate.

The wait() system call waits for any of the children of the process to
terminate, but does your child ever terminate (i.e. calling exit() or
_exit() or returning from main())? It can't be said for sure from the
code you posted. `ps' should give you a hint on that.

The printf(), btw, is executed only when the child that terminated was
*NOT* the child you just forked(). It is executed only if your process
forked other children somewhere else in the code.

Also, there could be a problem with the loop:

while(wait(0) != pid_of_chilren) 
{
    printf("....");
} 

Here you are waiting for any child, until you find the one you were
interested in, and then plainly ignore others: if you are sure that
you'll have one and only one child, a simple wait outside a loop would
suffice. OTOH, if you plan to have several children, that loop may
leave some of them unwaited (thus making them zombies until the parent
terminates), since you stop waiting at the very first match.

In the latter case, there is the waitpid() system call, which waits
for a specific child to terminate, while leaving the others untouched
(thus leaving them available for other wait() or waitpid() calls).

-- 
UNIX diapers by Pannolini USPTO 2039887  http://www.uspto.gov
Matteo Ianeselli      ianezz AT sodalia.it  (+39) 0461 316452
Visita il LinuxTrent:            http://www.linuxtrent.it



More information about the linuxCprogramming mailing list