0%

Buffered I/O

The term unbuffered means that each read or write invokes a system call in the kernel. These unbuffered I/O functions are not part of ISO C, but are part of POSIX.1.

File descriptors

To the kernel, all open files are refered to by file descriptors. A file descriptor is a non-negative integer. When we open an existing file or create a new file, the kernel returns a file descriptor to the process. When we want to read or write a file, we identify the file with the file descriptor that was returned by the open or create as an argument.

By convention, Unix shells associate file descriptor 0 with standard input of a process, file descriptor 1 with the standard output, and file descriptor 2 with the standard error.

The file descriptors returned by open and openat is guaranteed to be the lowest-numbered unused descriptor.

File sharing

The Unix supports the sharing of open files among different processes. The kernel uses three data structures to present an open file, and the relationships among them determine the effect one process has on another with regard to file sharing.

  1. Every process has an entry in the process table. Within each process table entry is a table of open file descriptors, which we can think of as a vector, with one entry per descriptor. Associated with each file descriptor are:
  • The file descriptor flags(close-on-exec)
  • A pointer to a file table entry
  1. The kernel maintains a file table for all open files. Each file table entry contains
  • The file status flags for the file, such as read, write, append, sync, and nonblocking
  • The current file offset
  • A pointer to the v-node table entry for the file
  1. Each open file(or device) has a v-node structure that contains information about the type of file and pointers to functions that operate on the file. For most file, the v-node also contains the i-node for the file. This information is read from disk when the file is opened, so that all the pertinent information about the file is readily available. For information, the i-node contains the owner of the file, the size of the file, pointers to where the actural data blocks for the file are located on disk, and so on.

File descriptor flags VS File status flags

File descriptor flags apply only to a single descriptor in a single process, whereas File status flags apply to all descriptors in any process that point to the given file table entry.

Atomic Operations

When a file is shared among mutiple processes, each process has its own file table entry with its own current file offset. Unexpected results can arise when mutiple processes write to the same file. Unix provides atomic operation to solve these probloms. Atomic operation refers to an operation that might be composed of mutiple steps. If the operation is performed atomically, either all the steps are performed(on success) or none are performed(on failure). It must not be possible for only a subset of the steps to be performed.

Reference

Adcanced Programming in the Unix Environment(Third Edition)