Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Handling multiple signals of the same type without deferring them

sm_srksm_srk Member Posts: 1
Hi,

I'm struck with a problem in signal handling part of my project (Linux). While executing a certain part of my code (critical section), i don't want the signal action handlers to be executed. Once, the critical section of code is completed i want the signal action handlers to be executed for all the signals the process received. For example, before entering the critical section code i had blocked SIGHUP, SIGTERM, SIGUSR1, SIGALRM, SIGINT, SIGABRT. So, during my critical section execution if 6 SIGHUP's are generated then after the completion i want my SIGHUP signal action handler to be called 6 times. To make sure that multiple SIGHUP's are not deferred, i had set the sa_flags to SA_NODEFER also, but still the signal action handler is getting triggered only once no matter the number of times i trigger SIGHUP with "kill -1 pid". I have attached the code snippet i had written for the same, please help me...


#include
#include
#include
#include
#include

int f = 1;

void sigHandlerHUP (int sigNum);
void sigHandlerTERM (int SigNum);

int main()
{
int sigNum, cnt = 0;

sigset_t curSet, oldSet, zeroSet;

struct sigaction sigActHUP, sigActTERM;

memset (&sigActHUP, '', sizeof (struct sigaction));
sigemptyset (&sigActHUP.sa_mask);
sigActHUP.sa_flags = SA_NODEFER;
sigActHUP.sa_handler = sigHandlerHUP;
sigaction (SIGHUP, &sigActHUP, NULL);

memset (&sigActTERM, '', sizeof (struct sigaction));
sigemptyset (&sigActTERM.sa_mask);
sigActTERM.sa_flags = 0;
sigActTERM.sa_handler = sigHandlerTERM;
sigaction (SIGTERM, &sigActTERM, NULL);

sigemptyset (&curSet);
sigemptyset (&oldSet);
sigemptyset (&zeroSet);

sigaddset (&curSet, SIGALRM);
sigaddset (&curSet, SIGTERM);
sigaddset (&curSet, SIGINT);
sigaddset (&curSet, SIGABRT);
sigaddset (&curSet, SIGHUP);
sigaddset (&curSet, SIGUSR1);

pthread_sigmask (SIG_BLOCK, &curSet, &oldSet);

printf ("
Here my critical section code gets started
");
sleep (36);
printf ("
Here my critical section code completes
");

sigsuspend (&zeroSet);
pthread_sigmask (SIG_SETMASK, &oldSet, NULL);

while (f);

return 0;
}

void sigHandlerHUP (int SigNum) {
static int hups = 0;
printf ("Recieved SIGHUP - %d
", ++hups);
}

void sigHandlerTERM (int SigNum) {
printf ("Recieved SIGTERM
");
f = 0;
}
Sign In or Register to comment.