Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _POSIX_C_SOURCE 190607L
- #include <fenv.h>
- #include <limits.h>
- #include <setjmp.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- #include <stdbool.h>
- void sigusr1_signal(int signum, siginfo_t *info, void *context) {
- printf("1st scenario:\n");
- if (info->si_code == SI_QUEUE) {
- printf("Signal was sent using sigqueue");
- }
- printf("Attached value: %d\n\n", info->si_value.sival_int);
- }
- void sigchld_signal(int signum, siginfo_t *info, void *context) {
- printf("2nd scenario:\n");
- printf("Child process %d exited with return code %d\n\n", info->si_pid, info->si_status);
- }
- void sigalrm_signal(){
- printf("3rd scenarion:\n");
- if (info->si_code == SI_TIMER) {
- printf("Signal was sent by timer with id of %d\n\n", info->si_value.sival_int);
- } else if (info->si_code == SI_USER) {
- printf("SIGALRM was triggered by user\n");
- }
- }
- int main() {
- //1st
- struct sigaction sigusr1;
- sigusr1.sa_sigaction = sigusr1_signal;
- sigusr1.sa_flags = SA_SIGINFO;
- sigemptyset(&sigusr1.sa_mask);
- sigaction(SIGUSR1, &sigusr1, NULL);
- sigqueue(getpid(), SIGUSR1, (union sigval){.sival_int = 1998});
- //2nd
- struct sigaction sigchld;
- sigchld.sa_sigaction = sigchld_signal;
- sigchld.sa_flags = SA_SIGINFO;
- sigemptyset(&sigchld.sa_mask);
- sigaction(SIGCHLD, &sigchld, NULL);
- //3rd
- struct sigaction sigalrm;
- sigalrm.sa_sigaction = sigalrm_signal;
- sigalrm.sa_flags = SA_SIGINFO;
- sigemptyset(&sigalrm.sa_mask);
- sigaction(SIGALRM, &sigalrm, NULL);
- struct timespec second = {1, 0};
- struct timespec zero = {0, 0};
- const struct itimerspec timer_value = {zero, second};
- timer_t timer;
- timer_create(CLOCK_REALTIME, NULL, &timer);
- timer_settime(timer, 0, &timer_value, NULL);
- kill(getpid(), SIGALRM);
- while(true){}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement