Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <sched.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5.  
  6. sig_atomic_t value = 0;
  7. sig_atomic_t flag = 0;
  8.  
  9. void sigterm_handler(int sig)
  10. {
  11.     flag = 1;
  12. }
  13.  
  14. void sigusr1_handler(int sig)
  15. {
  16.     value++;
  17. }
  18.  
  19. void sigusr2_handler(int sig)
  20. {
  21.     value *= -1;
  22. }
  23.  
  24. int main()
  25. {
  26.     struct sigaction sigusr1_act = {
  27.         .sa_handler = sigusr1_handler,
  28.         .sa_flags = SA_RESTART,
  29.         .sa_mask = 0,
  30.     };
  31.     struct sigaction sigusr2_act = {
  32.         .sa_handler = sigusr2_handler,
  33.         .sa_flags = SA_RESTART,
  34.         .sa_mask = 0,
  35.     };
  36.     struct sigaction sigterm_act = {
  37.         .sa_handler = sigterm_handler,
  38.         .sa_flags = SA_RESTART,
  39.     };
  40.     sigaction(SIGUSR1, &sigusr1_act, NULL);
  41.     sigaction(SIGUSR2, &sigusr2_act, NULL);
  42.     sigaction(SIGTERM, &sigterm_act, NULL);
  43.     printf("%d\n", getpid());
  44.     fflush(stdout);
  45.     scanf("%d", &value);
  46.     for (;;) {
  47.         pause();
  48.         if (flag == 1) {
  49.             break;
  50.         }
  51.         printf("%d\n", value);
  52.         fflush(stdout);
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement