It would appear that the issue is you are opening them all before closing any of them. If you want file descriptors to stay under your ulimit, you really want to close some of them before opening others.

If you're counting on the filehandles to close from going out of scope, there's some news you might not want. They won't because you have put them in an array. Try reusing the same lexical scalar over and over instead for the file handle if you want that, or manually close your file handles.

You can read data from the files, close the files, and still keep the data around. There's no need for all those files to be open at once.

As examples, the first of these bombs out for too many open file handles, while the second and third will just keep running:

my $data, $i, @file; while ( 1 ) { open $file[ $i ], '<', '/dev/zero' or die "Cannot read: $!\n"; read $file[ $i ], $data, 4; $i++; print "$data\t$i iterations...\n" unless $i % 100; }
my $data, $i; while ( 1 ) { open my $file, '<', '/dev/zero' or die "Cannot read: $!\n"; read $file, $data, 4; close $file; $i++; print "$data\t$i iterations...\n" unless $i % 100; }
my $data, $i, @file; while ( 1 ) { open $file[ $i ], '<', '/dev/zero' or die "Cannot read: $!\n"; read $file[ $i ], $data, 4; close $file[ $i ]; $i++; print "$data\t$i iterations...\n" unless $i % 100; }

In reply to Re: old file descriptors not being cleaned up by mr_mischief
in thread old file descriptors not being cleaned up by wagnerc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.