in reply to Re^4: Cannot catch ALRM signal for timeout
in thread Cannot catch ALRM signal for timeout

It still stuck, not alarmed for some reasons. My code right now has become:
sub process_scheme{ my ($output_file) = @_; open (OUTPUT_FILE, ">", $output_file) || die "cannot open log file +"; my $scheme_pid = open (SCHEME, "./t.pl | ") || die "cannot pipe sc +heme"; my $str = ""; MAIN: while (1) { eval{ local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n require +d alarm 5;#$allow_time; if (eof(SCHEME)){ close OUTPUT_FILE; close SCHEME; last MAIN; } $str = <SCHEME>; alarm 0; }; if ($@) { close OUTPUT_FILE; kill $scheme_pid; close SCHEME; return -$current_player; } # process the line print OUTPUT_FILE $str; } return 1; }
where the t.pl is
while(1){};
The script gets stuck, waiting forever, not stopping/returning anything

Replies are listed 'Best First'.
Re^6: Cannot catch ALRM signal for timeout
by almut (Canon) on Dec 08, 2009 at 23:35 UTC
    kill $scheme_pid;

    You probably meant something like

    kill INT => $scheme_pid;

    As you have it, kill would try to send the signal number $scheme_pid to no processes — so nothing happens, effectively...

    As a result, your code hangs in the implicit wait behind close SCHEME, because the child process is still alive.

Re^6: Cannot catch ALRM signal for timeout
by ikegami (Patriarch) on Dec 08, 2009 at 22:45 UTC
    what makes you think it's stuck? Are you sure it's really stuck and that you're not suffering from buffering? Add
    OUTPUT_FILE->autoflush(1);
    after the open. You'll need
    use IO::Handle qw( );
    at the top.