在Windows (MinGW)中,我的程序从调用进程继承不必要的句柄。
这个过程不需要打开这些文件,但是由于它存在于父进程的生命周期之外,所以我会遇到打开文件时通常会遇到的问题。
在Linux上,我解决了这样的问题:
// Close all file descriptors
// It's hard to figure out how many are open, but the first 1000 should do
int fd;
for (fd = 0; fd < 1000; fd++)
close (fd);
这在Windows中似乎不起作用。
如何确定哪些文件
linux命令"lsof“返回正在访问的文件,我在shell中尝试使用vi打开一个文件,用kwrite打开另一个文件,然后返回并得到vi的进程,但没有kwrite进程,如下所示
[linux@localhost shell_ex]$ lsof +d .
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 3458 linux cwd DIR 253,2 50 953101 .
bash 3747 linux cwd DIR 253,2 50 95
Perl程序使用IPC::Run通过在运行时确定的一系列命令将文件输送到另一个文件中,就像下面这个小测试节选所演示的:
#!/usr/bin/perl
use IO::File;
use IPC::Run qw(run);
open (my $in, 'test.txt');
my $out = IO::File->new_tmpfile;
my @args = ( [ split / /, shift ], "<", $in); # this code
while ($#ARGV >= 0) {