bssolver/siglib.c

64 lines
1.7 KiB
C

/*
This is a personal academic project. Dear PVS-Studio, please check it.
PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#define _POSIX_C_SOURCE 200112L
#include "siglib.h"
#include <signal.h>
#include "error.h"
#include <unistd.h>
#include <stdio.h>
void addSignalHandler(void (*changeViewFlag)()) {
struct sigaction sigAct = {{0}};
sigAct.sa_handler = changeViewFlag;
sigAct.sa_flags = 0;
if (sigaction(SIGSTART, &sigAct, NULL) == -1)
printSystemError("sigaction() -- ERROR");
if (sigaction(SIGEND, &sigAct, NULL) == -1)
printSystemError("sigaction() -- ERROR");
}
void blockSignal() {
sigset_t blockedSet = {{0}};
if (sigemptyset(&blockedSet) < 0)
printSystemError("sigaction() -- ERROR");
if (sigaddset(&blockedSet, SIGSTART) < 0)
printSystemError("sigaddset() -- ERROR");
if (sigaddset(&blockedSet, SIGEND) < 0)
printSystemError("sigaddset() -- ERROR");
if (sigprocmask(SIG_SETMASK, &blockedSet, NULL) < 0)
printSystemError("sigprocmask() -- ERROR");
}
void waitForSignal() {
sigset_t set;
if (sigemptyset(&set) < 0)
printSystemError("sigemptyset() -- ERROR");
sigpending(&set);
if (sigismember(&set, SIGEND)) {
return;
}
if (sigaddset(&set, SIGEND) < 0)
printSystemError("sigaddset() -- ERROR");
int temp;
if (sigwait(&set, &temp) != 0)
printError("sigwait() -- ERROR");
}
void sendSignal(int pid, int sig) {
union sigval value = {0};
if (sigqueue(pid, sig, value) < 0)
printSystemError("sigqueue() -- ERROR");
}
void sendStartSignal(int pid) {
sendSignal(pid, SIGSTART);
}
void sendFinishSignal(int pid) {
sendSignal(pid, SIGEND);
}