1N/A#!./perl
1N/A
1N/A#####################################################################
1N/A#
1N/A# Test for process id return value from open
1N/A# Ronald Schmidt (The Software Path) RonaldWS@software-path.com
1N/A#
1N/A#####################################################################
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A require './test.pl';
1N/A}
1N/A
1N/Aif ($^O eq 'dos' || $^O eq 'MacOS') {
1N/A skip_all("no multitasking");
1N/A}
1N/A
1N/Aplan tests => 10;
1N/A
1N/A
1N/Ause Config;
1N/A$| = 1;
1N/A$SIG{PIPE} = 'IGNORE';
1N/A
1N/Amy $perl = which_perl();
1N/A$perl .= qq[ "-I../lib"];
1N/A
1N/A#
1N/A# commands run 4 perl programs. Two of these programs write a
1N/A# short message to STDOUT and exit. Two of these programs
1N/A# read from STDIN. One reader never exits and must be killed.
1N/A# the other reader reads one line, waits a few seconds and then
1N/A# exits to test the waitpid function.
1N/A#
1N/A$cmd1 = qq/$perl -e "\$|=1; print qq[first process\\n]; sleep 30;"/;
1N/A$cmd2 = qq/$perl -e "\$|=1; print qq[second process\\n]; sleep 30;"/;
1N/A$cmd3 = qq/$perl -e "print <>;"/; # hangs waiting for end of STDIN
1N/A$cmd4 = qq/$perl -e "print scalar <>;"/;
1N/A
1N/A#warn "#$cmd1\n#$cmd2\n#$cmd3\n#$cmd4\n";
1N/A
1N/A# start the processes
1N/Aok( $pid1 = open(FH1, "$cmd1 |"), 'first process started');
1N/Aok( $pid2 = open(FH2, "$cmd2 |"), ' second' );
1N/Aok( $pid3 = open(FH3, "| $cmd3"), ' third' );
1N/Aok( $pid4 = open(FH4, "| $cmd4"), ' fourth' );
1N/A
1N/Aprint "# pids were $pid1, $pid2, $pid3, $pid4\n";
1N/A
1N/Amy $killsig = 'HUP';
1N/A$killsig = 1 unless $Config{sig_name} =~ /\bHUP\b/;
1N/A
1N/A# get message from first process and kill it
1N/Achomp($from_pid1 = scalar(<FH1>));
1N/Ais( $from_pid1, 'first process', 'message from first process' );
1N/A
1N/A$kill_cnt = kill $killsig, $pid1;
1N/Ais( $kill_cnt, 1, 'first process killed' ) ||
1N/A print "# errno == $!\n";
1N/A
1N/A# get message from second process and kill second process and reader process
1N/Achomp($from_pid2 = scalar(<FH2>));
1N/Ais( $from_pid2, 'second process', 'message from second process' );
1N/A
1N/A$kill_cnt = kill $killsig, $pid2, $pid3;
1N/Ais( $kill_cnt, 2, 'killing procs 2 & 3' ) ||
1N/A print "# errno == $!\n";
1N/A
1N/A
1N/A# send one expected line of text to child process and then wait for it
1N/Aselect(FH4); $| = 1; select(STDOUT);
1N/A
1N/Aprintf FH4 "ok %d - text sent to fourth process\n", curr_test();
1N/Anext_test();
1N/Aprint "# waiting for process $pid4 to exit\n";
1N/A$reap_pid = waitpid $pid4, 0;
1N/Ais( $reap_pid, $pid4, 'fourth process reaped' );
1N/A