Everything is a file: descriptors and sockets
Files, pipes and network connections all come back from the kernel as small numbers like 3. Here is what those numbers are, the per-process table behind them, and why the same read and write work on all of them.
01 The number open() gives back
Open a file in almost any language and somewhere underneath, the kernel hands back a small integer. In Python you can see it directly:
Why 3, and not 0 or some address? And why do a file, a pipe and a network connection all come back as the same kind of number? That number is a file descriptor, and it is the one handle a Unix process uses for nearly everything it reads or writes. This page covers Linux; macOS and the BSDs work the same way.
02 A number is a slot in a table
The kernel keeps a small descriptor table for every process. A descriptor is just an index into it. Each slot points at a kernel record of an open file (Linux calls it a struct file), which holds the current offset and whether you opened it for reading or writing. That record in turn points at the real object: a file on disk, a pipe buffer, a socket, a terminal.
So there are three layers, and the program sees only the first:
| Layer | Belongs to | Holds |
|---|---|---|
| Descriptor (`3`) | One process | A slot number in that process’s table |
| Open file record | The kernel | Offset, read/write mode, a pointer to the object |
| Object | The kernel | The file’s inode, a pipe buffer, a socket, a terminal |
Every process starts with 0, 1 and 2 already filled in by whoever started it, usually the shell. New descriptors always take the lowest free slot, which is why your first open() returns 3.
03 What open, pipe, socket and close do
Here is one process making five calls. Watch the table, not the files.
- just handed out
- just closed
- free slot
The program has just started. The kernel gave it three descriptors already: 0 (stdin), 1 (stdout) and 2 (stderr), all pointing at the terminal. Slots 3 onwards are free.
The calls differ in what they create, but each one ends the same way: a pointer stored in a free slot, and the slot’s number returned. After that, read(fd, …), write(fd, …) and close(fd) work on any of them. That shared interface is what people mean by “everything is a file”.
04 See it yourself in /proc
On Linux, /proc/<pid>/fd lists a process’s descriptors as links to what they point at. This script makes the same calls as the trace and then lists its own table. It ran with Python 3.12 inside a python:3.12-slim container:
Descriptors 4 and 5 show the same pipe:[…] number because they are two ends of one pipe. Here 0, 1 and 2 are not the terminal: the container ran without one, so Docker connected stdin to /dev/null and stdout and stderr to pipes. In an interactive shell they would show /dev/pts/0. On macOS, which has no /proc, lsof -p <pid> gives the same list.
06 Sockets: one descriptor per connection
A server has one listening socket. Each accept() returns a brand-new descriptor for that one client, so a server with 5,000 open connections holds over 5,000 descriptors. Each process has a limit on how many it may hold, shown by ulimit -n. It is often 1024 on a desktop Linux shell; containers and servers frequently raise it much higher.
Hit the limit and every call that makes a descriptor fails with EMFILE. This script opens a file 100 times without closing it, under a limit of 16:
Errno 24 is EMFILE. On a busy server the same error shows up as accept() failing, so new clients are refused while old ones keep working.
07 When it bites
Too many open filesLeaked descriptorsFiles or connections opened in a loop and never closed pile up until the limit is hit. Use with in Python, try-with-resources in Java, and watch the count in /proc/<pid>/fd.read() never returns 0A pipe that never endsA reader sees end-of-file only when every write end is closed. If the parent forks a child and forgets to close its own copy of the write end, the child waits for ever.rm big.logDeleted, but the disk is still fullRemoving a file removes its name. While any process holds a descriptor to it, the data stays on disk. lsof +L1 lists such files; restart or signal the process to let go.O_CLOEXECDescriptors leak into child programsDescriptors survive exec() unless marked close-on-exec, so a launched program can inherit your sockets. Python and most modern runtimes set the flag by default; plain C open() does not.close(fd); … write(fd)Writing to a reused numberAfter close, the number goes back to the pool and the next open may get it. A stale write then lands in the wrong file or socket, with no error.A descriptor is only meaningful inside the process that owns it. Sending the number 5 to another process gives it nothing; passing a real descriptor needs fork(), or a Unix socket message with SCM_RIGHTS.