Summary
In the course of this chapter, we introduced the concept of atomicity, which is crucial to the correct operation of some system calls. In particular, the open() O_EXCL flag allows the caller to ensure that it is the creator of a file, and the open() O_APPEND flag ensures that multiple processes appending data to the same file don’t overwrite each other’s output.
The fcntl() system call performs a variety of file control operations, including changing open file status flags and duplicating file descriptors.
Duplicating file descriptors is also possible using dup() and dup2().
We looked at the relationship between file descriptors, open file descriptions, and file i-nodes, and noted that different information is associated with each of these three objects.
Duplicate file descriptors refer to the same open file description, and thus share open file status flags and the file offset.
We described a number of system calls that extend the functionality of the conventional read() and write() system calls.
The pread() and pwrite() system calls perform I/O at a specified file location without changing the file offset.
The readv() and writev() calls perform scatter-gather I/O.
The preadv() and pwritev() calls combine scatter-gather I/O functionality with the ability to perform I/O at a specified file
location.
The truncate() and ftruncate() system calls can be used to decrease the size of a file, discarding the excess bytes, or to increase the size, padding with a zero-filled file hole.
We briefly introduced the concept of nonblocking I/O, and we’ll return to it in later chapters.
The LFS specification defines a set of extensions that allow processes running on 32-bit systems to perform operations on files whose size is too large to be represented in 32 bits.
The numbered files in the /dev/fd virtual directory allow a process to access its own open files via file descriptor numbers, which can be particularly useful in shell commands.
The mkstemp() and tmpfile() functions allow an application to create temporary files.