in reply to Re^13: Print inside SIGNALS (updated)
in thread Print inside SIGNALS

Hi haukex,

I have run your program and I keep getting 2 as a result too (on Linux)

I've tried to create something that also works on Windows:

alarm 2; $SIG{ALRM} = \&Finish; print "Sleep 5\n" ; sleep 5 ; sub Finish { print "Sleep 5 sig\n" ; sleep 5 ; print "Timeout reached\ +n"; } __END__ Sleep 5 Sleep 5 sig Timeout reached

You have to actually run this to see that it runs for 10 seconds. This shows that the first sleep is actually suspended and the sleep inside the Finish routine is executed, once if finishes it continues to execute the other previous sleep.

Yesterday I was testing on Linux and Windows back and forth and I think I got confused somewhere. It is clear to me that Windows behaves very different if it comes down to signals. I know that the OP asked for Linux assistance as well, so my response right now may be off topic I know.

Replies are listed 'Best First'.
Re^15: Print inside SIGNALS (updated)
by haukex (Archbishop) on Jul 20, 2018 at 20:37 UTC

    Thank you very much for your reply! As I replied to pryrt, I also now see the different behavior on Windows.

    During all my earlier testing I was actually reminded of an interaction between sleep and alarm: "You probably cannot mix alarm and sleep calls, because sleep is often implemented using alarm." Note how the second and third times are the same:

    $ perl -wMstrict -e '$SIG{ALRM}=sub{print time." Timeout"};print time. +" Before\n";alarm 2;sleep 10;print time." After\n"' 1532118865 Before 1532118867 Timeout1532118867 After

    But if you emulate sleep with polling, you see the same buffering issue as in the other cases - here, the second line of output isn't printed until after the 10 seconds are up:

    $ perl -wMstrict -e '$SIG{ALRM}=sub{print time." Timeout"};print time. +" Before\n";alarm 2;$a=time+10;1 while time<$a;print time." After\n"' 1532118963 Before 1532118965 Timeout1532118973 After

    Update: And on Windows:

    > perl -wMstrict -e "$SIG{ALRM}=sub{print time.qq{ Timeout}};print tim +e.qq{ Before\n};alarm 2;sleep 10;print time.qq{ After\n}" 1532119202 Before 1532119204 Timeout1532119212 After > perl -wMstrict -e "$SIG{ALRM}=sub{print time.qq{ Timeout}};print tim +e.qq{ Before\n};alarm 2;$a=time+10;1 while time<$a;print time.qq{ Aft +er\n}" 1532119272 Before 1532119274 Timeout1532119282 After

    Where each second line of output isn't printed until after 10 seconds.

      Ah yes, that explains stuff. On Linux I noticed that the sleep got interrupted and not on Windows

      I just finished my tests on Windows and I was just about to post the same results

      Where each second line of output isn't printed until after 10 seconds.

      Exactly

      Note: My head is still spinning around the suspended sleep (previous post) I think that the first sleep actually got suspended, I am actually pretty amazed by that, jee, think about it, it must almost be something like a process context switch...