如何使用“pipe”来促进 Perl 中的进程间通信?

发布于 2024-08-26 23:50:18 字数 1337 浏览 4 评论 0原文

谁能解释我如何成功地让我的进程进行通信?我发现 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 是什么意思READHANDLEWRITEHANDLE

  • 第二:我可以在不依赖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?

alt text
(source: wellho.net)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2024-09-02 23:50:18

以下是基于 perldoc perlipc 中的代码的简化示例:

#!/usr/bin/perl

use strict; use warnings;

$SIG{CHLD} = sub { wait };

pipe my $reader, my $writer;

my $pid = fork;
defined $pid or die "fork failed: $!";

if ( $pid ) { # parent
    close $reader;
    for my $n ( 1 .. 5 ) {
        select $writer;
        $| = 1;
        print $writer "Message $n\n"
            or die "Failed to pass message to child: $!";
        sleep 1;
    }
    close $writer;
    exit;
}
else { # child
    close $writer;
    while ( my $msg = <$reader> ) {
        print "Child received: $msg";
        last if rand > 0.5; # to check error handling in parent
    }
    close $reader;
    exit;
}

Here is a simplified example based on code found in perldoc perlipc:

#!/usr/bin/perl

use strict; use warnings;

$SIG{CHLD} = sub { wait };

pipe my $reader, my $writer;

my $pid = fork;
defined $pid or die "fork failed: $!";

if ( $pid ) { # parent
    close $reader;
    for my $n ( 1 .. 5 ) {
        select $writer;
        $| = 1;
        print $writer "Message $n\n"
            or die "Failed to pass message to child: $!";
        sleep 1;
    }
    close $writer;
    exit;
}
else { # child
    close $writer;
    while ( my $msg = <$reader> ) {
        print "Child received: $msg";
        last if rand > 0.5; # to check error handling in parent
    }
    close $reader;
    exit;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文