在 Perl 中不关闭目录句柄有什么含义? [英] What are the implications of not closing a directory handle in Perl?

查看:22
本文介绍了在 Perl 中不关闭目录句柄有什么含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近继承了别人编写的一些代码.

I recently inherited some code that someone else had written.

我发现在代码中所有打开目录以供阅读的地方都没有关闭它,因为最初的开发人员有一个语法问题——他正在使用 close 函数来尝试关闭一个目录句柄而不是 closedir 函数.

I discovered that everywhere in the code that a directory was opened for reading, it was never closed because the original developer had a syntax problem - he was using the close function to try to close a directory handle instead of the closedir function.

代码是这样的:

opendir( DIR, $dir ) or die "Cannot open $dir: $!\n";
@files = readdir( DIR );
close( DIR );

(这是 Perl 最佳实践(第 208,278 页)中关于检查 close 函数的返回的另一个优点.如果检查了 close 的返回在这种情况下,它会因文件编号错误"而失败.)

(Which is another good point that is made in Perl Best Practices (pages 208,278) about checking the return of the close function. If the return of close were checked in this case, it would be failing with "Bad file number".)

我已经将其更改为 closedir,但这让我开始怀疑:既然目录句柄从未关闭,那么长时间保持目录句柄打开有什么负面影响?

I've since changed this to closedir, but it made me start wondering: Since the directory handle was never closed, what are the negative implications to keeping a directory handle opened for a long duration?

此程序较大(3,500 行代码),运行了一段时间(5-10 分钟),并且此程序的多个实例同时运行.对于上例中的此目录,$dir 对于所有实例都是相同的值.如果该程序的 10 个实例同时运行,则它们都针对同一目录持有一个打开的目录句柄达 5 分钟或更长时间.我确定 Perl 会在程序完成时自动关闭目录句柄,但最佳做法是尽快关闭它.

This program is larger (3,500 lines of code), runs for a while (5-10 minutes), and multiple instances of this program are running at the same time. In the case of this directory in the example above, $dir is the same value for all instances. If 10 instances of this program were running at the same time, they all held an open directory handle against the same directory for 5 minutes or longer. I'm sure Perl is closing the directory handle automatically when the program finishes, but best practice says to close it as soon as possible.

对我来说,让文件句柄保持打开状态会导致问题的地方更为明显(特别是对于为写入而打开的文件句柄),但是如果不关闭目录句柄会发生什么坏事?

It is more obvious to me where leaving file handles open can cause problems (especially for file handles that are open for writing), but what bad things can happen by not closing a directory handle?

我之所以这么问是因为在一个奇怪的情况下,这个程序试图创建一个文件(在上面由 $dir 定义的目录中).文件名中嵌入了 PID,因此文件已经存在的可能性较小,但是 Perl 无法打开文件进行写入,因为它说它已经存在.当我们查看目录时,该文件不存在.我想知道这个目录上所有打开的目录句柄是否都会导致这样的问题?

The reason I am asking is because there has been an odd circumstance where this program was trying to create a file (in the directory defined by $dir above). The filename had the PID embedded in it, so it is a smaller chance that the file could already be there, but Perl was unable to open the file for writing, because it said it already existed. When we looked in the directory, that file did not exist. I am wondering if all of the open directory handles on this directory could cause such a problem?

我不确定操作系统是否有所不同,但该程序在 AIX 上运行.

I'm not sure if the OS makes a difference, but this program is running on AIX.

提前致谢,周五快乐!

推荐答案

你浪费了一个目录描述符——它可能算作一个文件描述符.如果您的程序打开了足够多的目录以耗尽文件描述符,最终会伤害您.否则,它是非常无害的,虽然不太理想.它使系统(和 Perl)保留资源,否则它可能能够释放.

You wasted a directory descriptor - which probably counts as a file descriptor. It would ultimately hurt you if your program opened enough directories to run out of file descriptors. Otherwise, it is pretty harmless, though less than ideal. It makes the system (and Perl) keep resources around which it might otherwise be able to release.

如果目录句柄是一个局部变量,而不是一个普通的 DIR 样式的名称,那么您可能需要 Perl 清理.参见 opendir 说:

If the directory handle was a local variable, not a plain DIR-style name, you might have Perl cleaning up behind you. See opendir which says:

打开一个名为 EXPR 的目录,供 readdir、telldir、seekdir、rewinddir 和 closedir 处理.如果成功则返回真.DIRHANDLE 可能是一个表达式,其值可以用作间接目录句柄,通常是真正的目录句柄名称.如果 DIRHANDLE 是未定义的标量变量(或数组或散列元素),则为该变量分配对新匿名目录句柄的引用.DIRHANDLE 有独立于 FILEHANDLE 的命名空间.

Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and closedir. Returns true if successful. DIRHANDLE may be an expression whose value can be used as an indirect dirhandle, usually the real dirhandle name. If DIRHANDLE is an undefined scalar variable (or array or hash element), the variable is assigned a reference to a new anonymous dirhandle. DIRHANDLEs have their own namespace separate from FILEHANDLEs.

这篇关于在 Perl 中不关闭目录句柄有什么含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆