Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #define _POSIX_C_SOURCE 190607L
  2.  
  3. #include <fenv.h>
  4. #include <limits.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <sys/wait.h>
  12. #include <string.h>
  13. #include <stdbool.h>
  14.  
  15. void sigusr1_signal(int signum, siginfo_t *info, void *context) {
  16. printf("1st scenario:\n");
  17. if (info->si_code == SI_QUEUE) {
  18. printf("Signal was sent using sigqueue");
  19. }
  20. printf("Attached value: %d\n\n", info->si_value.sival_int);
  21. }
  22.  
  23. void sigchld_signal(int signum, siginfo_t *info, void *context) {
  24. printf("2nd scenario:\n");
  25. printf("Child process %d exited with return code %d\n\n", info->si_pid, info->si_status);
  26. }
  27.  
  28. void sigalrm_signal(){
  29. printf("3rd scenarion:\n");
  30. if (info->si_code == SI_TIMER) {
  31. printf("Signal was sent by timer with id of %d\n\n", info->si_value.sival_int);
  32. } else if (info->si_code == SI_USER) {
  33. printf("SIGALRM was triggered by user\n");
  34. }
  35. }
  36.  
  37. int main() {
  38. //1st
  39. struct sigaction sigusr1;
  40. sigusr1.sa_sigaction = sigusr1_signal;
  41. sigusr1.sa_flags = SA_SIGINFO;
  42. sigemptyset(&sigusr1.sa_mask);
  43. sigaction(SIGUSR1, &sigusr1, NULL);
  44. sigqueue(getpid(), SIGUSR1, (union sigval){.sival_int = 1998});
  45.  
  46. //2nd
  47. struct sigaction sigchld;
  48. sigchld.sa_sigaction = sigchld_signal;
  49. sigchld.sa_flags = SA_SIGINFO;
  50. sigemptyset(&sigchld.sa_mask);
  51. sigaction(SIGCHLD, &sigchld, NULL);
  52.  
  53. //3rd
  54. struct sigaction sigalrm;
  55. sigalrm.sa_sigaction = sigalrm_signal;
  56. sigalrm.sa_flags = SA_SIGINFO;
  57. sigemptyset(&sigalrm.sa_mask);
  58. sigaction(SIGALRM, &sigalrm, NULL);
  59.  
  60. struct timespec second = {1, 0};
  61. struct timespec zero = {0, 0};
  62. const struct itimerspec timer_value = {zero, second};
  63.  
  64. timer_t timer;
  65. timer_create(CLOCK_REALTIME, NULL, &timer);
  66. timer_settime(timer, 0, &timer_value, NULL);
  67.  
  68. kill(getpid(), SIGALRM);
  69.  
  70. while(true){}
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement