Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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/’; $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/'; $| = 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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2016-10-23 01:58 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (299 votes). Check out past polls.