Sigprocmask () blocks signals on UNIX

I wrote a small piece of code. This code first blocks {SIGSEGV}, and then adds SIGRTMIN to the same set. So, my final set of signals, {SIGSEGV, SIGRTMIN}. Thus, if I use SIG_UNBLOCK, as I understand it, I first need to unlock SIGRTMIN, and then again, if I call SIG_UNBLOCK, SIGSEGV should be unlocked.

That is, 1) {SIGSEGV, SIGRTMIN} 2) SIG_UNBLOCK = unlock SIGRTMIN, 3) Call SIG_UNBLOCK = unlock SIGSEGV again. I only transfer the process to SIGRTMIN, so my second unlock should stop the process using SIGRTMIN. But this is not so. Please help. NB: Please do not provide links to other questions on sigprocmask (), I saw them and they do not clarify my question.

enter code here
#include <signal.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
 sigset_t old_set,new_set;
 sigemptyset(&old_set);
 sigemptyset(&new_set);

 if(sigaddset(&old_set,SIGSEGV)==0)
 {
   printf("sigaddset successfully added for SIGSEGV\n");
 }
 sigprocmask(SIG_BLOCK,&old_set,NULL); // SIGSEGV signal is masked
 kill(0,SIGSEGV);


 //*****************************************************************

 if(sigaddset(&new_set,SIGRTMIN)==0)
 {
  printf("sigaddset successfully added for SIGRTMIN\n");
 }
  sigprocmask(SIG_BLOCK,&new_set,&old_set); // SIGRTMIN signal is masked
 kill(0,SIGSEGV);

 //****************** Unblock one signal at a time ******************

 sigprocmask(SIG_UNBLOCK,&new_set,&old_set); // SIGRTMIN signal is unmasked
 sigprocmask(SIG_UNBLOCK,&new_set,&old_set); // SIGSEGV signal is unmasked

}

Output:
 [root@dhcppc0 signals]# ./a.out
  sigaddset successfully added for SIGSEGV
  sigaddset successfully added for SIGRTMIN
  (Note:SIGSEGV is not received even after sigprocmask(SIG_UNBLOCK,&new_set,&old_set); a second time)
+5
source share
2 answers

Your premise is incorrect. The entire set is locked and unlocked in one call sigprocmask.

In addition, usually you should create a set containing all the signals that you want to block, then you will try to block them with sigprocmask(SIG_BLOCK, pointer_to_sigset);.

SIGSEGV. , . , , :

/* ... */
sigset_t signal_set; /* We don't need oldset in this program. You can add it,
                        but it best to use different sigsets for the second
                        and third argument of sigprocmask. */
sigemptyset(&signal_set);

sigaddset(&signal_set, SIGSEGV);
sigaddset(&signal_set, SIGRTMIN);

/* now signal_set == {SIGSEGV, SIGRTMIN} */

sigprocmask(SIG_BLOCK, &signal_set, NULL): /* As i said, we don't bother with the 
                                              oldset argument. */

kill(0,SIGSEGV);  
kill(0,SIGSEGV);  /* SIGSEGV is not a realtime signal, so we can send it twice, but
                     it will be recieved just once */

sigprocmask(SIG_UNBLOCK, &signal_set, NULL); /* Again, don't bother with oldset */

/* SIGSEGV will be received here */

, . : , oldset, oldset. SIG_BLOCK, SIG_UNBLOCK SIG_SETMASK sigprocmask.

+11

: , , old_set new_set. SIGSEGV old_set, , , , ( SIGSEGV). .

enter code here
#include <signal.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
 sigset_t old_set,new_set;
 sigemptyset(&old_set);
 sigemptyset(&new_set);

 if(sigaddset(&old_set,SIGSEGV)==0)
 {
  printf("sigaddset successfully added for SIGSEGV\n");
 }
 sigprocmask(SIG_BLOCK,&new_set,&old_set); // SIGSEGV signal is masked
 kill(0,SIGSEGV);


//*****************************************************************

if(sigaddset(&new_set,SIGRTMIN)==0)
{
  printf("sigaddset successfully added for SIGRTMIN\n");
}
if(sigprocmask(SIG_BLOCK,&new_set,&old_set)==-1) // SIGRTMIN signal is masked
{
  perror("sigprocmask");
}
kill(0,SIGSEGV);


//****************** Unblock all signals  ******************

if(sigprocmask(SIG_UNBLOCK,&new_set,&old_set)==-1) // SIGRTMIN signal is unmasked
 {
  perror("sigprocmask");
 }
}
0

All Articles