-
The problem statement, all variables and given/known data:
I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the child�s process. -
Relevant commands, code, scripts, algorithms:
fork() and wait(), waitpid(). I�m using Mac OS X 10.6.8. -
The attempts at a solution (include all code and scripts):
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int status;
pid_t pid[5];
pid_t w;
int i, j;
pid[0] = fork();
for (i = 0;i < 6;i++) {
if (pid < 0) {
printf("Error at level: %d\n", i);
} else if (pid == 0) {
printf("child created: %d\n", i);
} else {
printf("parent created: %d\n", i);
if ((i + 1) < 6) {
pid[i + 1] = fork();
printf("fork level: %d\n", i);
w = waitpid(pid[i + 1], &status, 0);
printf("pid %d deleted \n", pid[i + 1]);
} else {
break;
}
}
//pid[i + 1] = fork();
}
for (j = 0;j < 6;j++) {
pid[j] = wait(&status);
printf("process: %d terminated!\n", j);
}
return 0;
}
- H�gskolan p� Gotland (Gotland University), Visby (Gotland), Sweden, Magnus Wessberg, and TPV714 (i was unable to post the url because i�ve not posted five times)