Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with File descriptors in Unix

Tags:

linux

unix

Every process has a file descriptor table (FDT) and each file has a file descriptor. The file descriptors for stdin, stdout and stderr are 0,1, and 2. These values are same for all processes. The FDT I believe contains references to the INODE entries of those file. The file descriptors are reused across processes i.e. they are not globally unique. Is there a global FDT maintained by kernel to which each process' FDT references? What do FDT for stdin, stdout and stderr correspond to? Are these special files linked to the keyboard, display etc. Please provide links to articles, books etc.

like image 758
Bruce Avatar asked Sep 22 '11 07:09

Bruce


1 Answers

A good starting point is the article "A small trail through the Linux kernel" from 2001. The mechanisms are still similar, though the implementation has moved on and is best studied in a more recent kernel.

Inside the kernel each open file descriptor corresponds to a struct file, which contains all the information about the open file or device. The file descriptor is really no more than an index into the FDT for the process. In the Linux kernel the struct file is attached to the FDT by the function fd_install(). The struct file can be reassigned to another file descriptor by the dup2 system call.

Processes can share the same FDT if the processes were created by the clone system call with the CLONE_FILES flag, but there is no global FDT. The normal fork operation creates a new FDT which is a copy of the parent FDT. The practical use of this is for each thread of an multi-threaded application to be a cloned process sharing a common FDT, ensuring that all threads can use the same integer file descriptors. If you create a new process using fork/exec, the new process starts with the same file descriptors but can open and close files without affecting the parent.

The FDT entries for stdin, stdout, stderr are inherited from the parent. There is nothing special about their kernel implementation of these three FDT entries; their meaning comes from the conventional use by the C library. The parent process alone decides what they are connected to. They may connect to character devices, or they may have been connected to files or to pipes. For the character device case, the most normal is to be a tty or pty device. The free book Linux Device Drivers has a good overview of these.

like image 152
Adrian Cox Avatar answered Sep 28 '22 02:09

Adrian Cox