Apue Note - File I/O

Most file I/O can be performed using only five functions: open, read, write,
lseek, and close. Unbuffered IO 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 referred to by file
descriptors - non-negative integer. When we open an existing file or
create a new file, the kernel returns a file descriptor to the process.

Copy a file - using only read and write

Changing BUFFSIZE in it can test IO efficiency. From 1 byte to 512k bytes.
We found from 1024, the timing are quite the same.

Atomic Operations

The UNIX System provides an atomic way to do this operation if we set
the O_APPEND flag when a file is opened. As we described in the previous
section, this causes the kernel to position the file to its current end of
file before each write. We no longer have to call lseek before each write.

dup and dup2 Functions

dup2 is an atomic operation, whereas the alternate form involves two
function calls.

sync, fsync, and fdatasync Functions

Traditional implementations of the UNIX System have a buffer cache or
page cache in the kernel through which most disk I/O passes. When we write
data to a file, the data is normally copied by the kernel into one of its
buffers and queued for writing to disk at some later time. This is called
delayed write.

fcntl Function

The fcntl function can change the properties of a file that is already open.

ioctl Function

The ioctl function has always been the catchall for I/O operations. Anything
that couldn’t be expressed using one of the other functions in this chapter
usually ended up being specified with an ioctl. Terminal I/O was the biggest
user of this function.

/dev/fd

Newer systems provide a directory named /dev/fd whose entries are files
named 0, 1, 2, and so on. Opening the file /dev/fd/n is equivalent to
duplicating descriptor n, assuming that descriptor n is open.