[Spread-users] Question about thread-safety

John Schultz jschultz at commedia.cnds.jhu.edu
Mon Jun 16 17:38:01 EDT 2003

>>NOTE: if anyone knows how to instruct Unix/Linux systems not to reuse
>>file descriptor IDs in a process, could you please email me or the list
>>a good reference (with page #)? Thanks!

> It is part of the guaranteed semantics of open() that it returns the
> lowest available file descriptor, so there's definitely no way to do
> this unless you simply keep it open.

Doesn't this cause a general race condition in multi-threaded Unix/Linux
applications that open and close file descriptors in multiple threads?

The race condition: a thread is about to operate on file descriptor x, but
just before it does another thread(s) close x and create a new file
descriptor which is assigned the same value as x.

To avoid this race condition wouldn't all calls that create and/or close
file descriptors have to be done in a single thread that ensures no other
threads are operating on any file descriptors it is about to close? If so,
that seems like a BAD thing to me.

> I don't think there's any standard way to force the kernel to never
> re-use a file descriptor without leaving it open as e.g. above.  Even if
> there was, a long-running, multithreaded process would eventually run
> out of file descriptors.

I disagree on practical grounds.  You could use a 64b counter that is only
incremented to assign file descriptor IDs -- if you flip that bad boy,
then exit.  Of course, because we are talking about file descriptors, by
then homo sapiens probably would have already evolved into something else.

John Schultz
Co-Founder, Lead Engineer
D-Fusion, Inc. (http://www.d-fusion.net)
Phn: 443 838 2200

More information about the Spread-users mailing list