Skip to main content

File Descriptor

A file descriptor is a small non-negative integer the kernel gives a process to refer to an open file, socket, pipe, or device. Descriptors 0, 1, and 2 are stdin, stdout, and stderr.

Understanding File Descriptors

What Is a File Descriptor in Linux?

When a program opens a file, the kernel returns a number, not the file itself. That number is the file descriptor (FD), and the program passes it to read(), write(), and close(). Files, network sockets, pipes, and devices all use the same mechanism, which is why one set of system calls works on all of them.

How the File Descriptor Table Works

Each process has its own table mapping FD numbers to open resources.

TEXT
FD Points to
0 stdin (keyboard or pipe)
1 stdout (terminal or pipe)
2 stderr (terminal)
3 /var/log/app.log
4 socket: TCP connection to database
5 /etc/config.yaml

Redirection simply repoints these numbers. command > out.txt repoints FD 1 at a file, and 2>&1 makes FD 2 point wherever FD 1 points at that moment.

How to Inspect File Descriptors

Bash
ls -la /proc/$$/fd/ # FDs of the current shell
lsof -p 1234 # open files of a process
lsof /var/log/nginx/access.log # who has this file open
lsof +L1 # files deleted but still open
ulimit -n # per-process FD limit for this shell
ls /proc/1234/fd | wc -l # FDs in use by a process
grep 'open files' /proc/1234/limits

How to Fix "Too Many Open Files"

This error (EMFILE) means a process hit its FD limit. Every network connection costs one FD, so a busy server needs far more than the common default of 1024.

INI
# systemd service unit
[Service]
LimitNOFILE=65536
Bash
# nginx: add to nginx.conf in the main context
# worker_rlimit_nofile 65536;
# Verify the running process picked it up
grep 'open files' /proc/$(pidof -s nginx)/limits

Troubleshooting File Descriptors

Symptom Command What to Look For
Too many open files lsof -p PID | wc -l Count against limit
Disk full after delete lsof +L1 Process holding file open
Cannot unmount lsof /mountpoint Open files on mount
FD count keeps growing ls /proc/PID/fd | wc -l Leak over time
Tip

If you delete a large log file and df shows no space freed, a process still holds it open. The space returns only when every FD for it closes. Restart the process, or truncate it with : > file before deleting next time.

Common Mistake

Raising ulimit -n in your shell and expecting a systemd service to benefit. Services use LimitNOFILE= from their unit file instead.

Remember

FD exhaustion is a classic production failure for busy services. Set the limit deliberately and monitor FD counts.

Frequently Asked Questions

Why do file descriptors matter for debugging "too many open files"?

The limit covers sockets, pipes, and files together, so a service with many connections or an unclosed-handle leak eventually fails to accept connections or open files. Compare `ls /proc/PID/fd | wc -l` with the process limit to confirm.

What is the difference between stdout and stderr?

They are separate streams (FD 1 and FD 2). Keeping them separate lets you save normal output to a file while errors still show on screen, or capture each independently.

What causes file descriptor leaks in code?

Opening a file or socket and not closing it on an error path. Constructs that guarantee cleanup, such as Python's `with`, Java's try-with-resources, or Go's `defer`, prevent most leaks.

Found a mistake, an outdated step or a broken link?