Value of sigset_t after sigprocmask

301 Views Asked by At

From man sigprocmask:

"If oset is not null, it is set to the previous value of the signal mask."

My questions: Without running or debugging the program,

  1. After executing line 10, the value of the old signal mask is stored into y. Since there were no blocked signals before this line was executed, what is the value of y?

  2. Is there a way to determine the value of z after executing line 14?

1 void my_sig_handler(int sig){
2     write(1, "a", 1);
3 }
4 int main(){
5     signal(SIGINT, my_sig_handler);
6     sigset_t x, y, z;
7     sigemptyset(&x);
8     sigaddset(&x, SIGINT);
9
10    sigprocmask(SIG_BLOCK, &x, &y);
11    write(1,"1",1);
12    do_some_work();
13    write(1,"2",1);
14    sigprocmask(SIG_SETMASK, &y, &z);
15
16    exit(0);
17 }
1

There are 1 best solutions below

0
On

You can test membership in a signal set with sigismember. Historically POSIX omitted a way to identify the range of values that might be signal numbers that you need to test, but all historical systems have _NSIG or NSIG defined to tell you this, and future POSIX is adding it.

Note that you can't in general assume y is empty after line 10, since signal mask is inherited across fork/exec. Your program may have been started with some signals masked.