}
if (child) {
- printf("Forked child %d\n", child);
+ printf("%d: Forked child %d\n", getpid(), child);
child_count++;
return child;
}
+ /* reset child's child count */
+ child_count = 0;
execvp(cmd, argv);
error = errno;
printf("Failed to execv command %s: %s\n", cmd, strerror(error));
int harvest_zombies(int pid)
{
- int status;
+ int status, error;
if (child_count == 0)
return 0;
pid = waitpid(pid, &status, 0);
- if (status == 0)
- printf("pid %d: terminated with exit code %d\n", pid, status);
+ if (pid < 0) {
+ error = errno;
+ fprintf(stderr, "%d: Error on wait(): %s\n", getpid(),
+ strerror(error));
+ }
+ else
+ printf("pid %d: exit %d. %d still running\n", pid, status,
+ child_count--);
- child_count--;
return 1;
}
close(wfd[0]);
*readfd = rfd[0];
*writefd = wfd[1];
+ child_count++;
return pid;
}