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 (09:15)
From: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] Closing all open file descriptors
Zitat von Markus E L <>:

> Oliver Bandel wrote:
> > Zitat von Dave Benjamin <>:
> >
> >> On Fri, 14 Sep 2007, Mattias EngdegÄrd wrote:
> >>
> >> >> 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.
> >> >
> >> > I think platform-dependent code is required here. The common way of
> >> > doing this under Linux (and Solaris, probably) is to readdir
> >> > /proc/PID/fd/. Windows is of course very different.
> >>
> >> I like this approach. It doesn't eliminate the need to convert ints to
> >> descrs,
> >
> > On Unix, file-descriptors are identified by integer-values.
> >
> > In OCaml, you have abstract types for file-descriptors.
> > So, how to convert them? I think, officially there is no way.
> By writing a C-function which does the conversion. This, of course
> could only exist under Linux.

I think this would need to hack in the internals of OCaml.
And I don't see that it really would be necessary.
But to have the number of max_open_files IMHO would be fine.
I mentioned that it might be called Sys.max_open_descr or similarly.

> Generally a function enumerate_my_open_files would not be such a bad
> idea.

You can gather together the files (names or descriptors)
you have opened, if you want. After the work is done,
close the channels/files.

If you think you need all this for your daemon,
I ask: why do you think you need it? If you write it, you have
full control over all files you open, or if you are using
sockets, there also should be no problem.

Why do you want to have numbers for your opened files?
You can have a filename and get a file_descr or a channel.
You can use a Map, if you need to enumerate them.
But you also could use a Map that has filenames as keys
and file_descr or channels as value of the map.
So, if you want to close one certain channel, you
have your association.
You also could usa a PID of a child as key, or both:
a PID and a filename, or connection-ID or something like that.

Instead of looking for libraries for all kind of possible
applications, IMHO it's better to build the needed things
by your own, using the already available libraries like Map,
Set and so on.
Or if you need FIFOs for the connections, there also is a module for that

Rethink your design and you possibly will not have a necessity
for the functions you asked for.

If you would say a littlebid more about the tasks of your
daemon, one could give a more detailed answer.