File I/O
- A file descriptor is an integer used to reference a file.
- Actually it is a bit more complicated than that.
- On the file system, the space is decomposed into a number of blocks.
- Each of these blocks is of a fixed size and has an address.
- Blocks are collected into files by inodes
- For each file on the system, there is an inode.
- inodes are identified by unique numbers.
- Much more on this in chapter 14 if we get there.
- The Inode contains information such as
- File type : file, pipe, socket, ...
- List of blocks in the file.
- Access times and other meta information about the file
- There is one inode per file.
- But, strangely, multiple file names may point to the same inode.
- This is called a hard link.
- To keep track of open files, the system maintains a table of active files
- This is a system wide table.
- This table contains information about the files that are open
- The inode number
- The status of the file.
- An offset into the file (currently position where the accessing processes is reading/writing)
- There is at least one entry in this table per open file.
- But there may be multiple entries per inode.
- Program 1 and Program 2 are both accessing foo.txt
- This table is probably a static array, built at kernel compile time
- So there is a system wide limit on open files.
- (Open can fail with ENFILE The system-wide limit on the total number of open files has been reached.)
- Finally, there is a file descriptor table
- This is a per process table.
- It contains file flags
- And a pointer into the system wide open file table.
- It is most likely a static table as well.
- So there is a limit on the number of open file descriptors a process may have
- ( Open can fail with EMFILE The per-process limit on the number of open file descriptors has been reached )
- A file descriptor points to an open file, which points to an inode.
- Multiple file descriptors (in the same or different processes) can point to the same open file, which points to a single inode
-
- As indicated, file descriptors are small non-negative integers
- They are indexes into the file descriptor table.
- File descriptors are created with a number of system commands
- File descriptors are accessed with a number of system commands
- File descriptors are released with close
- Each process is created with three open file descriptors
- standard input, fd = 0, STDIN_FILENO
- standard output, fd = 1, STDOUT_FILENO
- standard error, fd = 2, STDERR_FILENO
- Processes also inherits file open file descriptors from their parent.
- discuss Code2HtML < a.C > a.html