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
| #include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void){
pid_t pid_fils;
int i;
do{
pid_fils = fork();
}while((pid_fils == -1) && (errno == EAGAIN));
for(i = 0; i < 2; i++){
if(pid_fils == - 1){
fprintf(stderr, "fork() impossible, errno=%d\n", errno);
return 1;
}
printf("Fils %d crée\n", i);
}
if(pid_fils == 0){ //Processus fils
fprintf(stdout, "Fils : PID=%ld, PPID=%ld\n", (long)getpid(), (long)getppid());
return 0;
}
else{
fprintf(stdout, "Pere : PID=%ld, PPID=%ld, PID fils=%ld\n", (long)getpid(), (long)getppid(), (long)pid_fils);
wait(NULL);
return 0;
}
} |
Partager