我想在这里做的就是:
这是我到目前为止所得到的:我仍然需要实现一个循环来重复,直到用户没有输入消息。
#!perl -w
use strict
pipe(PIPE_READ, PIPE_WRITE);
autoflush PIPE_WRITE 1;
my $pid = fork();
if ($pid) {
&write_pipe ($pid);
waitpid($pid,0);
}
elsif (defined $pid) {
&read_pipe;
}
else {
die "cannot fork: $!";
}
sub write_pipe {
print "pid $$ \n";
print "Enter message: ";
sleep 1;
my $usr_msg = <>;
print "Parent pid = $$ message = $usr_msg";
print PIPE_WRITE "$usr_msg\n";
close(PIPE_WRITE)
close(PIPE_READ);
}
sub read_pipe {
print "child pid = $pid";
my $msg_read = <PIPE_READ>;
close(PIPE_WRITE);
print "received from pipe $msg_read";
}
答案 0 :(得分:2)
首先,您无意中创建了两个孩子。取代
if ($pid = fork)
与
if ($pid)
在孩子中,请致电
close(PIPE_WRITE);
在家长中,请致电
close(PIPE_READ);
在父母(完成写作)时,请致电
close(PIPE_WRITE);
至于从文件句柄读取,直到输入EOF或特定命令,
while (my $line = <>) {
last if $line =~ /^(?:quit|exit)$/;
...
}