Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 2.c - программа, блокирующая получение сигнала SIGUSR1
- *
- * Copyright 2020 (c), Semenov Nikita <ndsemeno@petrsu.ru>
- *
- * This code is licensed under a MIT-style license.
- */
- #include <stdio.h>
- #include <errno.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <locale.h>
- int status;
- void handler(int signo);
- int main()
- {
- (void) setlocale(LC_ALL, "");
- sigset_t newmask;
- sigset_t oldmask;
- sigset_t waitmask;
- struct sigaction act;
- act.sa_handler = handler;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- if (sigaction(SIGUSR1, &act, NULL) < 0) {
- perror("signal failure");
- exit(EXIT_FAILURE);
- }
- // инициализация пустого набора сигналов new
- if (sigemptyset(&newmask) < 0) {
- perror("sigemptyset failure");
- exit(EXIT_FAILURE);
- }
- // добавление SIGUSR1 в new
- if (sigaddset(&newmask, SIGUSR1) < 0) {
- perror("sigaddset failure");
- exit(EXIT_FAILURE);
- }
- if (raise(SIGUSR1) < 0) {
- perror("signal failure");
- exit(EXIT_FAILURE);
- }
- // блокировка
- if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
- printf("получение SIGUSR1 не было успешно заблокировано\n");
- else printf("получение SIGUSR1 успешно заблокировано\n");
- if (raise(SIGUSR1) < 0) {
- perror("signal failure");
- exit(EXIT_FAILURE);
- }
- if (sigpending(&waitmask) < 0) {
- perror("sigpending failure");
- exit(EXIT_FAILURE);
- }
- if (sigismember(&waitmask, SIGUSR1) > -1)
- printf("сигнал SIGUSR1 ожидает обработки\n");
- // восстановление
- if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0)
- printf("получение SIGUSR1 не было успешно разблокировано\n");
- else printf("получение SIGUSR1 было успешно разблокировано\n");
- return 0;
- }
- void handler(int signo)
- {
- if (signo == SIGUSR1) printf("получен сигнал SIGUSR1\n");
- else printf("номер нераспознанного сигнала = %d\n", signo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement