Sub topic 12
File Access Methods
This chapter assumes you know what a file is and what attributes and operations it has (create, open, read, write, seek, close). It also assumes you know that a file system maps a file to disk blocks. Here we look at how a program reaches the data inside a file: sequentially, directly, or through an index.
Why access methods exist
A file is a sequence of bytes or fixed-size records. The access method is the rule a program uses to say which part of the file it wants next. The method belongs to the program's view of the file. The disk allocation scheme (contiguous, linked, indexed) is a separate choice. Some pairings are cheap and others are expensive, and later chapters return to that.
The operating system keeps one number per open file, the current file position (also called the file offset). Most access methods are different ways of reading or changing that number.
Sequential access
In sequential access, the program processes data in order, one record after another. Editors, compilers and media players mostly work this way.
The operations are:
read_next: return the record at the current position, then advance the position by one record.write_next: write at the current position, which is the end of the file when appending, then advance.reset(rewind): set the position to the start.- Some systems also allow skipping forward or back a few records.
The model comes from magnetic tape, but it is the default on disk files too. A POSIX read() call with no lseek() is sequential access.
Sequential access also lets the OS predict what comes next. It can read ahead, which means fetching the following blocks into the buffer cache before the program asks for them. This hides disk latency.
Direct (random) access
In direct access, the file is a numbered sequence of fixed-length records. The program can read or write any record without reading the ones before it. The operations take a record number n:
read(n)andwrite(n)position_file(n), followed byread_nextorwrite_next
The record number is relative to the start of the file, so record 0 is the first. The OS turns it into a byte offset:
offset = n × record_size
It then turns the offset into a logical block and a position inside that block:
logical_block = offset // block_sizeoffset_in_block = offset % block_size
Databases and any other application that needs quick lookup in a large file use this method. Sequential access can be simulated on top of direct access by keeping a counter and incrementing it. The reverse is not efficient, because reaching record n sequentially means passing over n - 1 records.
In POSIX, direct access is lseek(fd, offset, SEEK_SET) followed by read or write. The following program shows the offset arithmetic.
def locate(record_number, record_size, block_size):
offset = record_number * record_size
return offset // block_size, offset % block_size
# 100-byte records, 512-byte blocks
for n in (0, 4, 5, 10):
block, within = locate(n, 100, 512)
print(f"record {n}: logical block {block}, byte {within}")Record 5 starts at byte 500 of block 0, and it spills into block 1 because it needs bytes 500 to 599. A record can therefore cross a block boundary. The OS must then read two blocks for one record. That is one reason record sizes are often chosen to divide the block size evenly.
Indexed access
Indexed access is built on top of direct access. The file holds records, and a separate index lets the program find a record by a key instead of a number. The index is like the index of a book: it holds a key and a pointer to the record, so it is much smaller than the file.
To find a record, the program searches the index for the key and then uses the pointer to do a direct access to the file.
If the file is large, the index can itself be too big for memory. The solution is a multilevel index. A top-level index, small enough to stay in memory, points to blocks of a second-level index, and those blocks point to the data records.
A worked example makes the saving clear. Take a file of 1,000,000 records of 1 KB each, with an index entry of 8 bytes (key plus pointer) for every record.
- Index size: 1,000,000 × 8 B = 8 MB.
- Searching the whole file for a key reads up to 1 GB.
- Searching a sorted index with binary search takes about log2(1,000,000) ≈ 20 probes. With a second-level index in memory, only one index block and one data block are read from disk.
Examples are IBM's ISAM (Indexed Sequential Access Method) and, in modern systems, the B-tree indexes used by databases. Most general-purpose operating systems do not offer indexed access in the file system itself. They provide sequential and direct access, and the application or a database library builds indexes on top.
Comparison
| Method | Unit of access | Typical use | Cost to reach record n |
|---|---|---|---|
| Sequential | Next record | Logs, compilers, streaming | Read n records first |
| Direct | Record number | Fixed-record tables | One seek plus one read |
| Indexed | Key | Lookup by name or ID | Index search, then one direct read |
Access method versus allocation scheme
The access method says what the program asks for. The allocation scheme decides how hard that is for the file system to deliver. Keep the two apart in GATE questions.
- Contiguous allocation supports direct access well: block
iis atstart + i. - Linked allocation supports sequential access well, but direct access to block
ineedsipointer hops. FAT reduces that cost because the chain is in a table that is usually cached in memory. - Indexed allocation supports direct access with one extra index-block read, or a few for large files.
Common errors
- Counting records from 1 in the offset formula. With
offset = n × record_size, the first record is 0. If a problem numbers records from 1, use(n - 1) × record_size. - Forgetting that a record may straddle two blocks, so one logical read costs two block reads.
- Confusing the access method with the allocation method. "Sequential access" does not mean "contiguous allocation".
- Assuming direct access is always faster. On a hard disk, direct access to scattered records pays a seek and a rotational delay for each one, as the disk access time chapter showed. Sequential reads of adjacent blocks avoid most of that cost.
Exercise
A file has fixed 200-byte records and the file system uses 1024-byte blocks. Records are numbered from 0. For record 12, give the logical block number, the byte offset inside that block, and the number of blocks that must be read.
Show answer
Offset = 12 × 200 = 2400 bytes. Logical block = 2400 // 1024 = 2. Offset in block = 2400 % 1024 = 352. The record occupies bytes 2400 to 2599. Block 2 covers bytes 2048 to 3071, so the whole record lies inside block 2 and only one block is read.
Exercise
A program must look up customer records by a 16-byte customer ID in a file of 500,000 records. Which access method is the right fit, and why is plain direct access not enough?
Show answer
Indexed access. Direct access needs a record number, and the program only knows the customer ID. An index maps the ID to the record's location, so the program searches the small index and then does one direct read, instead of scanning the file sequentially.