Files
SCR/SCR3.1/TP3/Exo4/Exo4.c
2025-09-25 13:53:00 +02:00

43 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
// Création du processus échoué
perror("fork");
return 1;
} else if (pid == 0) {
// Processus fils
printf("Fils: %d\n", pid);
printf("Fils: Mon PID %d\n", getpid());
printf("Fils: Celui du père %d\n", getppid());
printf("Fils: je m'endors pour 4 secondes...\n");
sleep(4);
exit(2);
} else {
// Processus père
printf("Père: fork() %d (le PID du fils)\n", pid);
printf("Père: mon PID %d\n", getpid());
printf("Père: PID de mon père %d\n", getppid());
printf("Père: attends la fin de mon fils...\n");
wait(&status);
printf("Père: Code de retour : %d\n", WEXITSTATUS(status));
printf("Père: processus actifs...\n");
execl("/bin/ps", "ps", "-ef", NULL);
//Sera exécutée que si execl échoue
perror("execl");
return 1;
}
return 0;
}