我试图在perl中为同一个分叉进程设置多个管道。这是一个最小的例子,只有一个,但最后我希望这样有多个管道:
#!/usr/bin/perl
use Fcntl;
pipe PIPEREAD, PIPEWRITE;
# is supposed to increase the max file descriptors
$^F = 255; # default is 2
$flags = fcntl(PIPEREAD, F_GETFL, 0);
# doesn't do anything
fcntl(PIPEREAD, F_SETFL, $flags & (~FD_CLOEXEC)) or die "Can't set flags: $!\n";
if (!fork()) {
exec("cat", "/dev/fd/" . fileno(PIPEREAD));
}
print PIPEWRITE "Test\n";
close PIPEWRITE;
sleep(1);
此操作失败,因为上面的所有文件描述符
2
当我调用exec时关闭。我如何防止这种行为?
失败的原因
cat: /dev/fd/3: No such file or directory
我已尝试取消设置
FD_CLOEXEC
标志和增加
$^F
。没有任何效果。