Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <sched.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9.  
  10. volatile sig_atomic_t cnt;
  11. volatile sig_atomic_t must_exit = 0;
  12.  
  13. void handle_sigterm(int signum)
  14. {
  15. must_exit = 1;
  16. }
  17.  
  18. void handle_sigusr1(int signum)
  19. {
  20. cnt += 1;
  21. printf("%d\n", cnt);
  22. fflush(stdout);
  23. }
  24.  
  25. void handle_sigusr2(int signum)
  26. {
  27. cnt *= -1;
  28. printf("%d\n", cnt);
  29. fflush(stdout);
  30. }
  31.  
  32. int main()
  33. {
  34. struct sigaction action_usr1;
  35. memset(&action_usr1, 0, sizeof(action_usr1));
  36. action_usr1.sa_handler = handle_sigusr1;
  37. action_usr1.sa_flags = SA_RESTART;
  38. sigaction(SIGUSR1, &action_usr1, NULL);
  39.  
  40. struct sigaction action_usr2;
  41. memset(&action_usr2, 0, sizeof(action_usr2));
  42. action_usr2.sa_handler = handle_sigusr2;
  43. action_usr2.sa_flags = SA_RESTART;
  44. sigaction(SIGUSR2, &action_usr2, NULL);
  45.  
  46. struct sigaction action_term;
  47. memset(&action_term, 0, sizeof(action_term));
  48. action_term.sa_handler = handle_sigterm;
  49. action_term.sa_flags = SA_RESTART;
  50. sigaction(SIGTERM, &action_term, NULL);
  51.  
  52. printf("%d\n", getpid());
  53. fflush(stdout);
  54.  
  55. scanf("%d", &cnt);
  56.  
  57. while (!must_exit) {
  58. pause();
  59. }
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement