English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2007-09-14 (10:09)
From: Gerd Stolpmann <info@g...>
Subject: Re: [Caml-list] Closing all open file descriptors
Am Donnerstag, den 13.09.2007, 17:56 -0500 schrieb Dave Benjamin:
> 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?

Not really. Unix wants it this way.

ocamlnet's netsys library has functions to deal with that:
Netsys.file_descr_of_int and Netsys.sysconf_open_max. Look here for the


This does not work under Win32, of course, but I bet this wasn't part of
your question. The Netsys functions fail in this case.

Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714