|
#!/usr/bin/perl
# Fig. 18.11: fig18_11.pl
# Using pipe to communicate with child processes.
use warnings;
use strict;
use IO::Handle;
pipe( CHILDINPUT, PARENTOUTPUT );
pipe( PARENTINPUT, CHILDOUTPUT );
PARENTOUTPUT->autoflush( 1 );
CHILDOUTPUT->autoflush( 1 );
STDOUT->autoflush( 1 );
if ( my $pid = fork() ) {
for ( 1 .. 10 ) {
print( PARENTOUTPUT "$_\n" );
print( "I said $_, " );
chomp( my $response = < ARENTINPUT> );
print( "and he said $response!\n" );
}
#这里开始的 close()语句
close( PARENTOUTPUT ) or
die( "Cannot close PARENTOUTPUT: $!" );
close( PARENTINPUT ) or
die( "Cannot close PARENTINPUT: $!" );
}
elsif ( defined( $pid ) ) {
close( PARENTOUTPUT ) or
die( "Cannot close PARENTOUTPUT: $!" );
close( PARENTINPUT ) or
die( "Cannot close PARENTINPUT: $!" );
while ( <CHILDINPUT> ) {
chomp();
print( CHILDOUTPUT $_ * 20, "\n" );
}
close( CHILDOUTPUT ) or
die( "Cannot close CHILDOUTPUT: $!" );
close( CHILDINPUT ) or
die( "Cannot close CHILDINPUT: $!" );
exit();
}
else {
die( "Fork did not work" );
}
想问的是,
1.为什么要关闭管道?
2.关闭了某个句柄,管道还可以使用吗?
3.close()函数对管道有什么影响?
我是个菜鸟,实在不明白管道的原理,还请老鸟指教,先谢了 |
|