程序中找到打开的全局文件句柄

程序中找到打开的全局文件句柄

本文介绍了如何在 perl 程序中找到打开的全局文件句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了一个问题,我必须关闭所有打开的文件句柄,我的 Apache cgi 脚本才能继续.我将问题追溯到 Parse::RecDescent.

I just tracked down a problem where I had to close all open filehandles for my Apache cgi script to continue. I traced the problem to 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;
}

我的问题是如何找到所有打开的包文件句柄?

My question is how do I find all open package filehandles?

我知道 fileno 会返回一个打开的文件句柄的计数器.有没有办法对这些进行反向查找,或者通过它们的 fileno 计数器关闭文件句柄?

I know fileno will return a counter for an open filehandle.Is there a way to do a reverse lookup for these, or close filehandles by their fileno counter?

推荐答案

在某些系统上,"/proc/$$/fd/" 返回的目录包含打开的文件描述符列表.您可以使用 POSIX::close 关闭它们.

On some systems, the directory returned by "/proc/$$/fd/" contains the list of open file descriptors. You could use POSIX::close to close them.

# close all filehandles
for (glob "/proc/$$/fd/*") { POSIX::close($1) if m{/(\d+)$}; }

这篇关于如何在 perl 程序中找到打开的全局文件句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:31