About

A file descriptor (Unix, Linux) or a file handle (Windows) is the connection id (generally to a file) from the Operating system in order to perform IO operations (Input/Ouput of Bytes).

For Wikipedia, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files.

In POSIX, this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly.

You can also see a file descriptor as an object that a process uses to:

A file descriptor is an opaque handle to the underlying machine-specific structure representing:

  • an open file,
  • an open socket,
  • or another source or sink of bytes.

In Unix-like systems, file descriptors (open files) can refer to:

A file descriptor is a low positive integer.

Management

Limit

Linux

In Linux, you can set the limit:

There is a limit to the amount of file descriptors per process.

If the file descriptor limit is exceeded for a process, you may see the following errors:

"Too Many Open Files"
"Err#24 EMFILE" (in truss output)

To resolve the issue, raise the OS limit on the number of available file descriptors

Windows

In Process explorer,

Process Explorer Handle Open File Search

Unix

For a process

# for a process
lsof -p PID

# Example for shared library
# lsof -p $PID | awk '{print $9}' | grep .so$

From a path

# from  a path
lsof /path
# Monitor continuously
lsof +D /path -r 2 
# example : the ``-f -- /'' arguments direct lsof to search for open files with a `/' path name, all open files in the `/' (root) file system.
sudo lsof -f -- /opt/app/logs/servicesFrameworks.log
# example all file in the bin with spy in their name
sudo lsof -s -- /bin/*spy*
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    3802 root  cwd    DIR    8,3     4096    2 .
lsof    7811 root  cwd    DIR    8,3     4096    2 .
lsof    7812 root  cwd    DIR    8,3     4096    2 .

For a device

sudo lsof /dev/hd4

Documentation / Reference