In operating system design, file descriptors are resources that users may either read bytes from or write bytes to. These may include actual files.
By convention:
0
: standard input (read)1
: standard output (write)2
: standard error (write)
Programming
On POSIX systems, there are several syscalls and utilities we can use to interact with file descriptors.
int dup(int oldfd);
— allocates a new file descriptor that refers to the same file descriptor asoldfd
. The new FD number is the lowest available number.int dup2(int oldfd, int newfd);
— atomically changes the FDnewfd
to point tooldfd
. This closesnewfd
, and allocates its same FD number to point tonewfd
. This is done atomically to prevent any race conditions associated with doing this process manually, i.e., callingclose()
, thendup()
.