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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
pid_t pid_fils;
/*******************************/
/* */
/* Handler des Signaux */
/* */
/*******************************/
void handler_pere(int signo)
{
static int nbre_recu = 0;
switch (signo)
{
case SIGUSR1: nbre_recu++;
break;
default: printf("\nNombre d'exemplaires reçus ..: %d\n", nbre_recu);
exit(0);
break;
}
}
void handler_fils(int signo)
{
static int nbre_envoye = 0;
switch (signo)
{
case SIGUSR1: nbre_envoye++;
break;
default: printf("Nombre d'exemplaires envoyés : %d\n", nbre_envoye);
exit(0);
}
}
/********************************/
/* */
/* Procédure Principale */
/* */
/********************************/
int main(void)
{
sigset_t mask1;
struct sigaction action1;
printf("Début du processus Père\n");
sigemptyset(&mask1);
// sigaddset(&mask1, SIGUSR1);
sigprocmask(SIG_BLOCK, &mask1, NULL);
action1.sa_handler = handler_pere;
sigaction(SIGUSR1, &action1, NULL);
sigaction(SIGINT, &action1, NULL);
sigemptyset(&mask1);
sigprocmask(SIG_BLOCK, &mask1, NULL);
if ((pid_fils = fork()) == 0)
{
/*========================*/
/* Processus Fils */
/*========================*/
sigset_t mask2;
struct sigaction action2;
printf("Début du processus Fils\n");
sigemptyset(&mask2);
sigprocmask(SIG_BLOCK, &mask2, NULL);
action2.sa_handler = handler_fils;
sigaction(SIGUSR1, &action2, NULL);
sigaction(SIGINT, &action2, NULL);
for (int i=0; i<100000; i++)
{
kill(getppid(), SIGUSR1);
sigsuspend(&mask2);
}
printf("Fin du processus Fils\n");
exit(0);
}
for (int i=0; i<100000; i++)
{
sigsuspend(&mask1);
kill(pid_fils, SIGUSR1);
}
printf("Fin du processus Père\n");
exit(0);
} |