Unix Programming - Signal handling

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2006 > Signal handling





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 Signal handling
sudeep

2006-01-23, 2:55 am

I came across this piece of code:

sigset_t mask, oldmask;

....

/* Set up the mask of signals to temporarily block. */
sigemptyset (&mask);
sigaddset (&mask, SIGUSR1);

....

/* Wait for a signal to arrive. */
sigprocmask (SIG_BLOCK, &mask, &oldmask);
while (!usr_interrupt)
sigsuspend (&oldmask);
sigprocmask (SIG_UNBLOCK, &mask, NULL);

The signal handler sets "usr_interrupt" on the occurance of SIGUSR1. My
doubt is that if sigprocmask is used to block SIGUSR1 then how is the
signal handler ever going to catch the SIGUSR1 signal in order to set
"usr_interrupt" so as to cause the while loop to transfer control to
the next statement?

I have seen a similar implementation in Steven's "Advanced Unix
Programming". I'd be grateful if you could kindly help me reason out
the logic to blocking the very signal which is supposed to trigger a
signal handler.

Thanks.

Paul Pluzhnikov

2006-01-23, 2:55 am

"sudeep" <sudeep.charles@gmail.com> writes:

> My doubt is that if sigprocmask is used to block SIGUSR1
> then how is the signal handler ever going to catch the SIGUSR1


The key is (from "man sigsuspend" on Linux):

The sigsuspend call temporarily replaces the signal mask for the
process with that given by mask and then suspends the process
until a signal is received.

So, if SIGUSR1 was not blocked before the first sigprocmask(),
then it will be unblocked again while the process is waiting for
a signal in sigsuspend().

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
sudeep

2006-01-23, 2:55 am

Hi Paul,

Thanks a bunch for your help.But how does sigsuspend ensure that the
problems associated with pause do not occur?

When pause is used and a signal occurs between the checking of the
global variable and the "pause" call itself the signal is lost. How
does the use of sigsuspend resolve the issue?

The man page says "The sigpending call allows the examination of
pending signals (ones
which have been raised while blocked)."

This still does not tell me how the issues with pause are resolved with
sigsuspend.

Thanks,
Sudeep Charles

Paul Pluzhnikov

2006-01-23, 2:55 am

"sudeep" <sudeep.charles@gmail.com> writes:

> But how does sigsuspend ensure that the
> problems associated with pause do not occur?


Sigsuspend() itself doesn't ensure anything.

> When pause is used and a signal occurs between the checking of the
> global variable and the "pause" call itself the signal is lost.


The reason for the "sigprocmask(SIG_BLOCK, ...)" is *precisely*
to ensure that the signal can *not* happen between the test and
the call to sigsuspend(), and so the signal can't be lost.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
sudeep

2006-01-23, 2:55 am

Hi Paul,
Thanks a lot for your time and clarification.
Thanks once again.

Regards,
Sudeep

Paul Pluzhnikov wrote:
> "sudeep" <sudeep.charles@gmail.com> writes:
>
>
> Sigsuspend() itself doesn't ensure anything.
>
>
> The reason for the "sigprocmask(SIG_BLOCK, ...)" is *precisely*
> to ensure that the signal can *not* happen between the test and
> the call to sigsuspend(), and so the signal can't be lost.
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com