Signals are queued for a process. They can be blocked and remain in pending state until they are unblocked by the process. The size of the queue is implementation dependent. Signal set is a mask for the signals. An example of a 32 bits signal set.
Signal set manipulation:-
/*
* initialize the set to unblock all signal
*/
int sigemptyset(sigset_t *set);
/*
* initialize the set to block all signal
*/
int sigfillset(sigset_t *set);
/*
* block a given signal in a set
*/
int sigaddset(sigset_t *set, int signum);
/*
* unblock a given signal in a set
*/
int sigdelset(sigset_t *set, int signum);
/*
* test if a signal is blocked in the set
*/
int sigismember(const sigset_t *set, int signum);
POSIX Signal Processing:-
The sigaction struct
struct sigaction {
void (*sa_handler)(int); // address of a signal handler
sigset_t sa_mask; /* block certain signal during the execution of the handler */
int sa_flags; // signal options
}
Signal handling functions
/*
install a signal handler for a signal, and return the previous sigaction struct.
*/
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
/*
modify the signal mask of the current process. how can be SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK
*/
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
/*
returns a set of signals which are blocked
*/
int sigpending(sigset_t *set);
/*
suspend the process until the specified signals (in the mask) occur
*/
int sigsuspend(const sigset_t *mask);
/*
install a signal handler for a signal, and return the previous sigaction struct.
*/
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
/*
modify the signal mask of the current process. how can be SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK
*/
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
/*
returns a set of signals which are blocked
*/
int sigpending(sigset_t *set);
/*
suspend the process until the specified signals (in the mask) occur
*/
int sigsuspend(const sigset_t *mask);
Signal handler will not change after a signal occurs. Signal mask is replaced by the sigaction() during signal handling and restored afterwards.
Posix Signal Example:-
static void signalHandler(int);
int main(int argc, char* argv[]) {
struct sigaction act;
struct sigaction oldact;
act.sa_handler = &signalHandler;
sigfillset(&act.sa_mask); /*block signal @ signal handler */
act.sa_flags = 0;
sigaction(SIGHUP, &act, &oldact);
while (receivedSignalNo_g != SIGTERM) {
if (receivedSignal_g) {
receivedSignal_g = 0;
receivedSignalNo_g = 0;
}
sleep(60);
}
}
static void signalHandler(int signalNumber) {
receivedSignal_g = 1;
receivedSignalNo_g = signalNumber;
sleep(60);
}
}
static void signalHandler(int signalNumber) {
receivedSignal_g = 1;
receivedSignalNo_g = signalNumber;
}
No comments:
Post a Comment