#include
#include
#include
#include
void forkChildren (int nChildren) {
int i;
pid_t pid;
for (i = 1; i <= nChildren; i++) {
pid = fork();
if (pid == -1) {
/* error handling here, if needed */
return;
}
if (pid == 0) {
printf("I am a child: %d PID: %d\n",i, getpid());
// we could use sisgsuspend instead of pause, due
// to a race condition in pause where the signal
// could be delivered just before the call to pause
// sleep (5);
pause();
return;
}
}
}
int main (int argc, char *argv[]) {
if (argc < 2) {
forkChildren (2);
} else {
forkChildren (atoi (argv[1]));
}
return 0;
}
c - fork x number of processes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment