3 ms·
>This matters because posix guarantees that open()/pipe() etc. will return the lowest file descriptor not in use[1]. I.e. this should work: close(0); fd
by treis 5y ago
>This matters because posix guarantees that open()/pipe() etc. will return the lowest file descriptor not in use[1]. I.e. this should work:
close(0);
fd = open(“/foo/bar”, …);
// fd is guaranteed to be 0
On a multi threaded system that isn't guaranteed is it? Meaning, another thread could call open in-between your close & open.
- CyberRabbi 5y agoIt is guaranteed whether multi-threaded or not. It’s a process level guarantee. If your application is designed such that you don’t know what your other threads are doing then POSIX cannot help you.
- loeg 5y agoWhat you’re getting at is that an individual thread cannot really use this property without some form of synchronization with other threads in the process. Eg, to use this property, other threads either do not allocate fds, or you take some central lock around all fd allocations. Most well-written programs do not rely on it.