LINUX / A CONCEPT NOTE

File Descriptors

why 'too many open files' happens

~70 sec read

Overview · mechanism
pitfall · examples

01 / THE SHORT VERSION

The idea in a few sentences.

In Linux, everything is a file. The kernel indexes open files, directories, network sockets, and pipes using integer handles called File Descriptors (FDs). Each process has a maximum limit of open FDs.

02 / FOLLOW THE MECHANISM

How a file descriptor is handled

  1. Open system call

    application opens a log file or accepts a network socket connection.

  2. Allocation

    kernel allocates a free file descriptor integer (e.g. 3) to the process.

  3. I/O

    process reads/writes data referencing this integer handle.

  4. Limits check

    if the process exceeds maximum ulimit boundaries, it throws a 'Too many open files' exception.

04 / COMMAND NOTES

Read the command, then the result.

Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.

EXAMPLE 01 · REFERENCE

check the maximum open file descriptor limit for your process

ulimit -n

EXAMPLE 02 · REFERENCE

count open file descriptors for a specific process

lsof -p PID | wc -l

Explore command anatomy in the CLI lab