Files and access methods
Files and access methods
Recorded by Dr. Krithika L.B. Slot m4-file-concepts
What a file is to the OS: attributes, operations, the open-file tables, file types and structure, and sequential, direct and indexed access.
By the end of this session you should be able to:
A file is a named collection of related information recorded on secondary storage — the smallest unit of logical storage the user sees. The OS maps files onto physical devices, so users never deal with sectors and tracks.
Human-readable, e.g. report.c.
Unique number inside the file system (Linux: inode number, ls -i).
Needed by systems that support types.
Pointer to the device and blocks.
Current size (and possibly maximum).
Who can read, write, execute (rwx bits).
Creation, last modification, last access — for protection, security and usage monitoring.
This information is kept in the directory structure / file control block (inode), which also lives on disk.
The OS provides system calls (Session 1.3) to create, write, read, reposition (seek), delete and truncate files. To
avoid searching the directory on every operation, a process first calls open(): the OS finds the file, copies its
information into an open-file table and returns an index — a file descriptor.
File descriptor → current file pointer (position) and access mode. Each process reading a file has its own position.
Location on disk, size, access dates, and an open count; the entry is removed when the count reaches 0 after the last close().
int fd = open("data.txt", O_RDWR | O_CREAT, 0644); /* create/open → descriptor */
write(fd, "Hello\n", 6); /* write at the file pointer */
lseek(fd, 0, SEEK_SET); /* reposition to the start */
char buf[16];
int n = read(fd, buf, sizeof buf); /* read n bytes */
close(fd); /* release the descriptor */
unlink("data.txt"); /* delete the file */A common technique is to include the type in the name as an extension: .c source, .o object,
.exe/none executable, .txt text, .zip archive, .mp4 multimedia. Windows relies on
extensions; UNIX uses a magic number at the start of the file (try the file command). Internally a file may be
unstructured (a sequence of bytes — UNIX), a sequence of fixed-length records, or a complex structure (an executable).
Sequential access — information is processed in order, one record after another, like a tape. read_next() reads and advances the pointer; write_next() appends; reset rewinds. Used by editors, compilers and most programs.
Direct (relative) access — the file is a numbered sequence of fixed-length blocks/records that can be read or written in any order: read(n), write(n). Ideal for databases: to find flight 713's seats, compute its block and jump there. Sequential access is easy to simulate on direct access (keep a counter cp).
Byte position of record n = n × record length (e.g. record 25 of 80-byte records starts at byte 2000).
Indexed access — an index (like a book's index) holds keys and pointers to blocks. Search the index, then access the block directly. For large files the index itself is indexed: a primary index points to secondary index blocks, which point to data (IBM ISAM).
Files and access methods
Recorded by Dr. Krithika L.B. Slot m4-file-concepts
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0open() searches the directory once and caches the file's information in the open-file table, so later operations use a descriptor instead of searching again.
They share the system-wide entry (location, size, open count); each has a private file pointer and access mode in its per-process table.
Direct (or indexed) access — jump straight to an account's record.
10 × 120 = 1200 (counting records from 0).
Thousands of files need to be organised — directories — and placed on disk blocks — allocation methods. Last session of Module 4.
Continue to 4.8 — Directory structures and file allocation →