Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by jasonk (Parson)
on Jul 19, 2005 at 14:04 UTC ( [id://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!

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476099]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found