Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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-15 (16:12)
From: Markus E L <ls-ocaml-2006@m...>
Subject: Re: [Caml-list] Closing all open file descriptors

Eric Cooper wrote:

> I think the right interface for this should be a "fold" over open file
> descriptors:
>     open Unix
>     let descr_of_int n = (Obj.magic (n : int) : file_descr)
>     let valid_descr fd =
>       try ignore (fstat fd); true
>       with Unix_error (EBADF, "fstat", _) -> false
>     let fold_open_fds f init =
>       let f' x s =
> 	let fd = descr_of_int (int_of_string s) in
> 	if valid_descr fd then f x fd else x
>       in
>       Array.fold_left f' init (Sys.readdir "/proc/self/fd")
> Note that the readdir itself opens a file descriptor, which is
> included in the result but then closed. So there's a little extra
> complexity to filter it out so that the caller's function doesn't get
> applied to a bad descriptor.

Yes, that's a good idea. This can be used to retrieve a list where
needed or to operate on the descriptors immediately. On systems which
absolutely don't have a (system dependend) method to pull the
filedescriptors from the system, one could just iterate over a
predefined range. So this interface could be implemented on a larger
range of systems with varying efficiency.

Now the price question: Does anyone here have some insights how one
gets at the open descriptors under windows? :-)

Regards -- Markus