Signal handling
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Signal handling




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Signal handling  
sudeep


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 07: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.






[ Post a follow-up to this message ]



    Re: Signal handling  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 07: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.





[ Post a follow-up to this message ]



    Re: Signal handling  
sudeep


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 07: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






[ Post a follow-up to this message ]



    Re: Signal handling  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 07: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.





[ Post a follow-up to this message ]



    Re: Signal handling  
sudeep


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 07: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.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:42 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register