Using the system calls related to sigaction() and signal set manipulation, i.e. sigaddset(), sigfillset(), etc., how would I implement a signal handler to block only SIGUSR1 and SIGUSR2 and ignore other signals?
I tried this without using sigprocmask() by first creating a signal set to be passed to sigemptyset() then to sigaddset().
My code is shown below but it does not seem to work. SIGUSR1 and SIGUSR2 does not seem to get blocked even though it has been masked in sa_mask.
The code above might be wrong or I have a wrong understanding of the sa_mask.
Can someone enlighten me?
Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.
- janvier -
I tried this without using sigprocmask() by first creating a signal set to be passed to sigemptyset() then to sigaddset().
My code is shown below but it does not seem to work. SIGUSR1 and SIGUSR2 does not seem to get blocked even though it has been masked in sa_mask.
Code:
#include <stdio.h>
#include <signal.h>
static int count = 0;
static int loopcount = 0;
void
handler(int signal_no)
{
printf("Signal caught %d\n", signal_no);
++count;
sleep(10);
}
int
main(int argc, char** argv)
{
struct sigaction sa_old; /* old signal actions */
struct sigaction sa_new; /* new signal actions */
sigset_t signal_set; /**/
sa_new.sa_handler = handler;
sigemptyset(&signal_set);
/* block SIGUSR1 and SIGUSR2 */
sigaddset(&signal_set, SIGUSR1);
sigaddset(&signal_set, SIGUSR2);
sa_new.sa_mask = signal_set;
sa_new.sa_flags = 0;
/* just handle SIGINT */
sigaction(SIGINT, &sa_new, &sa_old);
printf("..start..\n");
while(count < 2)
{
printf("Waiting for SIGINT\n");
sleep(4); /* snooze */
}
sigaction(SIGINT, &sa_old, 0);
printf("..end..\n");
return 0;
}
The code above might be wrong or I have a wrong understanding of the sa_mask.
Can someone enlighten me?
Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.
- janvier -