File System - File Descriptor or Handle (Open File)

1 - 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:

  • read or write to an open file
  • open network sockets

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 can refer to:

A file descriptor is a low positive integer.

3 - Management

3.1 - Limit

3.1.1 - 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

3.2.1 - Windows

In Process explorer,

3.3 - Unix

3.3.1 - For a process

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

3.3.2 - From a path

# from  a path
lsof /path
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 .

4 - Documentation / Reference

file/file_descriptor.txt · Last modified: 2018/09/16 12:42 by gerardnico