Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Print inside SIGNALS

by pedrete (Sexton)
on Jul 16, 2018 at 13:56 UTC ( [id://1218576]=note: print w/replies, xml ) Need Help??


in reply to Re: Print inside SIGNALS
in thread Print inside SIGNALS

Thanks Corion... it does not work in my Debian box ... :-( I edit... It works! it seems ALARM only affects user level calls...

Replies are listed 'Best First'.
Re^3: Print inside SIGNALS
by haj (Vicar) on Jul 16, 2018 at 14:19 UTC

    I'm on a Debian box as well, and the behaviour is like this:

    • Your original code doesn't print anything because the program finishes before the timer rings. That's quite normal.
    • With Corion's example, I get the print - but not the full sleep time, the code immediately resumes after the alarm. I didn't know that, but the alarm documentation gives a hint: It is usually a mistake to intermix "alarm" and "sleep" calls, because "sleep" may be internally implemented on your system with "alarm".

    If your C library's print function isn't re-entrant (many aren't), then there's always a chance that weird things happen when the signal goes off while your code is printing something.

      If your C library's print function isn't re-entrant (many aren't), then there's always a chance that weird things happen when the signal goes off while your code is printing something.

      That's not true for two reasons:

      • print doesn't use clib.
      • Since Perl 5.8.1, signals are deferred to be handled between Perl ops.
        Thanks for the correction! The rule "don't print in signal handling" stuck with me for too long, I should have checked this.
Re^3: Print inside SIGNALS
by jeffenstein (Hermit) on Jul 16, 2018 at 14:26 UTC

    Are you using threads in your script? From the threads documentation, signals in threads are not real signals and won't interrupt a blocked system call.

Re^3: Print inside SIGNALS
by ikegami (Patriarch) on Jul 16, 2018 at 14:24 UTC

    it seems ALARM only affects user level calls..

    Not at all. As Corion's program demonstrates, system calls are clearly interrupted.

    The signal will only be handled between Perl ops, so a long running op (regex match or XS function call) could delay the handling of the signal.

Re^3: Print inside SIGNALS
by ikegami (Patriarch) on Jul 16, 2018 at 14:04 UTC

    Then something is really messed up with your box. Unless you didn't actually run Corion's program?

      This code does not work...

      alarm 2; $SIG{ALRM} = \&Finish; my $a=<>; sub Finish { print "Timeout reached"; }

      This code does not work either...

      alarm 2; $SIG{ALRM} = \&Finish; while (1==1){}; sub Finish { print "Timeout reached"; }

      So i guess many calls are not interrumptibles... am i right>?

        So i guess many calls are not interrumptibles... am i right?

        I don't think that's the problem here - see my reply to your nearly identical post here.

        First snippet: <> can't be interrupted because there's no way to check <> for errors. However, the signal handler does get called once it returns.

        Second snippet: There's nothing to interrupt here, so the signal handler gets called immediately. However, since the program has no way of terminating the program cleanly, the message will never leave STDOUT's buffer to the underlying file system handle.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218576]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found