in reply to Re^5: Exiting a script with an 'infinitely looping' thread
in thread Exiting a script with an 'infinitely looping' thread

Due to Perl "safe signals", sleep is an uninteruptable opcode

The docs and my observations disagree.

#!/usr/bin/perl $\ = "\n"; $SIG{INT} = sub { print "SIGINT" }; print time; sleep(); # Sleep forever print time;
1227543323 SIGINT 1227543325

Safe signals means that the handler is only called after sleep returns, but sleep returns as soon as a signal is received.

The behaviour differs in Windows, but then again, there are no signals in Windows.

Replies are listed 'Best First'.
Re^7: Exiting a script with an 'infinitely looping' thread
by BrowserUk (Patriarch) on Nov 24, 2008 at 16:53 UTC
    The behaviour differs in Windows, ...

    Exactly. You know what I use...

    but then again, there are no signals in Windows.

    And yet:

    C:\test>perl -wle"1 while 1" Terminating on signal SIGINT(2) C:\test>perl -wle"1 while 1" Terminating on signal SIGBREAK(21)

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I meant at the OS level. What you see is Perl's emulation of signals. And as there usually is with emulations, there are incompatibilities in the behaviour of the emulated signals. That it doesn't work in Windows has nothing to do with safe signals, and all to do with a flawed emulation of sleep.

        That it doesn't work in Windows has nothing to do with safe signals,...

        On no?

        Try this (on a Windows system and perl 5.8.x using a cmd.exe exe console and a standard 101-key ps2-compatible keyboard configured for UK, on a Monday in November; shortly after 5.00 pm with a tail wind and a little good luck...)

        C:\test>set PERL_SIGNALS=safe C:\test>perl -wle"$SIG{INT}=sub{exit}; sleep 100" Terminating on signal SIGBREAK(21) C:\test>set PERL_SIGNALS=unsafe C:\test>perl -wle"$SIG{INT}=sub{exit}; sleep 100"

        In the first case, ^C has no effect, hence the need for ^break (SIGBREAK).

        However, the second example (with set PERL_SIGNALS=unsafe) it responds to ^C immediately.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.