The information stored in a file must be accessed and read into memory. Though there are many ways to access a file, some system provides only one method, other systems provide many methods, out of which you must choose the right one for the application.
Sequential Access Method
In this method, the information in the file is processed in order, one record after another. For example, compiler and various editors access files in this manner.
The read-next
– reads the next portion of a file and updates the file pointer which tracks the I/O location. Similarly, the write-next
will write at the end of a file and advances the pointer to the new end of the file.
The sequential access always reset to the beginning of the file and then starts skipping forward or backward records. It works for both sequential devices and random-access devices.
Direct Access Method
The other method for file access is direct access or relative access. For direct access, the file is viewed as a numbered sequence of blocks or records. This method is based on the disk model of file. Since disks allow random access to file block.
You can read block 34, then read 45, and write in block 78, there is no restriction on the order of access to the file.
The direct access method is used in database management. A query is satisfied immediately by accessing large amount of information stored in database files directly.
The database maintains an index of blocks which contains the block number. This block can be accessed directly, and information is retrieved.
The File Operations for Direct Access
Rather than read next
or write next
, the direct access method passes the block number as the parameter for read and write operations.
read n
write m
For example,
read 34
write 56
This is very similar to sequential access, but in addition to reading, the position the pointer to block 36 before a read or write operation.
The block number is a relative block number which is an index relative to the beginning of the file. Therefore, block 0 is the first relative block. The absolute disk location of the block may be 3553 and 4556 for the next block. This helps OS decide the placement of the file on the disk called the allocation problem
.
If user requests for a record N and a record length is L, then the system should read or write from L * (N) within the file.
Not all systems provide sequential and direct access. They either provide sequential access or direct access. Some system request access method information when the file is created. The access to the file depends on the declarations on these types of systems.
References
- Abraham Silberschatz, Peter B. Galvin, Greg Gagne (July 29, 2008) Operating System Concepts, 8 edn., : Wiley.