>> condition should I call break in order to exit the loop. I would like
>> to know when the child exits so I can exit the parent process.
>> However, I can't call waitpid while I am processing data between the
>> client and the target process.
> You should get SIGCHLD when your child dies/exists/terminates, so setting a
> handler for this signal (man sigaction) should give you some clues (though
> knowing which of the children has just exited could be a different issue).
after the target process writes all its data, you call _exit(0); (or
something similar) in it, right? The target process goes zombie.
Normally, you don't really care which child exited. Passing -1 to waitpid()
will clean up any children.
In the parent you catch SIGCHLD like he stated, with the signal handler being
something like the following (note, I use signal() instead of sigaction()..I
haven't learned sigaction() yet)
void zombie_killer(int sig)
{
int status, child_val;
pid_t child_pid=-1;
pid_t get_pid;
/* the argument type. */
while ( (get_pid = waitpid(
-1, /* Wait for any child */
(void *) &status,
WNOHANG /* Don't block waiting */
)) > 0) {
if (get_pid < 0) break;
else child_pid = get_pid;
}
/* No child to clean up, or error */
if (child_pid <= 0) {
if (child_pid == -1) {
/* error */
printf("waitpid() error %d (%s)\n",errno,strerror(errno));
}
else if (child_pid == 0) {
/* no child */
printf("waitpid() no child to clean up\n");
}
/* Reset handler */
signal(SIGCHLD, zombie_killer);
return;
}
else {
printf("waitpid() returned pid %u\n",(unsigned int)child_pid);
}
/* Reset handler */
/* Doing this before the waitpid() */
/* can lead to an infinite loop */
signal(SIGCHLD, zombie_killer);
/* Negative child_val indicates some error */
/* Zero child_val indicates no data available */
/*
* We now have the info in 'status' and can manipulate it using
* the macros in wait.h.
*/
if (WIFEXITED(status)) /* did child exit normally? */
{
child_val = WEXITSTATUS(status); /* get child's exit status */
printf("Child exited normally with status %d\n",child_val);
}
Quote:}
-Cygnus
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.
Anthony J. Biacco Network Administrator/Engineer
"Dream as if you'll live forever, live as if you'll die today"
http://cygnus.ncohafmuta.com http://www.intergrafix.net
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.