[Spread-users] Question about thread-safety
ben at algroup.co.uk
Mon Jun 16 08:59:07 EDT 2003
James Rauser wrote:
> John Schultz wrote:
>>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!
> I can vouch for the fact that this is a problem; in my context it was
> occuring in a server which forked child processes to handle requests.
> The children each called close() on the inherited spread mailbox,
> then reestablished their own connection with SP_connect(). The children
> can't call SP_disconnect(), because we don't really want to disconnect
> it. But: the child's copy of spread's internal session table wasn't
> updated, so if the new SP_connect() call obtained the same numerical
> FD, the library got confused.
> I solved it by dup()ing the inherited FD onto an open file descriptor
> for /dev/null and just leaving it open. Instead of:
> I use:
> int tempfd = ::open("/dev/null", O_RDONLY);
> dup2(tempfd, mSpreadMbox); // now mSpreadMbox is a handle to /dev/null
> which wastes one open FD per child process. 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.
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.
"There is no limit to what a man can do or how far he can go if he
doesn't mind who gets the credit." - Robert Woodruff
More information about the Spread-users