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
| # include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <time.h>
# include <signal.h>
# include <sys/mman.h>
static pid_t* pidt;
// handler signal père
void sig_handler(int signo)
{
if (signo == SIGUSR1) {
printf("SIG HANDLER received SIGUSR1 by process %d\n", getpid());
printf("-> Envoie du signal SIGUSR1 au fils %d\n", pidt[0]);
// le père envoie un signal au premier fils
kill(pidt[0], SIGUSR1);
}
}
// handler signal fils
void sig_handlerr(int signo)
{
if (signo == SIGUSR1) {
printf("SIG HANDLER2 received SIGUSR1 by process %d\n", getpid());
}
}
int main(int argc, char* argv[]) {
printf("pid du processus père %d\n", getpid());
pidt = mmap(NULL, sizeof(pid_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (fork()==0) {
printf("1 processus fils %d\n", getpid());
pidt[0]=getpid();
printf("pid enregistré= %d\n", pidt[0]);
signal(SIGUSR1, sig_handlerr);
exit(0);
}
wait(NULL);
signal(SIGUSR1, sig_handler);
if (fork()==0) {
printf("2 processus fils %d", getpid());
// il envoie un signal SIGUSR1 au processsu père
printf("-> envoie SIGUSR1 au père %d\n", getppid());
kill(getppid(), SIGUSR1); // envoie un signal SIGUSR1 au processus père
exit(0);
}
wait(NULL);
printf("\n\n------ Fin du programme -------");
return EXIT_SUCCESS;
} |
Partager