如何使用“pipe”来促进 Perl 中的进程间通信?
谁能解释我如何成功地让我的进程进行通信?我发现 IPC 上的 perldoc 令人困惑。
到目前为止我所掌握的是:
$| = 1;
$SIG{CHLD} = {wait};
my $parentPid = $$;
if ($pid = fork();) ) {
if ($pid == 0) {
pipe($parentPid, $$);
open PARENT, "<$parentPid";
while (<PARENT>) {
print $_;
}
close PARENT;
exit();
} else {
pipe($parentPid, $pid);
open CHILD, ">$pid";
or error("\nError opening: childPid\nRef: $!\n");
open (FH, "<list")
or error("\nError opening: list\nRef: $!\n");
while(<FH>) {
print CHILD, $_;
}
close FH
or error("\nError closing: list\nRef: $!\n");
close CHILD
or error("\nError closing: childPid\nRef: $!\n);
} else {
error("\nError forking\nRef: $!\n");
}
第一: perldoc pipeline 是什么意思
READHANDLE
、WRITEHANDLE
?第二:我可以在不依赖CPAN或其他模块的情况下实现解决方案吗?
(来源:wellho.net)
Can anyone explain how I can successfully get my processes communicating? I find the perldoc on IPC confusing.
What I have so far is:
$| = 1;
$SIG{CHLD} = {wait};
my $parentPid = $;
if ($pid = fork();) ) {
if ($pid == 0) {
pipe($parentPid, $);
open PARENT, "<$parentPid";
while (<PARENT>) {
print $_;
}
close PARENT;
exit();
} else {
pipe($parentPid, $pid);
open CHILD, ">$pid";
or error("\nError opening: childPid\nRef: $!\n");
open (FH, "<list")
or error("\nError opening: list\nRef: $!\n");
while(<FH>) {
print CHILD, $_;
}
close FH
or error("\nError closing: list\nRef: $!\n");
close CHILD
or error("\nError closing: childPid\nRef: $!\n);
} else {
error("\nError forking\nRef: $!\n");
}
First: What does perldoc pipe mean by
READHANDLE
,WRITEHANDLE
?Second: Can I implement a solution without relying on CPAN or other modules?
(source: wellho.net)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是基于 perldoc perlipc 中的代码的简化示例:
Here is a simplified example based on code found in perldoc perlipc: