SCR/SCR3.1/TP3/fils.c

38 lines
856 B
C
Raw Normal View History

2023-09-22 16:16:04 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void){
pid_t p;
int status;
p = fork();
switch ((int) p) {
case 0 :
printf("Retour de fork : %d, je suis le fils !\n", (int) p);
printf("Mon PID est : %d\n", (int) getpid());
printf("Mon PPID est : %d\n", (int) getppid());
sleep(4);
exit(2);
break;
case -1:
printf("Ca s'est mal passé, aucun fils n'aura été engendré");
exit(2);
break;
default :
printf("Retour de fork : %d, je suis le père\n" , (int) p);
printf("Mon PID est : %d\n", (int) getpid());
printf("Mon PPID est : %d\n", (int) getppid());
wait(&status);
printf("Voici le code a renvoyé %d en code de sortie\n", WEXITSTATUS(status));
execl("/usr/bin/ps", "ps", "-ef", NULL);
exit(2);
}
return EXIT_SUCCESS;
}