# Example of pipe problem # Useage: perl pipetest.pl 2 # $ARV[0] represents how many processes to fork off, run with 2 and it will # send a line of text from the main process and print it to STDOUT pipe READ1, WRITE1; pipe READ2, WRITE2; pipe READ3, WRITE3; pipe READ4, WRITE4; pipe READ5, WRITE5; for(1..$ARGV[0]) { $forke++; my $pid; if($pid = fork) { # parent $| = 1; $SIG{CHLD} = 'IGNORE'; if ($forke == $ARGV[0]) {&server;} } else { $| = 1; # child die "cannot fork :$!" unless defined $pid; print "Forked $forke\n"; for (;;) { $read_pipe="READ$forke"; while (<$read_pipe>) {print "Child $forke: parent said \"$_\"\n"}; } } } sub server { $t=0; for (1..5) {$t++; $write_pipe="WRITE$t"; select($write_pipe); $|=1; print "TESTING 1 2 3\n"; } }