sajad torkamani

In a nutshell

A file descriptor (sometimes abbreviated to fd) is an integer that uniquely identifies an opened file or socket for each Unix process.

The integer is used as an index for a file descriptor table maintained by the kernel for each process. File descriptors are created via system calls such as open() or socket().

There are three standard file descriptors assigned to all UNIX processes:

File descriptorFile / resource
0Standard input (stdin)
1Standard output (stdout)
2Standard error (stderr)

File descriptors in action

List open files for specific process

lsof -p <PID>

Example output for an Nginx process on macOS:

COMMAND  PID  USER   FD   TYPE             DEVICE SIZE/OFF                NODE NAME
nginx   3703 sajad  cwd    DIR               1,18      992              318951 /opt/homebrew
nginx   3703 sajad  txt    REG               1,18  1235600             2583138 /opt/homebrew/Cellar/nginx/1.21.6_1/bin/nginx
nginx   3703 sajad  txt    REG               1,18  2160688 1152921500312781028 /usr/lib/dyld
nginx   3703 sajad  txt    REG               1,18   527776              648301 /opt/homebrew/Cellar/pcre2/10.39/lib/libpcre2-8.0.dylib
nginx   3703 sajad  txt    REG               1,18   506208             6990213 /opt/homebrew/Cellar/openssl@1.1/1.1.1n/lib/libssl.1.1.dylib
nginx   3703 sajad  txt    REG               1,18  2192928             6990211 /opt/homebrew/Cellar/openssl@1.1/1.1.1n/lib/libcrypto.1.1.dylib
nginx   3703 sajad    0r   CHR                3,2      0t0                 334 /dev/null
nginx   3703 sajad    1u   CHR                3,2      0t0                 334 /dev/null
nginx   3703 sajad    2w   REG               1,18   181333              656573 /opt/homebrew/var/log/nginx/error.log
nginx   3703 sajad    3w   REG               1,18  8603212             1422873 /opt/homebrew/var/log/nginx/access.log
nginx   3703 sajad    6u  IPv4 0xe6b198812ddbd207      0t0                 TCP *:http-alt (LISTEN)
nginx   3703 sajad    7u  IPv4 0xe6b198812ddbdc97      0t0                 TCP *:http (LISTEN)
nginx   3703 sajad    8w   REG               1,18   181333              656573 /opt/homebrew/var/log/nginx/error.log
nginx   3703 sajad    9u  unix 0xe6b198812dd31fff      0t0                     ->0xe6b198812dd3218f
nginx   3703 sajad   10u  unix 0xe6b198812dd3218f      0t0                     ->0xe6b198812dd31fff

In the output above, some file descriptors begin with a number followed by a character that describes the mode under which the file is open (e.g., r for read, w for write, and u for read and write).

Other file descriptors reported are:

cwd  current working directory; 
Lnn  library references (AIX); 
err  FD information error (see NAME column); 
jld  jail directory (FreeBSD); 
ltx  shared library text (code and data); 
Mxx  hex memory-mapped type number xx. m86  DOS Merge mapped file; 
mem  memory-mapped file; 
mmap memory-mapped device; 
pd   parent directory; 
rtd  root directory; 
tr   kernel trace file (OpenBSD); 
txt  program text (code and data); 
v86  VP/ix mapped file;

List open files for all processes

lsof | head

Sources

Tagged: Unix