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