Linux Interview Questions : Open Files / Open File Descriptors

What is an open file?

An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file.

What is file descriptor?

A file descriptor is a data structure used by a program to get a handle on a file. The most commonly known are:

0 for standard in 
1 for standard out
2 for standard error

Does the number of open files equal the number of open file descriptors?

There is a difference in the number of current open files and the number of current file descriptors/handlers. Even though a file is open, it might not have a file descriptor associated with it such as current working directories, memory mapped files and executable text files.

The ‘lsof’ command will provide the number of open files associated with a process. For Example, for the process with PID 4448:

# lsof | grep -w 2374
pickup    2374      postfix  cwd       DIR              202,1      4096    8945851 /var/spool/postfix
pickup    2374      postfix  rtd       DIR              202,1      4096        128 /
pickup    2374      postfix  txt       REG              202,1    285112    8945807 /usr/libexec/postfix/pickup
pickup    2374      postfix  mem       REG              202,1     62184     463326 /usr/lib64/libnss_files-2.17.so
pickup    2374      postfix  mem       REG              202,1    155744     758332 /usr/lib64/libselinux.so.1
pickup    2374      postfix  mem       REG              202,1     15688     125778 /usr/lib64/libkeyutils.so.1.5
pickup    2374      postfix  mem       REG              202,1     58728     127331 /usr/lib64/libkrb5support.so.0.1
pickup    2374      postfix  mem       REG              202,1     11384      37284 /usr/lib64/libfreebl3.so
....
# lsof | grep -w 2374 | wc -l
51

There are total 51 open files associated with PID 2374 (postfix).

To determine the number of open file descriptors associated with a process use the following. For the same postfix process, 2374:

# ls -l /proc/2374/fd 
total 0
lrwx------. 1 root root 64 Nov 18 18:06 0 -> /dev/null
lrwx------. 1 root root 64 Nov 18 18:06 1 -> /dev/null
l-wx------. 1 root root 64 Nov 18 18:06 10 -> pipe:[28867]
lrwx------. 1 root root 64 Nov 18 18:06 2 -> /dev/null
lr-x------. 1 root root 64 Nov 18 18:06 3 -> pipe:[18701]
l-wx------. 1 root root 64 Nov 18 18:06 4 -> pipe:[18701]
lrwx------. 1 root root 64 Nov 18 18:06 5 -> socket:[18631]
lrwx------. 1 root root 64 Nov 18 18:06 6 -> socket:[18629]
lrwx------. 1 root root 64 Nov 18 18:06 7 -> socket:[28851]
lrwx------. 1 root root 64 Nov 18 18:06 8 -> anon_inode:[eventpoll]
lr-x------. 1 root root 64 Nov 18 18:06 9 -> pipe:[28867]
l-wx------. 1 root root 64 Nov 18 18:06 92 -> pipe:[18702]
# ls -l /proc/2374/fd  | wc -l
13

There are 13 file descriptors associated with PID 2374 (postfix). We can see there is a difference between the number of open files and the number of file descriptors per process. postfix has 51 open files but only 13 file descriptors associated with it. Some of the open files do not have file descriptors associated with them; e.g., libraries, current working directories.

How to get the maximum number of file descriptors?

In Linux, maximum number of file descriptors limit can be read from /proc file system. To get the current limit on the number of file descriptors for the whole system use the following command.

# cat /proc/sys/fs/file-max
180451
Note: The parameter /proc/sys/fs/file-max can be changed dynamically.

How to compute the number of file descriptors currently being used?

Use the following command.

# cat /proc/sys/fs/file-nr
1344  0  180451
   |  |   |_ Max no. of file descriptors allowed on the system
   |  |      
   |  |__ Total free allocated file descriptors
   |
   |__  Total allocated file descriptors

To compute the number of file descriptors currently being used:

1344 - 0 = 1344

How to get the per-process limits on the file descriptors?

Use ‘ulimit -a‘ to get the per-process limits.

# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7132
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 4096
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7132
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

How does one find the number of open file descriptors being used by any given process?

Use the following command (Substitute your PID)

# ls -l /proc/2374/fd  | wc -l
13

What are some other useful parameters?

NR_OPEN  = Maximum number of open files per process
NR_FILE  = Total number of files that can be open in the system at any time
FILE-MAX = Kernel parameter refers to maximum number of file descriptors allowed per system 
FILE-NR  = Refers to the number of current file descriptors used at any moment.
LSOF     = Gives the number of open files.
Related Post