A file descriptor is a small number (0 or higher) that a process uses to refer to an open resource. The kernel keeps a table for each process. The table maps each number to an open file description. This tracks things like the current read position (offset) and the flags.
By convention:
- 0 is stdin (standard input)
- 1 is stdout (standard output)
- 2 is stderr (standard error)
open() returns the lowest number that is not in use.
"Everything is a file" means that very different resources use the same descriptor calls. These resources all support read, write and close:
- regular files and directories
- pipes and terminals
- devices
- network sockets
For example, each TCP connection in your Node server is a descriptor that libuv watches.
To see a process's descriptors, run ls -l /proc/<pid>/fd or lsof -p <pid>. Each process has a limit on how many descriptors it can have open at once. ulimit -n shows this limit.
- A file descriptor is a small number that points into the process's table of open files.
- 0, 1 and 2 are stdin, stdout and stderr.
- Files, pipes, devices and sockets all share the same read, write and close calls.
- You can inspect a process's descriptors with /proc/<pid>/fd or lsof.
- Each process has a limit on open descriptors, shown by ulimit -n.
Read more