Version française
Home     About     Download     Resources     Contact us    
Browse thread
Closing all open file descriptors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Allsopp <dra-news@m...>
Subject: RE: [Caml-list] Closing all open file descriptors
> I'm writing a daemon and I would like to be able to close all open file 
> descriptors. As far as I can tell, there is no way to determine the 
> maximum number of file descriptors available, and neither is there a way 
> to get a file descriptor from an integer. The best I've come up with is 
> the following:
>
>    for fd = 0 to 1024 do
>      try Unix.close (Obj.magic fd : Unix.file_descr)
>      with Unix.Unix_error _ -> ()
>    done;
>
> Is there a better way?

Probably irrelevant here, but this approach wouldn't work under Windows
(Unix.file_descr is the Win32 file handle at that point which is often
larger than 1024). More relevantly, Unix can be reconfigured to allow for
more than 1024 open files.

Wouldn't an easier, portable way be to wrap Unix.openfile and Unix.close to
maintain a set of open file_descrs or is your concern the risk of files left
open by third party code?


David