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 29 30 31 32 33 34 35 36 37 38 39
| int main(){
int i, process, status, dead;
printf("processus principal %d\n", getpid());
for(i=0; i<3; i++){
process=fork();
if(i==0){//cas du processus zombi
if(process==0){
printf("%d fils %d de %d\n",i+1, getpid(), getppid());
printf("mort de %d\n", getpid());
exit(EXIT_SUCCESS);
}else{
printf("père %d de %d\n", getpid(), process);
dead=wait(&status);
printf("fils %d de %d est mort (%d)\n\n",dead, getpid(), status);
}
}else if(i==1){//processus zombi qui sera orphelin
if(process==0){
printf("%d fils %d de %d\n",i+1, getpid(), getppid());
printf("mort de %d\n", getpid());
exit(EXIT_SUCCESS);
}else{
printf("père %d de %d\n\n", getpid(), process);
}
}else{//cas du processus orphelin puis zombi
if(process==0){
printf("%d fils %d de %d\n",i+1, getpid(), getppid());
sleep(10);
printf("fils %d de %d\n", getpid(), getppid());
printf("mort de %d\n", getpid());
exit(EXIT_SUCCESS);
}else{
printf("père %d de %d\n\n", getpid(), process);
}
}
sleep(2);
}
exit(EXIT_SUCCESS);
return 0;
} |
Partager