dedicated signal handler thread, problem with sigwait

dedicated signal handler thread, problem with sigwait

Post by Roger Reynold » Sun, 03 May 1998 04:00:00



I am attempting to create a thread dedicated to "handling" signals by:
 1) blocking all async signals (INT, TERM, QUIT, HUP) in the "main" thread
 2) create a thread to do signal handling - this thread loops on sigwait for
      the previously blocked async signal set.
 3) create other threads to do the work of the application.

Now, this works just fine, except for one little problem.
Upon return from sigwait,  the signal handler thread may block waiting for
a mutex or for some other reason.  If another instance of the signal is
raised
during this time, the application takes the default behavior - exits.

Now, all of the threads I created have the signal blocked, since I blocked
it in main, and all threads inherit the signal mask.  But, there are a
couple
of other threads that are started by the system, in addition to the thread
that main is called on.  Is it possible that the signal is being delivered
to
one of these threads, and the default action is then running its course?

If so, is there anything I can do about it?
If there isn't then sigwait would seem to be of very little utility.
This is on a solaris 2.5.1 system, by the way.

Thanks

============================================================
Roger S. Reynolds

  Web: http://www.rogerware.com

 
 
 

dedicated signal handler thread, problem with sigwait

Post by John D. Hicki » Mon, 04 May 1998 04:00:00


You might be able to handle the async signals directly from the main
thread provided that it is event-driven:

- create a pipe
- use sigaction to install a handler which runs with
  all signals blocked and writes the signal number to the pipe
- add the read end of the pipe to the set of read fds watched
  by select/poll
- when data are available on your signal pipe read them as signals
  that have been raised and dispatch accordingly

This has the advantage that you have a signal handler at all times
(provided that you use sigacton correctly). It works in singly threaded
environments and lets you call any non-reentrant handling functions.

 
 
 

dedicated signal handler thread, problem with sigwait

Post by Roger Reynold » Mon, 04 May 1998 04:00:00


Quote:>Okay.....I'm assuming posix threads here.

Yes, pthreads, sorry.

Quote:

>Questions: ...

I found some code that I left laying around in one of my worker threads
by mistake that was manipulating the signal mask, and this is what was
causing the trouble I was describing.

Quote:>> But, there are a couple of other threads that are started by the system,
>> in addition to the thread that main is called on.

>Such as?  Who or what is creating these threads and whose mask are
>they inheriting?

That remains as my question, (though for purposes of this discussion they
are not implicated, and must have these async signals blocked)

Try this with dbx:

(dbx) stop in main
(dbx) r
Running: sig

(dbx) threads



(dbx) exit

So, I don't know what threads 2 and 3 are, but they are always there.

Thanks.

============================================================
Roger S. Reynolds

  Web: http://www.rogerware.com

 
 
 

dedicated signal handler thread, problem with sigwait

Post by William LeFebv » Tue, 05 May 1998 04:00:00


Okay.....I'm assuming posix threads here.

Also, I'd recommend asking the folks over at comp.programming.threads.



>I am attempting to create a thread dedicated to "handling" signals by:
> 1) blocking all async signals (INT, TERM, QUIT, HUP) in the "main" thread
> 2) create a thread to do signal handling - this thread loops on sigwait for
>      the previously blocked async signal set.
> 3) create other threads to do the work of the application.

Questions:  are you blocking all signals in the main program BEFORE
any threads are started?  This should be done with pthread_sigmask,
and one of the arguments should be a signal mask created with sigemptyset
and sigaddset calls:

        sigset_t signal_set;

        sigemptyset(&signal_set);
        sigaddset(&signalset, SIGINT);
        sigaddset(&signalset, SIGTERM);
        ...
        pthread_sigmask(SIG_BLOCK, &signal_set, NULL);

In the signal handling thread, do you KEEP the sigmask the same?
This is important:  even the signals that you are trying to process
should be kept blocked in the signal handling thread.  Now it's just
a matter of calling sigwait with the same mask:

        int sig_number;
        sigwait(&signal_set, &sig_number);

Quote:>Now, all of the threads I created have the signal blocked, since I blocked
>it in main, and all threads inherit the signal mask.  But, there are a
>couple
>of other threads that are started by the system, in addition to the thread
>that main is called on.

Such as?  Who or what is creating these threads and whose mask are
they inheriting?

Quote:>Is it possible that the signal is being delivered to
>one of these threads, and the default action is then running its course?

Yes it is.  But I don't really know who is creating those threads,
so its difficult for me to see how they could inherit any sigmask
other than the initial thread's.

--
                                William LeFebvre
                                Group sys Consulting

                                +1 770 813 3224