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

Sorry, but no, the central point of this discussion has nothing to do with SIGINT, and even whether something is output to STDOUT is a secondary issue. Above, ikegami said that the SIGALRM handler would fire after and not during <>, which I doubted, tested, and showed that it's not how it behaves - at least on my system, in case that makes a difference, hence my oneliners and questions above. The claim that I must've sent the SIGINT before the SIGALRM handler could fire is nonsense.

Replies are listed 'Best First'.
Re^13: Print inside SIGNALS
by Veltro (Hermit) on Jul 19, 2018 at 20:09 UTC

    I don't care much about what ikagami said exactly, but you did kill the program is what he noticed, and that is what I focus on. As far as I'm concerned that is the reason why your example does not print anything. Maybe a piece of code will help:

    use strict ; use warnings ; alarm 2 ; $SIG{'ALRM'} = sub{ print "Timeout reached" } ; $SIG{'INT'} = sub { exit(0) } ; <> ; __END__ [/tmp] # perl pltst1.pl ^C[/tmp] # perl pltst1.pl ^CTimeout reached[/tmp] # (1. ctrl-c pressed < 2 seconds 2. ctrl-c pressed > 2 seconds)

    1. So you see, it does matter if you kill the program. 2. Whether something is being outputted to STDOUT is not a secondary issue. It matters everything. STDERR and STDOUT buffer mechanisms are fundamentaly different.

    edit: So if people are down-voting this that is fine, but then at least show me where I am wrong please.

    edit 2: Examples added regards the difference between STDOUT and STDERR below (Pevious example was Linux, this was on Windows):

    WITH syswrite STDERR and print STDERR:

    use strict ; use warnings ; alarm 2 ; $SIG{ALRM} = sub{ syswrite STDERR, "s Timeout reached " ; print STDERR "p Timeout reached " ; } ; sleep(4) ; syswrite STDERR, "s Program end " ; print STDERR "p Program end " ; sleep(2) ; __END__ C:\perlproject>perl sigtst.pl s Timeout reached p Timeout reached s Program end p Program end s Timeout reached - Printed after 2 seconds p Timeout reached - Printed after 2 seconds s Program end - Printed after 4 seconds p Program end - Printed after 4 seconds

    WITH syswrite STDOUT and print STDOUT:

    use strict ; use warnings ; alarm 2 ; $SIG{ALRM} = sub{ syswrite STDOUT, "s Timeout reached " ; print STDOUT "p Timeout reached " ; } ; sleep(4) ; syswrite STDOUT, "s Program end " ; print STDOUT "p Program end " ; sleep(2) ; __END__ C:\perlproject>perl sigtst.pl s Timeout reached s Program end p Timeout reached p Program end s Timeout reached - Printed after 2 seconds s Program end - Printed after 4 seconds p Timeout reached - Printed after 6 seconds p Program end - Printed after 6 seconds
      I don't care much about what ikagami said exactly

      That's a strange thing to say when that's all this sub-thread is about. Please re-read the thread starting from here.

      ctrl-c pressed < 2 seconds

      No, that's exactly the thing I have an issue with: I certainly have a dense moment now and then, but I'm not dumb enough to set an alarm for two seconds upon which something is to be printed, kill the program before the two seconds is up, and then use this as my evidence that nothing was printed. And I'm going to take special care when I'm pointing out a potential mistake to a Monk whose technical knowledge is much deeper than mine and who is almost always right in his posts.

      And it should be obvious to anyone that I didn't hit Ctrl-C within the first two seconds if they had actually run my first two one-liners (again, assuming their system behaves the same as mine). Which is why I posted them in the first place!

      but you did kill the program ... As far as I'm concerned that is the reason why your example does not print anything.

      Then you are making this same incorrect (and insulting) assumption that ikegami did. Did you try out my most recent one-liner that has nothing to do with SIGINT?

      STDOUT ... matters everything

      No, it does not.

      $ perl -e '$x=time; $SIG{ALRM}=sub{$y=time; close STDIN}; alarm 2; <>; print STDERR $y-$x," ",time-$x,"\n"' 2 2

      Just to make it clear: This shows how <> is interrupted by $SIG{ALRM}, without STDOUT being involved at all. Which is why I think that the first snippet posted by pedrete here is just suffering from buffering (but I'm always willing to be proven wrong).

      $SIG{'INT'} = sub { exit(0) } ;

      This misses the point, because of course the output buffers get flushed on exit. With buffering issues, the question is not whether something gets printed, but when. And perhaps you overlooked that the OP confirmed that it was a buffering issue all along.

      I've said all I can say until someone shows some actual evidence to the contrary.

        In this part:

        (1. ctrl-c pressed < 2 seconds 2. ctrl-c pressed > 2 seconds)

        It was me who pressed ctrl-c within 2 seconds to show the output of the program when I would. I have never said that you did.

        So fine, I feel that you that you are twisting my words and I guess this is the way you want to go, so let this be the last time I will bring up an issue with you. I've never tried to insult you. I guess that's what you get speaking out to 'popular' monks. Disregards of whatever I tried to bring up.

        And it should be obvious to anyone that I didn't hit Ctrl-C within the first two seconds

        duh. I never claimed you killed it within the first two seconds. I said you killed it before <> returned, and thus before the signal handler was called.

        Then you are making this same incorrect (and insulting) assumption that ikegami did.

        You tried to disprove my claim that the signal handler does get called once <> returns by killing the process before it returns. So you failed. But now, you're compounding that by making stupid assumptions. Stop, and think.