What is a file descriptor in Linux and how does it relate to open files?
Learn what a Linux file descriptor is, how the kernel maps open files to integers like 0, 1, 2, and how redirection and dup() rely on them.
Expected Interview Answer
A file descriptor is a small non-negative integer that the kernel gives a process to represent an open file or I/O resource; the process uses that number for every read, write, or close on the resource.
When a process opens a file, socket, or pipe, the kernel creates an entry in the process's file-descriptor table and returns the lowest available integer index into it. Descriptors 0, 1, and 2 are reserved for standard input, standard output, and standard error. The integer is just a handle — the real state (offset, mode, and a pointer to the underlying open file description) lives in kernel tables, which is what makes redirection, piping, and dup() possible.
- Gives processes a simple integer handle to complex I/O resources
- Enables redirection and piping of stdin, stdout, and stderr
- Uniform interface for files, sockets, and pipes
- Allows sharing and duplication via dup() and fork()
- Lets the kernel track offsets and access modes safely
AI Mentor Explanation
Think of a cricket team's coat-check where each player hands in their full kit bag and gets a numbered token. From then on nobody describes the bag — they just show token 7 to retrieve it. A file descriptor is that token: the process opens a file, the kernel stashes all the real details, and hands back a small number the process flashes for every read or write.
Step-by-Step Explanation
Step 1
Open the resource
A call like open(), socket(), or pipe() asks the kernel for access to a file or I/O endpoint.
Step 2
Kernel allocates a slot
The kernel finds the lowest free index in the process's descriptor table and records the open file details there.
Step 3
Return the integer
The syscall returns that index — the file descriptor — to the process, or -1 on error.
Step 4
Use the descriptor
read(fd, ...), write(fd, ...), and lseek(fd, ...) all reference the resource by its integer.
Step 5
Close it
close(fd) frees the slot so the integer can be reused for the next open resource.
What Interviewer Expects
- That a descriptor is a small non-negative integer index
- Knowledge of fd 0, 1, 2 as stdin, stdout, stderr
- Understanding that the kernel holds the real open-file state
- Awareness that sockets and pipes also use descriptors
- How dup()/redirection reuse and remap descriptors
Common Mistakes
- Thinking the descriptor is the file itself rather than a handle
- Forgetting that 0, 1, 2 are reserved for standard streams
- Believing only regular files have descriptors (sockets/pipes do too)
- Not closing descriptors, causing leaks and 'too many open files'
- Confusing the per-process fd table with the system-wide open file table
Best Answer (HR Friendly)
“A file descriptor is a small number the system gives a program when it opens a file or connection. The program uses that number to read, write, or close the resource, so it never has to track the messy internal details itself.”
Code Example
#include <fcntl.h>
#include <unistd.h>
int main(void) {
int fd = open("data.txt", O_RDONLY); // returns a small integer, e.g. 3
if (fd == -1) return 1; // -1 signals an error
char buf[64];
read(fd, buf, sizeof(buf)); // read via the descriptor
close(fd); // release the descriptor
return 0;
}ls -l /proc/$$/fd # 0 -> stdin, 1 -> stdout, 2 -> stderr, plus any open filesFollow-up Questions
- What are file descriptors 0, 1, and 2?
- How does shell redirection (>, <, 2>&1) work with descriptors?
- What does dup2() do and why is it useful?
- What is the difference between the fd table and the open file description?
- What causes a 'too many open files' error and how do you fix it?
MCQ Practice
1. A file descriptor in Linux is best described as a:
A descriptor is a small non-negative integer index into the process's file-descriptor table.
2. Which descriptor number is standard error?
0 is stdin, 1 is stdout, and 2 is stderr by convention.
3. When a new file is opened, which descriptor number is assigned?
The kernel returns the lowest unused descriptor index for the process.
Flash Cards
What is a file descriptor? — A small non-negative integer the kernel returns to represent an open file or I/O resource for a process.
What are fd 0, 1, 2? — Standard input (0), standard output (1), and standard error (2).
Where is the real open-file state stored? — In kernel tables; the descriptor is just an index into the process's fd table pointing to the open file description.
How is a descriptor released? — By calling close(fd), which frees the slot so the integer can be reused.