Sub topic 10
File Access Methods
Assume you understand processes, system calls, kernel and user mode, binary arithmetic, and simple averages and ratios. This chapter explains how a process locates data inside a file. The access method affects performance and which operations are convenient.
The working model
A file is a logical sequence of bytes or records. An access method is the rule used to find the next piece of data.
| Method | How data is found | Best fit |
|---|---|---|
| Sequential | Read from the beginning toward the end | Logs, tapes, complete scans |
| Direct | Compute or provide the position of a block or record | Databases, fixed-size records |
| Indexed | Search an index that points to data locations | Large files with selective searches |
The operating system usually transfers data in blocks, even when a program asks for smaller pieces. A file descriptor stores the current file position for sequential operations. A direct-access operation can change that position explicitly.
Sequential access
With sequential access, the program reads records in order. To reach record 8, it normally reads records 1 through 7 first. This is efficient when the program needs most of the file because each block is read once and the access pattern is predictable.
Appending is also natural for many sequential files. Searching for one late record, however, can be slow.
Direct access
Direct, or random, access lets a program jump to a location. If every record has size s bytes and record numbering starts at zero, the byte offset of record i is:
offset = i × sFor example, record 25 in a file of 100-byte records begins at byte 2,500. Fixed-size records make this calculation simple. Variable-size records need an offset table or another structure because the position cannot be calculated from the record number alone.
def record_offset(record_number, record_size):
if record_number < 0 or record_size <= 0:
raise ValueError("record number must be nonnegative and size must be positive")
return record_number * record_size
print(record_offset(25, 100)) # 2500Indexed access
Indexed access uses a separate structure called an index. An index maps a search key, such as a student ID, to a data location. The program searches the index first, then jumps to the referenced block.
An index reduces search work when only a few records are needed. It requires extra storage and maintenance. If records move, their index entries must be updated. A multi-level index can index the index itself when the first index becomes too large.
Choosing a method
Use sequential access for ordered processing, direct access for predictable fixed-size positions, and indexed access for frequent searches by a key. A system can combine methods: an indexed file may use direct access after finding the target block.
Exercise
A file contains 80-byte records. What byte offset contains record 12 when numbering starts at zero, and which access method can use this calculation?
Show answer
The offset is 12 × 80 = 960 bytes. Direct access can use this calculation.