Perl,如何为我的exec'd孩子创建一个管道?

6 c unix perl pipe

我试图使用管道(单向)将数据从我的perl脚本传递到我的c程序.我需要找到一种方法来做到这一点,而不会弄乱子程序STDIN或STDOUT,所以我尝试创建一个新句柄并传递fd.

我创建了2个IO :: Handles并创建了一个管道.我写到管道的一端,并尝试将管道另一端的文件描述符传递给正在执行的子程序.我通过设置ENV变量来传递文件描述符.为什么这不起作用?(它没有打印出"你好世界").据我所知,文件描述符和管道在执行时由子进程继承.

Perl脚本:

#!/opt/local/bin/perl
use IO::Pipe;
use IO::Handle;

my $reader = IO::Handle->new();
my $writer = IO::Handle->new();
$reader->autoflush(1);
$writer->autoflush(1);
my $pipe = IO::Pipe->new($reader, $writer);
print $writer "hello world";
my $fh = $reader->fileno;
$ENV{'MY_FD'} = $fh;
exec('./child') or print "error opening app\n";
# No more code after this since exec replaces the current process
Run Code Online (Sandbox Code Playgroud)

C程序,app.c(编译gcc app.c -o child):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char ** argv) {
  int fd = atoi(getenv("MY_FD"));
  char buf[12];
  read(fd, buf, 11);
  buf[11] = '\0';
  printf("fd: %d\n", fd);
  printf("message: %s\n", buf);
}
Run Code Online (Sandbox Code Playgroud)

输出:

fd: 3
message:
Run Code Online (Sandbox Code Playgroud)

消息永远不会通过管道传递给C程序.有什么建议?

pil*_*row 2

您的管道文件描述符设置为 FD_CLOEXEC,因此在 exec() 时关闭。

Perl$^F控制这种行为。在调用 IO::Pipe->new之前尝试这样的事情:

$^F = 10;  # Assumes we don't already have a zillion FDs open
Run Code Online (Sandbox Code Playgroud)

或者,您可以在创建管道后使用Fcntl自行清除 FD_CLOEXEC 标志。