Ch 14 Advanced I/O

Ch 14 Advanced I/O

Record Locking

Used to ensure that a single process writes to a file. When the first process is reading or modifying a portion of a file, record locking can prevent other processes from modifying the same file region. It’s somewhat similar to gap locks in databases.

POSIX.1 fcntl locking structure includes:

  • Lock type: shared read lock, exclusive write lock, unlock a region
  • Starting byte offset of the lock or unlock region
  • Byte length of the region

Note: The region range can extend beyond the end, but cannot exceed the beginning of the file The mutual exclusion relationship between shared read locks and exclusive write locks is highly similar to locks in databases

The system automatically splits or merges intervals during lock/unlock operations Locks are automatically released after the file is closed

Child processes created by fork do not inherit locks set by the parent process. They need to call fcntl to acquire their own locks to prevent simultaneous file writing by parent and child processes

New programs executed via exec can inherit locks from the original executing program, but file descriptors with close-on-exec set will automatically release these locks

System Text Editors

General system editors don’t use record locking. They close the file after reading it, so they cannot support concurrent read/write access to each file by multiple users.

I/O Multiplexing

select Function

Parameters: descriptors of interest, conditions each descriptor cares about, waiting duration, total number of ready descriptors

Return value: -1 on error, 0 if no descriptors are ready, positive return value indicates the number of ready descriptors

poll Function

Similar to select, but poll constructs an array of pollfd structures, with each element specifying the descriptor number and conditions of interest

POSIX Asynchronous I/O

It provides a consistent method for performing asynchronous I/O on different types of files, using aiocb structures (AIO control blocks) to describe I/O operations

Asynchronous I/O operations must explicitly specify offsets The sigevent structure describes how applications should be notified upon I/O event completion

First initialize the asynchronous control block, then use the aio_read function for asynchronous reading and aio_write for asynchronous writing. When these functions return successfully, the asynchronous requests have already been placed in the operating system’s queue for processing.

During waiting, attention must be paid to keeping the AIO control block and database buffers stable; the corresponding memory must remain valid at all times and cannot be reused until the I/O operation completes.

Memory-Mapped I/O (mmap)

Maps a disk file to a buffer in storage space. When writing to the buffer, the corresponding bytes are automatically written to the file, allowing I/O operations without using write, and similarly for read.

mmap cannot be applied to copying between devices (network or terminal)