My program is supposed to limit the number of child processes to 3.
With the code below, waitpid stalls my parent process so I can’t create more child processes after the first one. If I don’t use waitpid then I don’t know when a child process quits to decrease the number of alive processes.
int numProcs = 0;
while(1==1) {
/*
* inserts code that waits for incoming input
*/
numProcs++;
pid = fork();
if (pid == 0) {
doStuff(); // may exit anytime, based on user input
} else {
if (numProcs > 3) {
wait(&status);
numProcs--;
} else {
waitpid(pid, &status, 0); // PROBLEM!
numProcs--;
}
}
}
I’ve been searching for this problem the whole day. Can somebody help?
At the risk of being obvious, you basically want to just drop the else clause. The logic you’re looking for is something like:
This actually lets you change the value of max_active without restarting, which is the only justification for the for loop around the wait() call.
The obvious complaint is that number_active in my version doesn’t actually tell you how many processes are active, which is true. It tells you how many processes you haven’t wait()’ed for, which means that you might keep some zombies (but the number is limited). If you’re constantly running at or close to the maximum number of tasks, this doesn’t matter, and unless your maximum is huge, it doesn’t matter anyway, since the only Design Requirement was that you don’t use more than the maximum number of tasks, and consequently you only have to know that the number active is not more than the maximum.
If this really bothers you and you want to clean the tasks up, you can put:
before the other for loop, which will reap the zombies before checking if you need to block. (I can’t remember if waitpid(-1, &status WNOHANG) returns an error if there are no processes at all, but in any event there’s no point continuing the loop on an error.)