Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are file descriptors, explained in simple terms?

  1. What would be a more simplified description of file descriptors compared to Wikipedia's? Why are they required? Say, take shell processes as an example and how does it apply for it?

  2. Does a process table contain more than one file descriptor. If yes, why?

like image 701
Nishant Avatar asked Mar 10 '11 07:03

Nishant


People also ask

What does a file descriptor look like?

File descriptors are usually integers (0, 1, 2 and not 0.5, 1.5, 2.5). Given we often describe processes as "process-tables", and given that tables has rows (entries) we can say that the file descriptor cell in each entry, uses to represent the whole entry.

Are file descriptors per process?

Linux systems limit the number of file descriptors that any one process may open to 1024 per process. (This condition is not a problem on Solaris machines, x86, x64, or SPARC). After the directory server has exceeded the file descriptor limit of 1024 per process, any new process and worker threads will be blocked.

What are valid file descriptors?

Range of possible values of file descriptors is from 0 to 1023 for Linux system (32-bit or 64-bit system). You cannot create a file descriptor with value more then 1023. In case of file descriptor of value 1024, it will return an error of EBADF (bad file descriptor, error no-9).


1 Answers

In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS (somewhere in kernel). These entries are represented by integers like (...100, 101, 102....). This entry number is the file descriptor. So it is just an integer number that uniquely represents an opened file for the process. If your process opens 10 files then your Process table will have 10 entries for file descriptors.

Similarly, when you open a network socket, it is also represented by an integer and it is called Socket Descriptor. I hope you understand.

like image 75
Tayyab Avatar answered Sep 21 '22 09:09

Tayyab