signal handler - Linux

This is a discussion on signal handler - Linux ; Hi, I read in Advanced unix programming book that after a signal caught using signal catch function, for the same second signal default action 'll be taken. But when I executed in a box, for the second signal also same ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: signal handler

  1. signal handler

    Hi,

    I read in Advanced unix programming book that after a signal caught
    using signal catch function, for the
    same second signal default action 'll be taken.

    But when I executed in a box, for the second signal also same signal
    handler is executed.
    Is there anything wrong in my understanding?

    Below I pasted program which i executed.

    #include "signal.h"

    int cnt;

    void
    alrm_catcher(int signo)
    {
    printf("\n%d\n", cnt++);
    return;
    }

    main()
    {

    if (signal(SIGALRM, alrm_catcher) == SIG_ERR) {
    return;
    }

    alarm(5);
    pause();
    alarm(3);
    pause();
    }

    rakesh@MATRIX:~/deepak/signal$ ./a.out

    0

    1

    Thanks,
    Deepak

  2. Re: signal handler

    deepak wrote:
    > I read in Advanced unix programming book that after a signal caught
    > using signal catch function, for the
    > same second signal default action 'll be taken.


    > But when I executed in a box, for the second signal also same signal
    > handler is executed. Is there anything wrong in my understanding?


    No. It's a historical difference between BSD and SysV branches
    of UNIX. On BSD the signal handler reverted back to the default
    signal handler after catching a signal. On SysV the signal handler
    remained what you had set it to. Under Linux per default the
    signal handler does not get reset as you found out from your
    program. To make programs work under both BSD and SysV systems
    it was common practice to set the signal handler again in the
    signal handler itself to make sure it worked correctly on both
    types of systems.

    Nowadays you should be using sigaction() instead of signal()
    for setting up signal handlers and sigaction() lets you specify
    explicitely which behaviour you want, i.e. set the sa_flags
    member of the sigaction structure to SA_RESETHAND to get
    BSD like behaviour. If it's not set the signal handler does
    not get reset on receipt of a signal.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  3. Re: signal handler

    Jens Thoms Toerring schrieb:

    > To make programs work under both BSD and SysV systems
    > it was common practice to set the signal handler again in the
    > signal handler itself to make sure it worked correctly on both
    > types of systems.


    Yielding a race condition...

    > Nowadays you should be using sigaction() instead of signal()
    > for setting up signal handlers and sigaction() lets you specify
    > explicitely which behaviour you want, i.e. set the sa_flags
    > member of the sigaction structure to SA_RESETHAND to get
    > BSD like behaviour. If it's not set the signal handler does
    > not get reset on receipt of a signal.


    Definitely. signal() has way too many semantic meanings under different
    systems, sigaction() does not.

    Regards,
    Johannes

    --
    "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
    reicht zu wissen, daß andere es besser können und andere es auch
    besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
    in de.sci.electronics <47fa8447$0$11545$9b622d9e@news.freenet.de>

  4. Re: signal handler

    jt@toerring.de (Jens Thoms Toerring) writes:
    > deepak wrote:
    >> I read in Advanced unix programming book that after a signal caught
    >> using signal catch function, for the
    >> same second signal default action 'll be taken.

    >
    >> But when I executed in a box, for the second signal also same signal
    >> handler is executed. Is there anything wrong in my understanding?

    >
    > No. It's a historical difference between BSD and SysV branches
    > of UNIX. On BSD the signal handler reverted back to the default
    > signal handler after catching a signal. On SysV the signal handler
    > remained what you had set it to.


    You have this backwards. The original UNIX(*) signal imlementation
    resetted the action for a particular signal to the default action
    before invoking a user-defined signal handler. Hence, the user handler
    needed to be re-estalblished in the signal handling routine, which
    opened a time window during which the default action would be taken,
    no matter if a particular application 'wanted' that. To deal with this
    problem, both 4.2BSD and SVR3 added another set of signal handling
    interfaces. The BSD 'signal' routine was then reimplemented using the
    new facilities, which meant that the handler wouldn't be reset and
    that interrupted system calls would be restarted automatically, while
    'AT&T UNIX(*)' kept the old behaviour for backwards compatibility.
    The glibc signal defaults to the BSD semantics, but the other
    behaviour can be selected at compile time (by defining _XOPEN_SOURCE).

    The historical information is straight from section 10 of
    1st. ed. APUE, and sigaction was already standardized then
    (1993). It should be about time to really start using it in new code.



  5. Re: signal handler

    Rainer Weikusat wrote:
    > jt@toerring.de (Jens Thoms Toerring) writes:
    > > deepak wrote:
    > >> I read in Advanced unix programming book that after a signal caught
    > >> using signal catch function, for the
    > >> same second signal default action 'll be taken.

    > >
    > >> But when I executed in a box, for the second signal also same signal
    > >> handler is executed. Is there anything wrong in my understanding?

    > >
    > > No. It's a historical difference between BSD and SysV branches
    > > of UNIX. On BSD the signal handler reverted back to the default
    > > signal handler after catching a signal. On SysV the signal handler
    > > remained what you had set it to.


    > You have this backwards. The original UNIX(*) signal imlementation
    > resetted the action for a particular signal to the default action
    > before invoking a user-defined signal handler. Hence, the user handler
    > needed to be re-estalblished in the signal handling routine, which
    > opened a time window during which the default action would be taken,
    > no matter if a particular application 'wanted' that. To deal with this
    > problem, both 4.2BSD and SVR3 added another set of signal handling
    > interfaces. The BSD 'signal' routine was then reimplemented using the
    > new facilities, which meant that the handler wouldn't be reset and
    > that interrupted system calls would be restarted automatically, while
    > 'AT&T UNIX(*)' kept the old behaviour for backwards compatibility.
    > The glibc signal defaults to the BSD semantics, but the other
    > behaviour can be selected at compile time (by defining _XOPEN_SOURCE).


    > The historical information is straight from section 10 of
    > 1st. ed. APUE, and sigaction was already standardized then
    > (1993). It should be about time to really start using it in new code.


    Thanks for the correction. I should have rechecked with APUE
    instead of trusting my memory...
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  6. Re: signal handler

    On Jun 29, 5:24 am, j...@toerring.de (Jens Thoms Toerring) wrote:
    > Rainer Weikusat wrote:
    > > j...@toerring.de (Jens Thoms Toerring) writes:
    > > > deepak wrote:
    > > >> I read in Advanced unix programming book that after a signal caught
    > > >> using signal catch function, for the
    > > >> same second signal default action 'll be taken.

    >
    > > >> But when I executed in a box, for the second signal also same signal
    > > >> handler is executed. Is there anything wrong in my understanding?

    >
    > > > No. It's a historical difference between BSD and SysV branches
    > > > of UNIX. On BSD the signal handler reverted back to the default
    > > > signal handler after catching a signal. On SysV the signal handler
    > > > remained what you had set it to.

    > > You have this backwards. The original UNIX(*) signal imlementation
    > > resetted the action for a particular signal to the default action
    > > before invoking a user-defined signal handler. Hence, the user handler
    > > needed to be re-estalblished in the signal handling routine, which
    > > opened a time window during which the default action would be taken,
    > > no matter if a particular application 'wanted' that. To deal with this
    > > problem, both 4.2BSD and SVR3 added another set of signal handling
    > > interfaces. The BSD 'signal' routine was then reimplemented using the
    > > new facilities, which meant that the handler wouldn't be reset and
    > > that interrupted system calls would be restarted automatically, while
    > > 'AT&T UNIX(*)' kept the old behaviour for backwards compatibility.
    > > The glibc signal defaults to the BSD semantics, but the other
    > > behaviour can be selected at compile time (by defining _XOPEN_SOURCE).
    > > The historical information is straight from section 10 of
    > > 1st. ed. APUE, and sigaction was already standardized then
    > > (1993). It should be about time to really start using it in new code.

    >
    > Thanks for the correction. I should have rechecked with APUE
    > instead of trusting my memory...
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___ j...@toerring.de
    > \__________________________ http://toerring.de


    Thanks to all for clearing this.
    I thought my understanding was wrong.

    Thanks,
    Deepak

  7. Re: signal handler

    > void
    > alrm_catcher(int signo)
    > {
    > printf("\n%d\n", cnt++);
    > return;
    > }


    I would like to point out that this function implementation is not
    async-signal-safe.

    Regards,
    Markus

  8. Re: signal handler

    Real message restored

    Markus Elfring wrote:
    > > void
    > > alrm_catcher(int signo)
    > > {
    > > printf("\n%d\n", cnt++);
    > > return;
    > > }

    >
    > I would like to point out that this function implementation is not
    > async-signal-safe.
    >
    > Regards,
    > Markus


+ Reply to Thread