amuck-landowner

File Descriptors

splitice

Just a little bit crazy...
Verified Provider
Currently when working with file handles / descriptors I have observed the following behaviour.

  1. I open a file with fopen()
  2. the File Descriptor is 5
  3. I close the file with fclose()
  4. I open a new file with fopen()
  5. I receive a File Descriptor of 5
Does anyone know if this is specified anywhere? Caveats? Can I reasonably expect this behaviour on Linux distributions within a single threaded, single process application?

Just thinking this could greatly simplify some code I have to write :p
 
Check out the POSIX specification for open(): http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html.

 


The open() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process.

You say your application is single-threaded, single-process, but remember to take in consideration that a different application may open a file descriptor between your steps #4 and #5, and hence your application would get a different file descriptor in step #5, so you cannot rely on it being always the same.
 

splitice

Just a little bit crazy...
Verified Provider
Thanks, thats what I was after. I was too preoccupied searching Linux I didn't consider the precursor specification.
 
Top
amuck-landowner