Unix Programming - Why does the signal mask not working here.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2006 > Why does the signal mask not working here.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Why does the signal mask not working here.
nin234@yahoo.com

2006-09-16, 1:43 pm

When I run the following program it prints out the following and exits
bash-2.03$ ./a.out
pthread_create success
Launching signal handler thread
User Signal 1

I expected the SigHandler thread to catch the signal and print
" Received signal nSigNo = 16"
My environment is Solaris 2.8

void *SigHandler (void *pSigarg)
{
std::cout << "Launching signal handler thread " << std::endl;
sigset_t set;
sigemptyset (&set);
pthread_sigmask (SIG_SETMASK, &set, 0);
sigfillset (&set);
int nSigNo;
siginfo_t info;
struct itimerspec oItv;
memset (&oItv, 0, sizeof (oItv));
oItv.it_interval.tv_sec = 2;
oItv.it_interval.tv_nsec = 2;
oItv.it_value.tv_sec = 2;
oItv.it_value.tv_nsec = 2;
struct sigevent evp;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGUSR1;
timer_t timerid;
timer_create (CLOCK_REALTIME, &evp, &timerid);
timer_settime (timerid, 0, &oItv, 0);
//setitimer (ITIMER_REAL, &oItv, 0);
//alarm (3);
do
{
nSigNo = sigwaitinfo (&set, &info);
std::cout << "Received signal nSigNo=" << nSigNo << " "<< "Log
level=" << info.si_value.sival_int << std::endl;
switch (nSigNo)
{
case SIGALRM:
std::cout << "Timer expired " << std::endl;
break;
}
} while (nSigNo != -1 || errno == EINTR);
std::cout << "Exiting the signal handling thread " << std::endl;
}

int main ()
{
pthread_t tid;
sigset_t set;
struct sigaction act;
memset (&act, 0, sizeof (act));
sigfillset (&set);
pthread_sigmask (SIG_SETMASK, &set, 0) ;
if (pthread_create (&tid, 0, SigHandler, 0))
perror ("pthread_create failed ");
else
std::cout << "pthread_create success " << std::endl;
sleep (5000);

}

David Schwartz

2006-09-16, 1:43 pm


nin234@yahoo.com wrote:

> I expected the SigHandler thread to catch the signal and print


Nope, you unblocked the signal, allowing it to be delivered to the
thread normally rather than being caught.

> void *SigHandler (void *pSigarg)
> {
> std::cout << "Launching signal handler thread " << std::endl;
> sigset_t set;
> sigemptyset (&set);
> pthread_sigmask (SIG_SETMASK, &set, 0);


Right here. You set the signal mask to zero, allowing SIGUSR1 to
interrupt the process. My man page says:

In normal usage, the caller blocks the signals in set via a
prior call
to sigprocmask() (so that the default disposition for these
signals
does not occur if they are delivered between successive calls to
sig-
waitinfo()or sigtimedwait()) and does not establish handlers for
these
signals.

DS

K-mart Cashier

2006-09-16, 1:43 pm


David Schwartz wrote:
> nin234@yahoo.com wrote:
>
>
> Nope, you unblocked the signal, allowing it to be delivered to the
> thread normally rather than being caught.
>
>
> Right here. You set the signal mask to zero, allowing SIGUSR1 to
> interrupt the process. My man page says:
>
> In normal usage, the caller blocks the signals in set via a
> prior call
> to sigprocmask() (so that the default disposition for these
> signals
> does not occur if they are delivered between successive calls to
> sig-
> waitinfo()or sigtimedwait()) and does not establish handlers for
> these
> signals.
>
> DS


Maybe this is inexperience, but I've really never seen anyone use C++
with Unix. Of course, I've never saw males in mini-skirts and nylons
until I moved to the San Francisco bay area. Anyhow, doesn't the
Original Poster also need a mutex to protect the quit flag?

Chad

David Schwartz

2006-09-16, 1:43 pm


K-mart Cashier wrote:

> Maybe this is inexperience, but I've really never seen anyone use C++
> with Unix.


I can't imagine why. Lots of significant real-world development takes
place using C++ and UNIX. In my /usr/bin/ on a typical Linux system, I
found 137 executables made from C++ source files.

> Of course, I've never saw males in mini-skirts and nylons
> until I moved to the San Francisco bay area. Anyhow, doesn't the
> Original Poster also need a mutex to protect the quit flag?


I didn't see a quit flag in his code.

DS

Nils O. Selåsdal

2006-09-16, 1:43 pm

K-mart Cashier wrote:
....

> Maybe this is inexperience, but I've really never seen anyone use C++
> with Unix.


Start at www.kde.org to find heaps of C++ software for unix.
It's ofcourse used outside KDE too.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com