/* somme_fils.
c
* Démonstration fork() + wait() :
* – le fils calcule Σ(1..10)
* – le père attend, puis affiche un message de clôture
*/
#include <stdio.h> // printf
#include <unistd.h> // fork, getpid
#include <sys/wait.h> // wait / waitpid
#include <stdlib.h> // exit
int main(void)
{
pid_t pid = fork();
if (pid < 0) { // Erreur de fork
perror("fork");
return 1;
}
if (pid == 0) { // ----- Processus enfant -----
int somme = 0;
for (int i = 1; i <= 10; ++i)
somme += i;
printf("[Fils] Somme de 1 à 10 = %d (PID = %d)\n",
somme, getpid());
exit(0); // Terminaison propre
}
else { // ----- Processus parent -----
wait(NULL); // Attend n’importe quel fils
printf("[Parent] Fin du parent (PID = %d)\n", getpid());
}
return 0;
}