379_demos/lab1/demos/wait.c
2024-09-24 13:34:24 -06:00

21 lines
456 B
C

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main(void) {
for (int i = 0; i < 5; i++) {
int f = fork();
if (f == 0) {
printf("Child %d with pid %d\n", i, getpid());
_exit(0);
}
}
while (wait(NULL) > 0); // This waits until all children are terminated
// while (waitpid(-1, NULL, 0) > 0); // Same effect
printf("parent\n");
return 0;
}