What are File Descriptors in Linux

On this article, you’ll study every thing about file descriptors, like their makes use of in Linux, what a file descriptor desk is, how one can view the file descriptors beneath a selected course of, and how one can change the restrict of a file descriptor in Linux.
What are File Descriptors in Linux?
A file descriptor is a optimistic integer that acts as a singular identifier (or deal with) for “information” and different I/O assets, reminiscent of pipes, sockets, blocks, units, or terminal I/O.
All of the file descriptor information are saved in a file descriptor desk within the kernel. When a file is opened, a brand new file descriptor (or integer worth) is given to that file within the file descriptor desk.
For instance, if you happen to open a “example_file1.txt
” file (which is nothing however a course of), it will likely be allotted with the obtainable file descriptor (for instance, 101), and a brand new entry will probably be created within the file descriptor desk.
And whenever you open one other file like “example_file2.txt
“, it will likely be allotted to a different obtainable file descriptor like 102, and one other entry will probably be created within the file descriptor desk.
File Descriptor | Course of |
---|---|
101 | example_file1.txt |
102 | example_file2.txt |
The file descriptor for the referenced file will probably be obtainable to be used by one other course of when you shut the file.
So, whenever you open lots of of information or different I/O assets in your Linux system, there will probably be 100 entries within the file descriptor desk, and every entry will reference a singular file descriptor (or integer worth like 100, 102, 103…) to determine the file.
What’s the File Descriptor Desk in Linux?
When a course of or I/O system makes a profitable request, the kernel returns a file descriptor to that course of and retains the checklist of present and all operating course of file descriptors within the file descriptor desk, which is someplace within the kernel.
Now, your course of may rely on different system assets like enter and output; as this occasion can also be a course of, it additionally has a file descriptor, which will probably be hooked up to your course of within the file descriptor desk.
Every file descriptor within the file descriptor desk factors to an entry within the kernel’s world file desk. The file desk entry maintains the file of file (or different I/O useful resource) modes like (r
)ead, (w
)ceremony, and (e
)xecute.
Additionally, the file desk entry factors to a 3rd desk often known as the inode desk that factors to precise file data like dimension, modification date, pointer, and so forth.

Predefined File Descriptors
By default, three sorts of customary POSIX file descriptors exist within the file descriptor desk, and also you may already be aware of them as data streams in Linux:
File Descriptor | Title | Abbreviation |
---|---|---|
0 |
Commonplace Enter | stdin |
1 |
Commonplace Output | stdout |
2 |
Commonplace Error | stderr |
Aside from them, each different course of has its personal set of file descriptors, however few of them (aside from some daemons) additionally make the most of the above-mentioned file descriptors to deal with enter, output, and errors for the method.
To be sure that the method is utilizing the above file descriptor, simply search for the above file descriptor (in integer format) beneath “/proc/PID/fd/
“, the place PID stands for “course of identifier.”
For instance, I’ve began the GEDIT editor on my system, which makes use of the entire file descriptors talked about above, as proven.

Record all of a Operating Course of’s File Descriptors
As you simply realized, every operating course of in Linux has its personal set of file descriptors, but it surely additionally makes use of others to determine the particular file when speaking with kernel area by way of system calls or library calls.
Discover the Course of ID (or PID)
First, discover out your course of identifier (or PID) utilizing the ps command earlier than viewing the file descriptors beneath it.
$ ps aux | grep gedit
Change “gedit
” together with your operating course of identify, or you possibly can place “$$
” to move the present bash session.

Now, you’ve got two methods to checklist the file descriptors beneath a selected course of, adopted by:
Utilizing the ls command
Record the entire file descriptors and the information they discuss with beneath a sure PID by itemizing the content material of the “/proc/PID/fd/
” path, the place PID is the method ID, utilizing the ls command.
$ ls -la /proc/11472/fd/
Output:

Utilizing the lsof command
The lsof command is used to checklist the details about operating processes within the system and will also be used to checklist the file descriptor beneath a selected PID.
For that, use the “-d
” flag to specify a spread of file descriptors, with the “-p
” possibility specifying the PID. To mix this choice, use the “-a
” flag.
$ lsof -a -d 0-2147483647 -p 11472
Output:

What’s the Goal of File Descriptors within the First Place?
The file descriptor, together with the file desk, hold monitor of every operating course of’s permissions in your system and keep knowledge integrity.
A operating course of can inherit the performance of one other course of by inheriting its file descriptor, as you simply realized on this article.
What Occurs If You Run Out of File Descriptors?
That is essential as a result of a file descriptor is an integer worth that the kernel returns to the method (or different I/O useful resource) after a profitable try and open a file.
There’s a restrict to the variety of file descriptors (or integer values) that may be given to a course of. When that restrict is reached, knowledge will be misplaced.
In Linux, usually, there are two sorts of file descriptors: process-level file descriptors and system-level file descriptors.
Course of-Degree File Descriptor Limits
Verify the present process-level file descriptor restrict utilizing the ulimit command.
$ ulimit -n
Output:

Reset the restrict by including a customized optimistic quantity after the command.
$ ulimit -n 3276800
Notice that non-root customers are additionally in a position to make use of the above command to alter the process-level limits (<Kernel 2.4.x), however you must add the next strains in “/and so forth/safety/limits.conf
” to assign the person modification permission:
gentle nofile 2048
arduous nofile 8192
System-Degree File Descriptor Limits
Verify the restrict of the system-level descriptor utilizing the cat command.
$ cat /proc/sys/fs/file-max
Output:

Modify the file with the brand new worth through the use of the “>
” redirection symbol.
$ echo 90000 > /proc/sys/fs/file-max
After modifying the above file, modify the worth within the “nr_open
” file.
$ echo "50000" > /proc/sys/fs/nr_open
Remaining Ideas!
I hope this text makes it clear so that you can perceive the workings of file descriptors in Linux computing.
In case you have any questions or queries associated to this subject, then be at liberty to ask them within the remark part.