map / systems / file-descriptors

Systems · principle 4 of 7

File descriptors

Files, sockets, and pipes are all just numbered handles in a per-process table — and that table has a limit.

01 · Why it’s true

Unix exposes files, network sockets, pipes, and devices through one interface: a small integer called a file descriptor, an index into the process’s table of open resources. By convention 0, 1, and 2 are standard input, output, and error.

Every open file or connection holds a descriptor until it’s closed, and each process has a limit (see ulimit -n). A program that leaks descriptors, or a server holding many connections, eventually fails with “too many open files” — even with plenty of CPU and memory to spare.

02 · Picture it

1fd 0stdin2fd 1stdout3fd 2stderr4fd 3socket → database 10.0.0.5:54325fd 4file → /var/log/app.log6…up to the limit (ulimit -n), then “too many open files”Files, sockets and pipes share one numbering. Every unclosed one holds a slot until theprocess hits its limit.
1fd 0stdin2fd 1stdout3fd 2stderr4fd 3socket → database 10.0.0.5:54325fd 4file → /var/log/app.log6…up to the limit (ulimit -n), then“too many open files”Files, sockets and pipes share onenumbering. Every unclosed one holds aslot until the process hits its limit.
One table per process for every open file, socket and pipe.

03 · In the interview

  • Diagnosing “too many open files”.
  • Inspecting open descriptors with ls /proc/<pid>/fd or lsof -p <pid>.
  • Sizing limits for servers that hold many connections.

04 · Practice question

“A service starts failing with “too many open files” after a few days of uptime. What do you do?”

A strong answer covers

  1. Count descriptors over time (ls /proc/<pid>/fd | wc -l); steady growth means a leak.
  2. Use lsof -p <pid> to see what they are — lots of sockets to one host, files, pipes.
  3. Common causes: connections or files not closed on error paths, or HTTP response bodies never closed.
  4. Short term: raise the limit or restart. Long term: fix the leak and alert on descriptor usage.