【问题标题】:IO handle for STDOUT in Perl on Windows when forking?分叉时 Windows 上 Perl 中 STDOUT 的 IO 句柄?
【发布时间】:2011-06-10 21:15:03
【问题描述】:

我有以下问题: 我尝试将 perl 脚本移植到 Windows。 该脚本经常 fork 自身,并使用此代码进行 fork:

sub sub_fork {
    my ( $subref, @args ) = @_;

    my $fh = new IO::Handle;
    my $pid = open( $fh, "-|" );

    if ( $pid ) { # parent
 return ( $fh, $pid );
    }
    else {
 &$subref( @args );
 exit;
    }
}

Windows 不喜欢“-|”它似乎。 作为一个 Windows 用户,我实际上不知道这是做什么的,但这里有一些有趣的东西:http://www.cs.tufts.edu/comp/150PPP/notes/perl_ipc.php(搜索“Fancy opens”) 该代码在脚本中经常使用,因此我想将 sub 替换为完全相同的代码,这意味着它返回 $fh, $pid 其中 $fh 是子级标准输出的句柄。

【问题讨论】:

    标签: perl ipc fork


    【解决方案1】:

    此构造用于interprocess communication,专门用于创建从新后台进程的标准输出读取的文件句柄。

    my $pid = open my $fh, "-|";
    if ($pid == 0) { # child
        print "Hello world\n";
        exit;
    }
    print <$fh>;       #  Hello world\n
    

    请参阅perlipc doc,了解为什么这种构造很有用。

    这仅适用于具有“真正的 fork”的系统,但不包括 Windows。 Windows 解决方法将涉及使用 socketpair 为 IPC 创建一些套接字(唉,pipe 也不能胜任 Windows 上的任务)。这样的事情会起作用:

    sub sub_fork {
        my ($subref, @args) = @_;
        use Socket;
        my ($READER, $WRITER);
        socketpair $READER, $WRITER, AF_UNIX, SOCK_STREAM, PF_UNSPEC;
        shutdown($READER,1);    # close write channel for $READER
        shutdown($WRITER,0);    # and read channel for $WRITER
        my $pid = fork();
        if ( $pid ) {
            return ($READER, $pid);
        } else {
            close STDOUT;
            open STDOUT, '>&' . fileno($WRITER);    # dup STDOUT to print to $WRITER
    
            &$subref(@args);
    
            # both of these steps are required before you exit the child
            close STDOUT;
            shutdown($WRITER,1);
            exit;
        }
    }
    

    Forks::Super 模块(我编写的)也可以在 Windows 中处理此任务。

    use Forks::Super;
    sub sub_fork {
        my ($subref, @args) = @_;
        my $pid = fork { child_fh => 'out' };   # make child's STDOUT available
        if ($pid != 0) {
            return ($Forks::Super::CHILD_STDOUT{$pid}, $pid);
            #alternate: return ($pid->{child_stdout}, $pid);
        } else {
            &$subref(@args);
            exit;
        }
    }
    

    甚至更简洁

    use Forks::Super;
    sub sub_fork {
        my ($subref, @args) = @_;
        my $pid = fork { 
                     child_fh => 'out',
                     sub => $subref, args => \@args   # run $subref->(@args) in child
                  }; 
        return ($Forks::Super::CHILD_STDOUT{$pid}, $pid);
    }
    

    【讨论】:

    • 谢谢,这很有帮助!不幸的是,Forks::Super 模块似乎被 Strawberry Perl 破坏了。第一种方法有效。它在数据前面添加了“-[0-9]{4}”(例如“-1132”。我修改了正则表达式以相应地验证孩子的响应,但如果有办法删除它会很感兴趣在 sub_fork 中。
    • @Josef - 那是 Windows,而不是 Forks::Super 模块。 Windows 中的fork 调用会创建具有负进程 ID 的“伪进程”(实际上是 Windows 线程)。 0 和 -1 以外的任何值都表示成功。还有一个Forks::Super::isValidPid($pid) 函数可移植地检查fork 返回值。
    • 请参阅perldoc perlfork,了解有关 Windows 上 fork 仿真的更多讨论。
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多