Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

open file descriptors

by jeremyh (Beadle)
on Dec 13, 2003 at 15:52 UTC ( #314531=perlquestion: print w/replies, xml ) Need Help??
jeremyh has asked for the wisdom of the Perl Monks concerning the following question:

How can a script access it's open file descriptors? (what (perl?) data structure are they found in? On linux you can see them in /proc, but need to see in perl since porting to VMS. My script is invoked by a fork/exec from a calling process which has opened a read pipe and a write pipe for me - I need to get the file descriptors for the pipes. (on vms they will be mailboxes)

Replies are listed 'Best First'.
•Re: open file descriptors
by merlyn (Sage) on Dec 13, 2003 at 16:13 UTC
    This'll give you a list of filehandle objects open for read on the current file descriptors that are also open:
    use FileHandle; my @handles = map FileHandle->new_from_fd($_, "r"), 0..100;
    Handles that aren't open come back as undef in this array. So $handles[0] is STDIN, etc.

    If you don't need the mapping from specific fd, then add grep defined($_), in front of the map.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Well, that has the builtin assumption that the max fileno is 100, which is probably good enough, usually, but it will lead to a really hard to debug problem, eventually. It's actually somewhat of a weakness of perl, I think, that there is no exposed system call to get all open file descriptors. Obvoiously you can deal with this if you compile perl properly, and just refer to the system call by number through syscall()... but still, this is sad.

      This is additionally sad because of the behavior of perl's output buffers on fork() (leaving buffers unflushed), since the buffers are duped with the fork, but there is no means by which to systematically flush all buffers before fork(). However, as I understand it, the behavior on fork() is fixed in later versions of perl (I'm still on 5.005_03... but we're working on an upgrade plan to 5.8 soon)... so that's good at least.

      Not an editor command: Wq
        Having something, a system call, that could count all open file handles would be in the same class of functions like flock: only compatable to a point. I've never heard of a function in unix to do that, but I know that procfs hints you in on what's going on w/ a process.

        Play that funky music white boy..
      A very belated thanks to merlyn!

      This code has helped my in a fairly large project.
      This was my first post and didn't really know how the site worked at the time, so didn't mention how much this helped me out.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://314531]
Approved by valdez
Front-paged by grinder
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2018-01-24 04:01 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (255 votes). Check out past polls.