This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
TP-TR/chap1/ex1.c
vandechat96 8e00fb77dd ex chap 1
2022-05-10 22:45:38 +02:00

55 lines
1.6 KiB
C

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define SIGRT0 (SIGRTMIN + 3)
#define SIGRT1 (SIGRTMIN + 2)
#define SIGRT2 (SIGRTMIN + 1)
#define SIGRT3 (SIGRTMIN + 0)
void handler(int signum, siginfo_t *info, void *unused) {
if (signum == SIGRT0) printf("Rcv sig RT0\n");
else if (signum == SIGRT1) printf("Rcv sig RT1\n");
else if (signum == SIGRT2) printf("Rcv sig RT2\n");
else if (signum == SIGRT3) printf("Rcv sig RT3\n");
else printf("Rcv sig Unknow\n");
}
int main(int argc, char *argv[]) {
struct sigaction descriptor;
descriptor.sa_flags = SA_SIGINFO;
descriptor.sa_sigaction = handler;
sigfillset(&(descriptor.sa_mask));
sigaction(SIGRT0, &descriptor, NULL);
sigdelset(&(descriptor.sa_mask), SIGRT0);
sigaction(SIGRT1, &descriptor, NULL);
sigdelset(&(descriptor.sa_mask), SIGRT1);
sigaction(SIGRT2, &descriptor, NULL);
sigdelset(&(descriptor.sa_mask), SIGRT2);
sigaction(SIGRT3, &descriptor, NULL);
pid_t childPid = fork();
if (childPid == 0) {
// child
struct timespec request = {2, 0}, remaining;
clock_nanosleep(CLOCK_MONOTONIC, 0,&request, &remaining);
} else {
// parent
union sigval envelope;
struct timespec request = {1, 0}, remaining;
clock_nanosleep(CLOCK_MONOTONIC, 0,&request, &remaining);
sigqueue(childPid, SIGRT0, envelope);
sigqueue(childPid, SIGRT1, envelope);
sigqueue(childPid, SIGRT2, envelope);
sigqueue(childPid, SIGRT3, envelope);
clock_nanosleep(CLOCK_MONOTONIC, 0,&request, &remaining);
}
exit(EXIT_SUCCESS);
}