| 1 | #!./perl
|
|---|
| 2 |
|
|---|
| 3 | #####################################################################
|
|---|
| 4 | #
|
|---|
| 5 | # Test for process id return value from open
|
|---|
| 6 | # Ronald Schmidt (The Software Path) RonaldWS@software-path.com
|
|---|
| 7 | #
|
|---|
| 8 | #####################################################################
|
|---|
| 9 |
|
|---|
| 10 | BEGIN {
|
|---|
| 11 | chdir 't' if -d 't';
|
|---|
| 12 | @INC = '../lib';
|
|---|
| 13 | require './test.pl';
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | if ($^O eq 'dos' || $^O eq 'MacOS') {
|
|---|
| 17 | skip_all("no multitasking");
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | plan tests => 10;
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | use Config;
|
|---|
| 24 | $| = 1;
|
|---|
| 25 | $SIG{PIPE} = 'IGNORE';
|
|---|
| 26 | $SIG{HUP} = 'IGNORE' if $^O eq 'interix';
|
|---|
| 27 |
|
|---|
| 28 | my $perl = which_perl();
|
|---|
| 29 | $perl .= qq[ "-I../lib"];
|
|---|
| 30 |
|
|---|
| 31 | #
|
|---|
| 32 | # commands run 4 perl programs. Two of these programs write a
|
|---|
| 33 | # short message to STDOUT and exit. Two of these programs
|
|---|
| 34 | # read from STDIN. One reader never exits and must be killed.
|
|---|
| 35 | # the other reader reads one line, waits a few seconds and then
|
|---|
| 36 | # exits to test the waitpid function.
|
|---|
| 37 | #
|
|---|
| 38 | $cmd1 = qq/$perl -e "\$|=1; print qq[first process\\n]; sleep 30;"/;
|
|---|
| 39 | $cmd2 = qq/$perl -e "\$|=1; print qq[second process\\n]; sleep 30;"/;
|
|---|
| 40 | $cmd3 = qq/$perl -e "print <>;"/; # hangs waiting for end of STDIN
|
|---|
| 41 | $cmd4 = qq/$perl -e "print scalar <>;"/;
|
|---|
| 42 |
|
|---|
| 43 | #warn "#$cmd1\n#$cmd2\n#$cmd3\n#$cmd4\n";
|
|---|
| 44 |
|
|---|
| 45 | # start the processes
|
|---|
| 46 | ok( $pid1 = open(FH1, "$cmd1 |"), 'first process started');
|
|---|
| 47 | ok( $pid2 = open(FH2, "$cmd2 |"), ' second' );
|
|---|
| 48 | ok( $pid3 = open(FH3, "| $cmd3"), ' third' );
|
|---|
| 49 | ok( $pid4 = open(FH4, "| $cmd4"), ' fourth' );
|
|---|
| 50 |
|
|---|
| 51 | print "# pids were $pid1, $pid2, $pid3, $pid4\n";
|
|---|
| 52 |
|
|---|
| 53 | my $killsig = 'HUP';
|
|---|
| 54 | $killsig = 1 unless $Config{sig_name} =~ /\bHUP\b/;
|
|---|
| 55 |
|
|---|
| 56 | # get message from first process and kill it
|
|---|
| 57 | chomp($from_pid1 = scalar(<FH1>));
|
|---|
| 58 | is( $from_pid1, 'first process', 'message from first process' );
|
|---|
| 59 |
|
|---|
| 60 | $kill_cnt = kill $killsig, $pid1;
|
|---|
| 61 | is( $kill_cnt, 1, 'first process killed' ) ||
|
|---|
| 62 | print "# errno == $!\n";
|
|---|
| 63 |
|
|---|
| 64 | # get message from second process and kill second process and reader process
|
|---|
| 65 | chomp($from_pid2 = scalar(<FH2>));
|
|---|
| 66 | is( $from_pid2, 'second process', 'message from second process' );
|
|---|
| 67 |
|
|---|
| 68 | $kill_cnt = kill $killsig, $pid2, $pid3;
|
|---|
| 69 | is( $kill_cnt, 2, 'killing procs 2 & 3' ) ||
|
|---|
| 70 | print "# errno == $!\n";
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | # send one expected line of text to child process and then wait for it
|
|---|
| 74 | select(FH4); $| = 1; select(STDOUT);
|
|---|
| 75 |
|
|---|
| 76 | printf FH4 "ok %d - text sent to fourth process\n", curr_test();
|
|---|
| 77 | next_test();
|
|---|
| 78 | print "# waiting for process $pid4 to exit\n";
|
|---|
| 79 | $reap_pid = waitpid $pid4, 0;
|
|---|
| 80 | is( $reap_pid, $pid4, 'fourth process reaped' );
|
|---|
| 81 |
|
|---|