ArticleslgStudy

science

File descriptor

File descriptor is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand File descriptor rather than just read about it. In short: In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically have non-negative integer values, with negative values being reserved to indicate "no value" or error conditions.

File descriptor — main illustration
File descriptor — illustration

Key takeaways

  • File descriptor belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect File descriptor to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of File descriptor from memory before moving on to harder problems.

Reference excerpt

In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically have non-negative integer values, with negative values being reserved to indicate "no value" or error conditions. File descriptors are a part of the POSIX API. Each Unix process (except perhaps daemons) should have three standard POSIX file descriptors, corresponding to the three standard streams:

Overview

In the traditional implementation of Unix, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table. This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes. It also indexes into a third table called the inode table that describes the actual underlying files. To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process. The process does not have direct access to the file or inode tables. On Linux, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the process identifier. File descriptor /proc/PID/fd/0 is stdin, /proc/PID/fd/1 is stdout, and /proc/PID/fd/2 is stderr. As a shortcut to these, any running process can also access its own file descriptors through the folders /proc/self/fd and /dev/fd. In Unix-like systems, file descriptors can refer to any Unix file type named in a file system. As well as regular files, this includes directories, block and character devices (also called "special files"), Unix domain sockets, and named pipes. File descriptors can also refer to other objects that do not normally exist in the file system, such as anonymous pipes and network sockets. The FILE data structure in the C standard I/O library usually includes a low level file descriptor for the object in question on Unix-like systems. The overall data structure provides additional abstraction and is instead known as a file handle.

Operations on file descriptors The following lists typical operations on file descriptors on modern Unix-like systems. Most of these functions are declared in the <unistd.h> header, but some are in the <fcntl.h> header instead.

Creating file descriptors open() creat() socket() accept() socketpair() pipe() epoll_create() (Linux) signalfd() (Linux) eventfd() (Linux) timerfd_create() (Linux) memfd_create() (Linux) userfaultfd() (Linux) fanotify_init() (Linux) inotify_init() (Linux) clone() (with flag CLONE_PIDFD, Linux) pidfd_open() (Linux) open_by_handle_at() (Linux) kqueue() (BSD) pdfork() (kFreeBSD)

Deriving file descriptors dirfd() fileno()

Operations on a single file descriptor read(), write() readv(), writev() pread(), pwrite() recv(), send() recvfrom(), sendto() recvmsg(), sendmsg() (also used for sending FDs to other processes over a Unix domain socket) recvmmsg(), sendmmsg() lseek(), llseek() fstat() fstatvfs() fchmod() fchown() ftruncate() fsync() fdatasync() fdopendir() fgetxattr(), fsetxattr() (Linux) flistxattr(), fremovexattr() (Linux) statx (Linux) setns (Linux) vmsplice() (Linux) pidfd_send_signal() (Linux) pdkill() (kFreeBSD) waitid() (with P_PIDFD ID type, Linux) fdopen() (stdio function:converts file descriptor to FILE*) dprintf() (stdio function: prints to file descriptor)

Operations on multiple file descriptors select(), pselect() poll(), ppoll() epoll_wait(), epoll_pwait(), epoll_pwait2() (Linux, takes a single epoll filedescriptor to wait on many other file descriptors) epoll_ctl() (for Linux) kqueue() (for BSD-based systems). sendfile() splice(), tee() (for Linux) copy_file_range() (for Linux)

Operations on the file descriptor table The fcntl() function is used to perform various operations on a file descriptor, depending on the command argument passed to it. There are commands to get and set attributes associated with a file descriptor, including F_GETFD, F_SETFD, F_GETFL and F_SETFL.

close() closefrom() (BSD and Solaris only; deletes all file descriptors greater than or equal to specified number) close_range() (for Linux) dup() (duplicates an existing file descriptor guaranteeing to be the lowest number available file descriptor) dup2(), dup3() (Close fd1 if necessary, and make file descriptor fd1 point to the open file of fd2) fcntl (F_DUPFD)

Operations that modify process state fchdir() (sets the process's current working directory based on a directory file descriptor) mmap() (maps ranges of a file into the process's address space)

File locking flock() fcntl() (F_GETLK, F_SETLK and F_SETLKW) lockf()

Sockets

connect() bind() listen() accept() (creates a new file descriptor for an incoming connection) getsockname() getpeername() getsockopt() setsockopt() shutdown() (shuts down one or both halves of a full duplex connection)

Miscellaneous ioctl() (a large collection of miscellaneous operations on a single file descriptor, often associated with a device)

at suffix operations A series of new operations has been added to many modern Unix-like systems, as well as numerous C libraries, to be standardized in a future version of POSIX. The at suffix signifies that the function takes an additional first argument supplying a file descriptor from which relative paths are resolved, the forms lacking the at suffix thus becoming equivalent to passing a file descriptor corresponding to the current working directory. The purpose of these new operations is to defend against a certain class of TOCTOU attacks.

openat() faccessat() fchmodat() fchownat() fstatat() futimesat() linkat() mkdirat() mknodat() readlinkat() renameat() symlinkat() unlinkat() mkfifoat() fdopendir()

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with File descriptor

Start with the simplest possible case. Write down what File descriptor claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to File descriptor before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about File descriptor ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of File descriptor

In research
File descriptor appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses File descriptor in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
File descriptor is common in secondary-school and first-year university syllabi. It links to neighbouring topics POSIX, Unix file system technology, so understanding it makes those chapters shorter.
In everyday life
Look for File descriptor outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “File descriptor” →

How to study File descriptor in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what File descriptor means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain File descriptor out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is File descriptor in simple terms?

In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically have non-negative integer values, with negative values bei…

Why does File descriptor matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study File descriptor?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on File descriptor.

Tags

  • POSIX
  • Unix file system technology

Keep exploring