Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is signal blocking?

0 views
Skip to first unread message

Charley

unread,
Oct 30, 2001, 7:47:44 PM10/30/01
to
Hi, everyone.
Thanks for your answers about below my question.

I think I misunderstand and don't know signal blocking and mask.

Please explain signal blocking and mask in detail.
examples are welcomed.

Thanks in advance!


Patrick TJ McPhee

unread,
Oct 30, 2001, 11:38:40 PM10/30/01
to
In article <QSHD7.1659$cI6.6...@news.bora.net>,
Charley <cha...@serome.co.kr> wrote:

% Please explain signal blocking and mask in detail.
% examples are welcomed.

Start with a single-threaded process. The signal mask is a set of
signals which are blocked. If a signal is sent to the process while it
is blocked, it's said to be pending. If you unblock a pending signal,
it will be delivered to the process.

sigwait waits for a signal to become pending. The signal must be
blocked because if it isn't, there's no way for it to become pending,
at least on some systems.

With multi-threaded processes, the situation is roughly the same,
but each thread has its own signal mask. However, when you create
a thread, its initial mask is inherited from its creator, so if you
set a signal mask at the start of your program, it will apply to
all threads. If you want to handle signals using sigwait, it
should be blocked in all threads.

If a signal is sent to a process, it can be handled by any thread
which doesn't have it blocked -- the system gets to choose which
one. If all threads have the signal blocked, it is made pending,
and it can be handled using sigwait. This is good because it means
you're not handling the signal in a signal handler, so you can
do things which aren't allowed in signal handlers.

/* signal handling thread -- this just loops on
* sigwait until it gets cancelled */
void * sighnd(void * arg)
{
sigset_t mysigs;
int sig;

sigemptyset(&mysigs);
sigaddset(&mysigs, SIGUSR1);
sigaddset(&mysigs, SIGUSR2);

forever:
sigwait(&mysigs, &sig);
printf("got signal %d\n", sig);
other_async_unsafe_operations_woohoo(sig);
goto forever;

return NULL;
}

main()
{
sigset_t mysigs;
pthread_t tid;

sigemptyset(&mysigs);
sigaddset(&mysigs, SIGUSR1);
sigaddset(&mysigs, SIGUSR2);

/* block siguser1 and siguser2 for the process, effectively */
pthread_sigmask(SIG_BLOCK, &mysigs, NULL);

/* start a signal handler thread in the back-ground */
pthread_create(&tid, NULL, sighnd, NULL);

/* do things, which might interact with the signal handler
* or whatever */
do_stuff();

/* all done -- make the signal handler go away */
pthread_cancel(tid);
pthread_join(tid, NULL);

return 0;
}
--

Patrick TJ McPhee
East York Canada
pt...@interlog.com

0 new messages