Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use feature qw(say);
- use strict;
- use warnings;
- use POSIX ":sys_wait_h";
- local $SIG{ALRM} = sub { die "timed out\n"; };
- my $pid;
- my $start_time = time;
- #my $fh2;
- eval {
- alarm 5;
- #$pid = open $fh2, '-|', "sleep", 10;
- $pid = open my $fh, '-|', "sleep", 10;
- #$pid = open my $fh, "sleep 10|";
- #$pid = open FH, "sleep 10|";
- #$pid = open FH, '-|', "sleep", 10;
- say "reading line from child..";
- #my $line = <$fh2>; # hangs here for 10 seconds..
- my $line = <$fh>;
- #my $line = <FH>;
- #close $fh2;
- #close $fh;
- #close FH;
- say "end of eval..";
- alarm 0;
- };
- say $@ if $@;
- say "done. elapsed = ", time - $start_time;
- if ( $pid ) {
- say "PID = $pid";
- my $kid = waitpid $pid, WNOHANG;
- say "waitpid returned $kid";
- if ($kid == -1) {
- say "child does not exist";
- }
- if ($kid == 0) {
- say "Killing child $pid";
- kill 'KILL', $pid;
- say "Done.";
- }
- }
- else {
- say "No child started..";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement