Am citit si eu ceva asemanator intr-o carte de C++, a fost nasoala cartea..
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
pid_t child_pid;
char *tasks[] = {"Task1", "Task2", "Task3", "Task4", "Task5"};
const int task_no = 5;
for (int i = 0; i < task_no; ++i) {
child_pid = fork();
if (child_pid == 0) { // Child
printf("Child: I got to do %s\n", tasks[i]);
_exit(i);
} else { // Parrent
printf("Parrent: Spawned child %d\n", i);
}
}
printf("\nI'm going to wait for my children now.\n");
printf("Hopefully they don't turn into zombies\n\n");
int status = -1;
for (int i = 0; i < task_no; ++i) {
wait(&status);
if (WIFEXITED(status)) {
printf("Parrent: My child is not a zombie, yay.\n");
printf("Parrent: Child %d finished, yay!\n", WEXITSTATUS(status));
}
}
return 0;
}