我刚刚找到了一个问题,我必须关闭所有打开的文件句柄才能继续我的Apache cgi脚本.我将问题追溯到Parse :: RecDescent.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
$|++;
print "Content-Type: text/plain\n\n";
use Parse::RecDescent;
say "$$: pre-fork: ". time;
if(my $pid = fork) {
# parent
say "$$: return immediately: ". time;
}
else {
# child
say "$$: kicked off big process: ". time;
close STDIN;
close STDOUT;
close STDERR;
# close *{'Parse::RecDescent::ERROR'};
sleep 5;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何找到所有打开包文件句柄?
我知道fileno会返回一个打开文件句柄的计数器.有没有办法对它们进行反向查找,或者通过fileno计数器关闭文件句柄?
在某些系统上,返回的目录"/proc/$$/fd/"包含打开的文件描述符列表.你可以POSIX::close用来关闭它们.
# close all filehandles
for (glob "/proc/$$/fd/*") { POSIX::close($1) if m{/(\d+)$}; }
Run Code Online (Sandbox Code Playgroud)