Advertisement
blazinghorizon

Untitled

Dec 3rd, 2020 (edited)
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. /**
  2.  * 2.c - программа, блокирующая получение сигнала SIGUSR1
  3.  *
  4.  * Copyright 2020 (c), Semenov Nikita <ndsemeno@petrsu.ru>
  5.  *
  6.  * This code is licensed under a MIT-style license.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <locale.h>
  17.  
  18. int status;
  19.  
  20. void handler(int signo);
  21.  
  22. int main()
  23. {
  24.     (void) setlocale(LC_ALL, "");
  25.  
  26.     sigset_t newmask;
  27.     sigset_t oldmask;
  28.     sigset_t waitmask;
  29.  
  30.     struct sigaction act;
  31.     act.sa_handler = handler;
  32.     sigemptyset(&act.sa_mask);
  33.     act.sa_flags = 0;
  34.  
  35.     if (sigaction(SIGUSR1, &act, NULL) < 0) {
  36.         perror("signal failure");
  37.         exit(EXIT_FAILURE);
  38.     }
  39.  
  40.     // инициализация пустого набора сигналов new
  41.     if (sigemptyset(&newmask) < 0) {
  42.         perror("sigemptyset failure");
  43.         exit(EXIT_FAILURE);
  44.     }
  45.  
  46.     // добавление SIGUSR1 в new
  47.     if (sigaddset(&newmask, SIGUSR1) < 0) {
  48.         perror("sigaddset failure");
  49.         exit(EXIT_FAILURE);
  50.     }
  51.  
  52.     if (raise(SIGUSR1) < 0) {
  53.         perror("signal failure");
  54.         exit(EXIT_FAILURE);
  55.     }
  56.  
  57.     // блокировка
  58.     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
  59.         printf("получение SIGUSR1 не было успешно заблокировано\n");
  60.     else printf("получение SIGUSR1 успешно заблокировано\n");
  61.  
  62.     if (raise(SIGUSR1) < 0) {
  63.         perror("signal failure");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.  
  67.     if (sigpending(&waitmask) < 0) {
  68.        perror("sigpending failure");
  69.        exit(EXIT_FAILURE);
  70.     }
  71.  
  72.     if (sigismember(&waitmask, SIGUSR1) > -1)
  73.         printf("сигнал SIGUSR1 ожидает обработки\n");
  74.  
  75.     // восстановление
  76.     if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0)
  77.         printf("получение SIGUSR1 не было успешно разблокировано\n");
  78.     else printf("получение SIGUSR1 было успешно разблокировано\n");
  79.  
  80.     return 0;
  81. }
  82.  
  83. void handler(int signo)
  84. {
  85.     if (signo == SIGUSR1) printf("получен сигнал SIGUSR1\n");
  86.     else printf("номер нераспознанного сигнала = %d\n", signo);
  87. }
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement