The Six Basic File Operations
Most file creation and file I/O needs can be met by the six basic system calls shown in Table 3.1. This section uses these commands to show a basic implementation of the UNIX cat command, which is one of the easiest of the UNIX commands to implement.
However, before giving its implementation, it is necessary to describe the terms standard input, standard output, and standard error. As described in the section File Descriptors in Chapter 2, the first file that is opened by a user process is assigned a file descriptor value of 3. When the new process is created, it typically inherits the first three file descriptors from its parent. These file descriptors (0, 1, and 2) have a special meaning to routines in the C runtime library and refer to the standard input, standard output, and standard error of the process respectively. When using library routines, a file stream is specified that determines where data is to be read from or written to. Some functions such as printf() write to standard output by default. For other routines such as fprintf(), the file stream must be specified. For standard output, stdout may be used and for standard error, stderr may be used. Similarly, when using routines that require an input stream, stdin may be used. Chapter 5 describes the implementation of the standard I/O library. For now simply consider them as a layer on top of file descriptors.
When directly invoking system calls, which requires file descriptors, the constants ...
Get UNIX Filesystems: Evolution, Design, and Implementation now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.