Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <time.h>
  6.  
  7. #define CLOCKID CLOCK_REALTIME
  8. #define SIG SIGRTMIN
  9.  
  10. #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
  11. } while (0)
  12.  
  13. static void
  14. print_siginfo(siginfo_t *si)
  15. {
  16. timer_t *tidp;
  17. int or;
  18.  
  19. tidp = si->si_value.sival_ptr;
  20.  
  21. printf(" sival_ptr = %p; ", si->si_value.sival_ptr);
  22. printf(" *sival_ptr = 0x%lx\n", (long) *tidp);
  23.  
  24. or = timer_getoverrun(*tidp);
  25. if (or == -1)
  26. errExit("timer_getoverrun");
  27. else
  28. printf(" overrun count = %d\n", or);
  29. }
  30.  
  31. static void
  32. handler(int sig, siginfo_t *si, void *uc)
  33. {
  34. /* Note: calling printf() from a signal handler is not safe
  35. (and should not be done in production programs), since
  36. printf() is not async-signal-safe; see signal-safety(7).
  37. Nevertheless, we use printf() here as a simple way of
  38. showing that the handler was called. */
  39.  
  40. printf("Caught signal %d\n", sig);
  41. print_siginfo(si);
  42. signal(sig, SIG_IGN);
  43. }
  44.  
  45. int
  46. main(int argc, char *argv[])
  47. {
  48. timer_t timerid;
  49. struct sigevent sev;
  50. struct itimerspec its;
  51. long long freq_nanosecs;
  52. sigset_t mask;
  53. struct sigaction sa;
  54.  
  55. if (argc != 3) {
  56. fprintf(stderr, "Usage: %s <sleep-secs> <freq-nanosecs>\n",
  57. argv[0]);
  58. exit(EXIT_FAILURE);
  59. }
  60.  
  61. /* Establish handler for timer signal */
  62.  
  63. printf("Establishing handler for signal %d\n", SIG);
  64. sa.sa_flags = SA_SIGINFO;
  65. sa.sa_sigaction = handler;
  66. sigemptyset(&sa.sa_mask);
  67. if (sigaction(SIG, &sa, NULL) == -1)
  68. errExit("sigaction");
  69.  
  70. /* Block timer signal temporarily */
  71.  
  72. printf("Blocking signal %d\n", SIG);
  73. sigemptyset(&mask);
  74. sigaddset(&mask, SIG);
  75. if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1)
  76. errExit("sigprocmask");
  77.  
  78. /* Create the timer */
  79.  
  80. sev.sigev_notify = SIGEV_SIGNAL;
  81. sev.sigev_signo = SIG;
  82. sev.sigev_value.sival_ptr = &timerid;
  83. if (timer_create(CLOCKID, &sev, &timerid) == -1)
  84. errExit("timer_create");
  85.  
  86. printf("timer ID is 0x%lx\n", (long) timerid);
  87.  
  88. /* Start the timer */
  89.  
  90. freq_nanosecs = atoll(argv[2]);
  91. its.it_value.tv_sec = freq_nanosecs / 1000000000;
  92. its.it_value.tv_nsec = freq_nanosecs % 1000000000;
  93. its.it_interval.tv_sec = its.it_value.tv_sec;
  94. its.it_interval.tv_nsec = its.it_value.tv_nsec;
  95.  
  96. if (timer_settime(timerid, 0, &its, NULL) == -1)
  97. errExit("timer_settime");
  98.  
  99. /* Sleep for a while; meanwhile, the timer may expire
  100. multiple times */
  101.  
  102. printf("Sleeping for %d seconds\n", atoi(argv[1]));
  103. sleep(atoi(argv[1]));
  104.  
  105. /* Unlock the timer signal, so that timer notification
  106. can be delivered */
  107.  
  108. printf("Unblocking signal %d\n", SIG);
  109. if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
  110. errExit("sigprocmask");
  111.  
  112. exit(EXIT_SUCCESS);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement