csci4061/lab02-code/fork1.c
Michael Zhang 041f660ccd
f
2018-01-29 17:28:37 -06:00

17 lines
408 B
C

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
int i;
for (i = 0; i < 8; i++) {
pid_t child = fork();
if (child == 0) {
break;
} else { // make sure this is only executed on the parent process
waitpid(child, 0, 0); // wait for the child process to exit with status 0
}
}
printf("I am number %d, my pid is %d\n", i, getpid());
return 0;
}