Understanding the Concept of File Descriptors in the Linux Operating System

In 7th Edition Unix, the file descriptor is
an index to a table in the user structure which is simply an array of pointers into the [system global] open file table
. So the file descriptor itself is really an indirect reference, a ‘pointer to a pointer’ of sorts.


Solution 1:

In traditional Unix, the term “file” refers to entries in the file table, which are also known as open files. Although the term “file” is generic, it is used specifically in the context of the file table.

The metadata portion of a file is technically known as an inode, with files on disk commonly referred to as inodes. The relationship between an inode and its data blocks is typically one-to-one, meaning that referring to an inode implicitly implies the data it points to. While more modern filesystems may have features like copy-on-write that allow for shared data blocks, this is not universally applicable and is specific to traditional Unix. However, considering the term “filesystem,” it is reasonable to think of its contents as files.

When a file on disk is opened, in-core inodes are read into memory and stored in the inode table. However, this is one level of indirection beyond your inquiry.

The conflict in terminology arises between files stored on disk (referred to by an inode) and open files (referred to by entries in the file table).

Both “open file” and “file table entry” adequately describe what you are trying to explain.

I came across a fairly concise reference at: http://www.hicom.net/~shchuang/Unix/unix4.html. References in the format (Bach nn) pertain to the book titled The design of the unix operating system, authored by Maurice J. Bach.


Solution 2:


In 7th Edition Unix, the file descriptor serves as an indirect reference to an array of pointers in the user structure, which in turn points to the [system global] open file table. Therefore, the file descriptor can be considered as a ‘pointer to a pointer’. Additionally, it is used to index an array of one-byte flag fields, where the close-on-exec flag was the only flag included at that time.

struct user {
    ...
    struct file *u_ofile[NOFILE]; /* pointers to file structures of open files */
    char u_pofile[NOFILE]; /* per-process flags of open files */
}

Within the open file table, there is information about the file’s read/write mode, the inode pointer, the position, and potentially numerous other details on modern systems. Regarding the file descriptor table, I am uncertain if it holds any additional information on contemporary systems.

See:

  • Access the ‘u_ofile’ file in the V7/usr/sys/h/user.h directory by visiting the following link: http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/h/user.h.
  • The file.h can be found at the following link: http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/h/file.h.
  • Access the file “V7/usr/sys/sys/fio.c” through the following URL: http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/sys/fio.c (getf)

Frequently Asked Questions