views:

297

answers:

2

I have the following code that tries to tie arrays to files. Except, when I run this code, it only creates 2045 files. What is the issue here?

#!/usr/bin/perl
use Tie::File;

for (my $i = 0; $i < 10000; $i++) {
    @files{$i} = ();
    tie @{$files{$i}}, 'Tie::File', "files//tiefile$i";
}

Edit: I am on windows

+14  A: 

You are accumulating open file handles (see ulimit -n, setrlimit RLIMIT_NOFILE/RLIMIT_OFILE), and you ultimately hit a 2048 open file descriptors limit (2045 + stdin + stdout + stderr.)

Under Windows you will have to rewrite your application so that it has at most 2048 open file handles at any one time, since the 2048 limit is hard limit (cannot be modified) in MSVC's stdio.

vladr
Is there an equivalent command if I am using Windows and ActivePerl?
Jack L.
http://msdn.microsoft.com/en-us/library/6e3b887c.aspx I'm not sure how to call it from Perl without writing your own XS or using Inline::C, though.
ephemient
Unfortunately even setmaxstdio is limited to 2048 file handles. An application rewrite may be needed.
vladr
BTW, SetHandleCount does nothing in win32: http://msdn.microsoft.com/en-us/library/aa383722.aspx
ephemient
@ephemient, correct. It has been deprecated. The hard 2048 limit comes from _setmaxstdio and cannot be modified.
vladr
Really, do you need to hold that many filehandles open simultaneously? Abstract them to private one-offs (which may be slow) or get a Real Database.
Mark Canlas
+2  A: 

On Linux machines go to /etc/security/limits.conf and add or modify these lines

* soft nofile 10003
* hard nofile 10003

This will increase the number of files each process can have open to 10003 (remember that you always start with three open: stdin, stdout, and stderr).

Based on you comments it sounds like you are using a Win32 machine. I can't find a way to increase the number of open files per process, but you might, and I stress might, be able to handle this through fork'ing (which is really threading on Win32).

Chas. Owens
Is there an equivalent for Windows (that is what I'm working on)?
Jack L.
It appears that while the NT/Win32 allows for more open file handles, the C runtime has a hard limit at 2048 which cannot be extended.
ephemient