Restricting children processes
- From: Kelvin Moss <km_jr_usenet@xxxxxxxxx>
- Date: Mon, 17 Dec 2007 09:56:45 -0800 (PST)
Hi all,
I want to fork a number of children processes but I also want to
restrict the number of forked children processes at any time to not
cross some value (say N). Parent should wait if the threshold is met.
I have written a sample code for FreeBSD 4.11 that seems to work fine.
Could you please point out any mistakes, or offer any suggestions
Thanks ..
P.S. - Ignore the error checking for now.
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int max_children = 50;
int num_children = 0;
void sig_handler(int sig)
{
num_children--;
}
int main()
{
int pid;
int status;
int i;
signal(SIGCHLD, sig_handler);
for (i = 0; i < 10; i++) {
if (num_children == max_children) {
wait(&status);
}
pid = fork();
if (pid < 0) {
exit(2);
} else if (pid == 0) {
sleep(10*i); /* Do any random work */
exit(0);
} else {
num_children++;
}
}
}
.
- Follow-Ups:
- Re: Restricting children processes
- From: Martin Vuille
- Re: Restricting children processes
- From: Eric Sosman
- Re: Restricting children processes
- From: Rainer Weikusat
- Re: Restricting children processes
- Prev by Date: Re: Emacs
- Next by Date: Re: Restricting children processes
- Previous by thread: Emacs
- Next by thread: Re: Restricting children processes
- Index(es):
Relevant Pages
|