Saturday, September 29, 2012

File descriptior vs. file handle in Unix/Linux

File descriptor wiki provides a good explanation about the difference between a file descriptor and a file handle in UNIX/LINUX.
  • Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly.

    In Unix-like systems, file descriptors can refer to files, directories, block or character devices (also called "special files"), sockets, FIFOs (also called named pipes), or unnamed pipes.

    Here is a good summary to manipulate max number of open files/file descriptors.
  • The FILE * file handle in the C standard I/O library routines is technically a pointer to a data structure managed by those library routines; one of those structures usually includes an actual low level file descriptor for the object in question on Unix-like systems. Since file handle refers to this additional layer, it is not interchangeable with file descriptor.
To further complicate terminology, Microsoft Windows also uses the term file handle to refer to the more low-level construct, akin to POSIX's file descriptors. Microsoft's C libraries also provide compatibility functions which "wrap" these native handles to support the POSIX-like convention of integer file descriptors as detailed above.


Anonymous said...

What is exactly is meant by "abstract key" in the phrase "The user application passes the abstract key to the kernel through a system call," ? I am by abstract key you are referring to the File Descriptors number? Thanks in advance.

Anonymous said...

I thought that too. Either it's a meaningless filler word or they're deliberately fucking with us. In which case, hey writer fuck YOU.