Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Print inside SIGNALS

by Corion (Patriarch)
on Jul 16, 2018 at 13:50 UTC ( [id://1218574]=note: print w/replies, xml ) Need Help??


in reply to Print inside SIGNALS

The following works for me:

alarm 2; $SIG{ALRM} = \&Finish; sleep 30; sub Finish { print "Timeout reached"; }

But also see perlipc on signals and how they (badly) interact with filehandles.

Replies are listed 'Best First'.
Re^2: Print inside SIGNALS
by pedrete (Sexton) on Jul 16, 2018 at 15:28 UTC

    Thank everybody...

    My actual perl program is huge to post it here. It does not use threads but it execute many external commands.

    The Signal works and the Finish sub is executed.... it deletes temp files, close FHs, etc... everything works BUT print.. it does not print anything in STDOUT!

    If the Finish sub is executed by the program itself (not via ALARM), then it prints to STDOUT normally..

    I know that you are not magicians and that without the full code you cannot be accurate, but i would appreciate any clue or suggestion you may provide, please...

      print returns false and sets $! on failure.

      Turn on autoflush (see perlvar):

      $| = 1;
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: Print inside SIGNALS
by pedrete (Sexton) on Jul 16, 2018 at 13:56 UTC

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

      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.

      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.

      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.

      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>?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found