Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by jasonk (Parson)
on Jul 19, 2005 at 14:04 UTC ( #476099=note: print w/ replies, xml ) Need Help??


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

In addition to the method you've tried here, the FAQ also provides documentation on closing a filehandle using the syscall directly.

# from perlfaq5 require ’sys/syscall.ph’; $rc = syscall(&SYS_close, $fd + 0); # must force numeric die "can’t sysclose $fd: $!" unless $rc == -1;

The method you tried didn't work for me either (which makes me wonder why it's in the FAQ), but this one did. Here is the test script I used:

#!/usr/bin/perl -w require 'sys/syscall.ph'; $| = 1; open(OUT, ">foo"); &CloseAllOpenFiles(); print "done with close.\n"; sleep 20; # go look at process with lsof to see if fd still open sub CloseAllOpenFiles { for my $fd (0 .. 1024) { my $rc = syscall(&SYS_close, $fd); warn "Can't sysclose $fd: $!" unless $rc == -1; } }

We're not surrounded, we're in a target-rich environment!


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 14:16 UTC
    Ah, the syscall approach seems a bit less overhead, and seems to work well. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-07-23 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (154 votes), past polls