Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to close all open file descriptors after a fork?

by blazar (Canon)
on Jul 19, 2005 at 13:55 UTC ( #476094=note: print w/ replies, xml ) Need Help??


in reply to How to close all open file descriptors after a fork?

Maybe this is not what you want. But why don't you open all of your FDs as "lexical FHs" (recommended in any case, IMHO) and push them into an @array. You can later close $_ for @ARRAY if needed. (I prefer "lexical FHs" because they will automatically closed on scope exit1 - I'm aware that sockets require an explicit close, though.)

Update: incidentally

  • The &-form of sub call is now obsolete and most likely not to do what one expects in most situations unless one really knows what he is doing.
  • C-style for loops are legal, but are more error-prone and less reliable, not to say clear, than the corresponding perlish construncts.


1 Or more precisely when no more references exist.


Comment on Re: How to close all open file descriptors after a fork?
Select or Download Code
Re^2: How to close all open file descriptors after a fork?
by nneul (Novice) on Jul 19, 2005 at 13:59 UTC
    The problem is the "unknown context". This is in a subroutine that needs to spawn a background task, called from code that is not under my control. Therefore, I don't know what FDs/FHs are open.
      Well, in that case you may want to choose a way, to "spawn a background task" that will give you the pid of that task, and hopefully if you're under an OS that will let you access the relevant info for that process, you could check that.

      For example under Linux you will be able, and may want to examine "/proc/$pid/fd/".

Re^2: How to close all open file descriptors after a fork?
by bart (Canon) on Jul 19, 2005 at 14:38 UTC
    I prefer "lexical FHs" because they will automatically closed on scope exit
    No they won't. Your array will still hold a copy and they all will be kept open.

    These file handles are references, are they not? You could try weaken these references in the array, and they'll automatically go away as normal — leaving just an undef in place.

      No they won't. Your array will still hold a copy and they all will be kept open.
      I meant: "in a general situation". In this particular one I suggested to explicitly close the entries kept in the array. I also included a footnote mentioning the ref thing.
      These file handles are references, are they not? You could try weaken these references in the array, and they'll automatically go away as normal leaving just an undef in place.
      This is a very interesting suggestion, although I have no experience myself weakening references. I won't try because this is not my problem, but I will keep it in mind for when it may be of some use for me...
Re^2: How to close all open file descriptors after a fork?
by nneul (Novice) on Jul 19, 2005 at 14:56 UTC
    I've seen that mentioned before about the & style sub calls, old habits... Got any pointers to more details about that being obsolete?
      Yes, of course I should have mentioned it in my first reply (and I thought I did): perldoc -q perlsub.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://476094]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-10-21 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls