50 lines
1.4 KiB
C
50 lines
1.4 KiB
C
#include <stdio.h> /* entrées sorties */
|
|
#include <unistd.h> /* pimitives de base : fork, ...*/
|
|
#include <stdlib.h> /* exit */
|
|
#include <sys/wait.h> // pour le wait
|
|
#include <signal.h> /* traitement des signaux */
|
|
|
|
/* Traitant du signal */
|
|
void handler_sigint(int signal_num) {
|
|
printf("\nProcessus de pid %d : J'ai reçu le signal %d\n", getpid(),signal_num);
|
|
return;
|
|
}
|
|
|
|
int main() {
|
|
int retour, codeTerm;
|
|
|
|
/* associer un traitant au signal USR2 */
|
|
signal(12, handler_sigint);
|
|
|
|
printf("\nPère, pid=%d.\n", getpid());
|
|
|
|
retour = fork();
|
|
|
|
/* Bonne pratique : tester systématiquement le retour des appels système */
|
|
if (retour < 0) { /* échec du fork */
|
|
printf("Erreur fork\n");
|
|
/* Convention : s'arrêter avec une valeur > 0 en cas d'erreur */
|
|
exit(1);
|
|
}
|
|
|
|
if (retour == 0) { // fils
|
|
printf("\nFils, pid=%d, ppid=%d.\n", getpid(), getppid());
|
|
execl("/bin/sleep", "/bin/sleep", "10", NULL);
|
|
exit(1); // si exec fail
|
|
}
|
|
else { // père
|
|
printf("\nPère (%d), waiting mon fils (%d).\n", getpid(), retour);
|
|
|
|
retour = wait(&codeTerm); // on attend la fin du fils
|
|
if (retour == -1){
|
|
perror("wait ");
|
|
exit(3);
|
|
}
|
|
|
|
printf("%d, %d, %d, %d, %d\n", WIFEXITED(codeTerm), WEXITSTATUS(codeTerm), WIFSIGNALED(codeTerm), WTERMSIG(codeTerm), codeTerm);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|