“A file descriptor is an opaque handle that is used in the interface between user and kernel space to identify file/socket resources.”
Representation
A file descriptor is simply a non-negative integer. It can be thought of as a “handle” or “reference” to an open file.
File descriptors are bound to a process ID. Processes maintain a table of file descriptors (open files being used by process). A file descriptor is just an index to a processes’ file descriptor table.
Viewing a Processes’ FD Table
lsof -p <PROCESS_ID>
This will result in something like the following:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sleep 14726 root cwd DIR 8,1 4096 1201140 /home/x
sleep 14726 root rtd DIR 8,1 4096 2 /
sleep 14726 root txt REG 8,1 35000 786587 /bin/sleep
sleep 14726 root mem REG 8,1 11864720 1186503 /usr/lib/locale/locale-archive
sleep 14726 root mem REG 8,1 2030544 137184 /lib/x86_64-linux-gnu/libc-2.27.so
sleep 14726 root mem REG 8,1 170960 137156 /lib/x86_64-linux-gnu/ld-2.27.so
sleep 14726 root 0u CHR 136,6 0t0 9 /dev/pts/6
sleep 14726 root 1u CHR 136,6 0t0 9 /dev/pts/6
sleep 14726 root 2u CHR 136,6 0t0 9 /dev/pts/6
FD:
The 4th column FD
and the next column TYPE
correspond to the File Descriptor and the File Descriptor type.
The following are the possible types:
cwd – Current Working Directory
txt – Text file
mem – Memory mapped file
mmap – Memory mapped device
Or simply a file descriptor followed by the file mode. For example:
0u
- File descriptor 0
in read/write mode
22r
- File descriptor 22
in read-only mode
111w
- File descriptor 111
in write mode
TYPE:
See also: File Types
The following types only apply to the non-numbered values in the table:
REG – Regular File
DIR – Directory
FIFO – First In First Out
Entries that are numbered file descriptors will always have the type CHR
for “character special file” or “character device file”