1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| int tempo,x,a,status;
srand(time(NULL));
printf("Veuillez introduire un nombre entier entre 1 et 10:\n");
int n = 0;
while(n <1 || n >9)
{
scanf ("%d", &n);
}
printf("Nombre introduit: %d\n",n);
for (x=0 ; x<n; x++)
{
tempo = (rand()%3)+1;
printf("P: Creation du fils %d\n",x+1);
a = fork();
if (a<0) return 1;
if (a == 0)
{
printf("\t\tJe suis le fils %d de pid %d\n",x+1,getpid());
sleep(tempo);
exit(x);
}
}
for(x=0;x<n;x++)
{
wait(&status);
if(WIFEXITED(status)) printf("P: Le fils num %d de pid se termine\n",WEXITSTATUS(status)+1);
}
return 0; |