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?
Does a process table contain more than one file descriptor. If yes, why?
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.
/proc
all the time. –
Indecipherable p1
and p2
. By default the OS has connected their stdin
, stdout
and stderr
to keyboard, monitor and monitor respectively. These 3 standard streams have their FDs 0, 1 and 2 in both the processes. I can connect, say stdin
of p1
to some data.txt
. Hence, the FDs of p1
are independent from the file FDs of p2
. Am I wrong in my logic here somewhere? –
Stripteaser open()
will give you file descriptor 3, even if another running process happens to have a file descriptor 3. See the POSIX definition of open()
: "The open() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process." (emphasis added). –
Piperine lsof -p $SOMEPID
shows some common entries with lsof -p $SOMEOPTHERPID
then both processes are accessing the same entry? –
Niggerhead variable file = fopen('file-name')
, then is the variable file
a file descriptor? –
Remitter A file descriptor is an opaque handle that is used in the interface between user and kernel space to identify file/socket resources. Therefore, when you use open()
or socket()
(system calls to interface to the kernel), you are given a file descriptor, which is an integer (it is actually an index into the processes u structure - but that is not important). Therefore, if you want to interface directly with the kernel, using system calls to read()
, write()
, close()
etc. the handle you use is a file descriptor.
There is a layer of abstraction overlaid on the system calls, which is the stdio
interface. This provides more functionality/features than the basic system calls do. For this interface, the opaque handle you get is a FILE*
, which is returned by the fopen()
call. There are many many functions that use the stdio
interface fprintf()
, fscanf()
, fclose()
, which are there to make your life easier. In C, stdin
, stdout
, and stderr
are FILE*
, which in UNIX respectively map to file descriptors 0
, 1
and 2
.
Hear it from the Horse's Mouth : APUE (Richard Stevens).
To the kernel, all open files are referred to by File Descriptors. A file descriptor is a non-negative number.
When we open an existing file or create a new file, the kernel returns a file descriptor to the process. The kernel maintains a table of all open file descriptors, which are in use. The allotment of file descriptors is generally sequential and they are allotted to the file as the next free file descriptor from the pool of free file descriptors. When we closes the file, the file descriptor gets freed and is available for further allotment.
See this image for more details :
When we want to read or write a file, we identify the file with the file descriptor that was returned by open() or create() function call, and use it as an argument to either read() or write().
It is by convention that, UNIX System shells associates the file descriptor 0 with Standard Input of a process, file descriptor 1 with Standard Output, and file descriptor 2 with Standard Error.
File descriptor ranges from 0 to OPEN_MAX. File descriptor max value can be obtained with ulimit -n
. For more information, go through 3rd chapter of APUE Book.
osqueryi <<< echo '.all process_open_files'
in a bash shell. –
Braxton >
to highlight the quoted passage. –
Psychologize Other answers added great stuff. I will add just my 2 cents.
According to Wikipedia we know for sure: a file descriptor is a non-negative integer. The most important thing I think is missing, would be to say:
File descriptors are bound to a process ID.
We know most famous file descriptors are 0, 1 and 2.
0 corresponds to STDIN
, 1 to STDOUT
, and 2 to STDERR
.
Say, take shell processes as an example and how does it apply for it?
Check out this code
#>sleep 1000 &
[12] 14726
We created a process with the id 14726 (PID).
Using the lsof -p 14726
we can get the things like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sleep 14726 root cwd DIR 8,1 4096 1201140 /home/x
sleep 14726 root rtd DIR 8,1 4096 2 /
sleep 14726 root txt REG 8,1 35000 786587 /bin/sleep
sleep 14726 root mem REG 8,1 11864720 1186503 /usr/lib/locale/locale-archive
sleep 14726 root mem REG 8,1 2030544 137184 /lib/x86_64-linux-gnu/libc-2.27.so
sleep 14726 root mem REG 8,1 170960 137156 /lib/x86_64-linux-gnu/ld-2.27.so
sleep 14726 root 0u CHR 136,6 0t0 9 /dev/pts/6
sleep 14726 root 1u CHR 136,6 0t0 9 /dev/pts/6
sleep 14726 root 2u CHR 136,6 0t0 9 /dev/pts/6
The 4-th column FD and the very next column TYPE correspond to the File Descriptor and the File Descriptor type.
Some of the values for the FD can be:
cwd – Current Working Directory
txt – Text file
mem – Memory mapped file
mmap – Memory mapped device
But the real file descriptor is under:
NUMBER – Represent the actual file descriptor.
The character after the number i.e "1u", represents the mode in which the file is opened. r for read, w for write, u for read and write.
TYPE specifies the type of the file. Some of the values of TYPEs are:
REG – Regular File
DIR – Directory
FIFO – First In First Out
But all file descriptors are CHR – Character special file (or character device file)
Now, we can identify the File Descriptors for STDIN
, STDOUT
and STDERR
easy with lsof -p PID
, or we can see the same if we ls /proc/PID/fd
.
Note also that file descriptor table that kernel keeps track of is not the same as files table or inodes table. These are separate, as some other answers explained.
You may ask yourself where are these file descriptors physically and what is stored in /dev/pts/6
for instance
sleep 14726 root 0u CHR 136,6 0t0 9 /dev/pts/6
sleep 14726 root 1u CHR 136,6 0t0 9 /dev/pts/6
sleep 14726 root 2u CHR 136,6 0t0 9 /dev/pts/6
Well, /dev/pts/6
lives purely in memory. These are not regular files, but so called character device files. You can check this with: ls -l /dev/pts/6
and they will start with c
, in my case crw--w----
.
Just to recall most Linux like OS define seven types of files:
- Regular files
- Directories
- Character device files
- Block device files
- Local domain sockets
- Named pipes (FIFOs) and
- Symbolic links
File Descriptors (FD)
In Linux/Unix, everything is a file. Regular "files", directories, and even devices are files. Every file has an associated number called the file descriptor (FD), a non-negative integer that starts at 0.
Your terminal/console is a device, and therefore has a file descriptor associated with it. When an executing program prints something into the screen, the output is sent to the screen's file descriptor, and then the output is displayed in your screen by the associated device. Similarly, if the program's output is sent to the printer's file descriptor, the program's output is printed.
Whenever you execute a program/command at the terminal, the shell opens three files by default, each of them with an associated file descriptor and a pre-assigned role.
File File Descriptor Connected to (by default) Behavior Standard Input 0 keyboard A process uses to take input from a source. By default, from the keyboard. Standard Output 1 terminal/console A process uses to send normal output to a sink. By default, the terminal. Standard Error 2 terminal/console A process uses to senderror output to a sink. By default, the terminal. Because they're opened by default and have pre-assigned roles within any given process, they're collectively called standard streams.
Error Redirection
These standard streams are crucial for redirection and pipelines. For example, the command ls
outputs an error to the terminal if the provided directory doesn't exist:
$ ls ./non-existent
ls: ./nonexistent: No such file or directory
However using output redirection, you can send that output into another file, e.g., errors.log
, by using output redirection meta-character for standard error, 2>
:
$ ls ./non-existent 2> errors.log
This means "whatever output that comes out of standard error, redirect it to file errors.log
instead of the screen". Now errors.log
contains the output that previously was sent to the screen.
Pipeline
The standard streams also make it easy to make a program's output another program's input. In order to do this in the shell, you use the pipe (|
) meta-character.
$ ls -l | wc -l
This means "send the standard output of the ls -l
command into the standard input of the wc -l
command, and then send the result to the screen".
More points regarding File Descriptor
:
File Descriptors
(FD) are non-negative integers(0, 1, 2, ...)
that are associated with files that are opened.0, 1, 2
are standard FD's that corresponds toSTDIN_FILENO
,STDOUT_FILENO
andSTDERR_FILENO
(defined inunistd.h
) opened by default on behalf of shell when the program starts.FD's are allocated in the sequential order, meaning the lowest possible unallocated integer value.
FD's for a particular process can be seen in
/proc/$pid/fd
(on Unix based systems).
As an addition to other answers, unix considers everything as a file system. Your keyboard is a file that is read only from the perspective of the kernel. The screen is a write only file. Similarly, folders, input-output devices etc are also considered to be files. Whenever a file is opened, say when the device drivers[for device files] requests an open(), or a process opens an user file the kernel allocates a file descriptor, an integer that specifies the access to that file such it being read only, write only etc. [for reference : https://en.wikipedia.org/wiki/Everything_is_a_file ]
File descriptors
- To the kernel, all open files are referred to by file descriptors, including those entities that aren't files per se such as anonymous pipes and network sockets.
- A file descriptor is a non-negative integer, starting at 0.
- When we open an existing file or create a new one, the kernel returns a file descriptor to the calling code.
- When we want to read or write on a file, we identify the file with the file descriptor that was retuned by functions such as
open()
orcreate()
, and provided it to eitherread()
orwrite()
. - Initially each UNIX process has 20 file descriptors at its disposal, numbered 0 through 19 but it was extended to 63 by many systems.
- The first three are already opened when the process begins
- 0, the standard input
- 1, the standard output
- 2, the standard error
- When the parent process forks a process, the child process inherits the file descriptors of the parent.
All answer that are provided is great here is mine version --
File Descriptors are non-negative integers that act as an abstract handle to “Files” or I/O resources (like pipes, sockets, or data streams). These descriptors help us interact with these I/O resources and make working with them very easy. The I/O system is visible to a user process as a stream of bytes (I/O stream). A Unix process uses descriptors (small unsigned integers) to refer to I/O streams. The system calls related to the I/O operations take a descriptor as as argument.
Valid file descriptor ranges from 0 to a max descriptor number that is configurable (ulimit, /proc/sys/fs/file-max). Kernel assigns desc. for std input(0), std output(1) and std error(2) of the FD table. If a file open is not successful, fd return -1.
When a process makes a successful request to open a file, the kernel returns a file descriptor which points to an entry in the kernel's global file table. The file table entry contains information such as the inode of the file, byte offset, and the access restrictions for that data stream (read-only, write-only, etc.).
File descriptors are nothing but references for any open resource. As soon as you open a resource the kernel assumes you will be doing some operations on it. All the communication via your program and the resource happens over an interface and this interface is provided by the file-descriptor.
Since a process can open more than one resource, it is possible for a resource to have more than one file-descriptors.
You can view all file-descriptors linked to the process by simply running,
ls -li /proc/<pid>/fd/
here pid is the process-id of your process
Any operating system has processes (p's) running, say p1, p2, p3 and so forth. Each process usually makes an ongoing usage of files.
Each process is consisted of a process tree (or a process table, in another phrasing).
Usually, Operating systems represent each file in each process by a number (that is to say, in each process tree/table).
The first file used in the process is file0, second is file1, third is file2, and so forth.
Any such number is a file descriptor.
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.
In a similar way, when you open a network socket, it has a socket descriptor.
In some operating systems, you can run out of file descriptors, but such case is extremely rare, and the average computer user shouldn't worry from that.
File descriptors might be global (process A starts in say 0, and ends say in 1 ; Process B starts say in 2, and ends say in 3) and so forth, but as far as I know, usually in modern operating systems, file descriptors are not global, and are actually process-specific (process A starts in say 0 and ends say in 5, while process B starts in 0 and ends say in 10).
Addition to above all simplified responses.
If you are working with files in bash script, it's better to use file descriptor.
For example: If you want to read and write from/to the file "test.txt", use the file descriptor as show below:
FILE=$1 # give the name of file in the command line
exec 5<>$FILE # '5' here act as the file descriptor
# Reading from the file line by line using file descriptor
while read LINE; do
echo "$LINE"
done <&5
# Writing to the file using descriptor
echo "Adding the date: `date`" >&5
exec 5<&- # Closing a file descriptor
I'm don't know the kernel code, but I'll add my two cents here since I've been thinking about this for some time, and I think it'll be useful.
When you open a file, the kernel returns a file descriptor to interact with that file.
A file descriptor is an implementation of an API for the file you're opening. The kernel creates this file descriptor, stores it in an array, and gives it to you.
This API requires an implementation that allows you to read and write to the file, for example.
Now, think about what I said again, remembering that everything is a file — printers, monitors, HTTP connections etc.
That's my summary after reading https://www.bottomupcs.com/file_descriptors.xhtml.
© 2022 - 2024 — McMap. All rights reserved.