OS Notes
Understanding file concepts — file attributes, file types, file structure, file access methods (sequential, direct, indexed), and internal file representation.
Introduction
A file is the most fundamental unit of persistent storage that users interact with. But what exactly IS a file from the OS perspective? It is more than just data — it carries metadata (attributes), has structure, and can be accessed in different ways. Understanding file concepts gives you insight into how the OS efficiently stores and retrieves data.
File Attributes (Metadata)
Every file has associated metadata stored by the file system:
| Attribute | Description | Example |
|---|---|---|
| Name | Human-readable identifier | report.pdf |
| Type | Kind of file (extension) | .pdf, .txt, .exe |
| Size | Current size in bytes | 2,458,624 bytes |
| Location | Pointer to data on disk | Block 45892 |
| Protection | Access permissions | rwxr-xr-- |
| Owner | User who created/owns the file | john |
| Timestamps | Creation, modification, access times | 2024-01-15 14:30 |
| Link count | Number of directory entries pointing to file | 2 |
# View file attributes in Linux
$ ls -la report.pdf
-rw-r--r-- 1 john staff 2458624 Jan 15 14:30 report.pdf
# Detailed attributes
$ stat report.pdf
File: report.pdf
Size: 2458624 Blocks: 4808 IO Block: 4096 regular file
Inode: 8421507 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 501/john) Gid: ( 20/staff)
Access: 2024-01-15 14:30:00
Modify: 2024-01-15 14:30:00
Change: 2024-01-15 14:30:00File Types
Operating systems recognize different types of files:
Regular files: Contain user data — text, binary, images, executables. The OS does not care about the internal format; that is the application's responsibility.
Directory files: Contain lists of other files and their attributes. Special structure maintained by the OS.
Special files: Represent devices or OS constructs:
- Character special files: Represent character devices (keyboard, serial port)
- Block special files: Represent block devices (disk drives)
- Symbolic links: Pointers to other files
- Pipes/Sockets: IPC mechanisms represented as files (Unix philosophy)
File Extensions and Magic Numbers
The OS identifies file types through:
- Extensions: .txt, .pdf, .exe (convention, not enforced on Linux)
- Magic numbers: First few bytes identify the type (PDF starts with
%PDF, PNG starts with\x89PNG) - Metadata: Some file systems store type information explicitly
File Access Methods
How data within a file is read or written:
Sequential Access
Data is processed in order, from beginning to end. Like reading a book cover to cover. Most common access pattern.
Direct (Random) Access
Any record can be read in any order by specifying its position. Like looking up a specific page in a book.
// Direct access: jump to any position
lseek(fd, 1000, SEEK_SET); // Move to byte 1000
read(fd, buffer, 100); // Read 100 bytes from position 1000
lseek(fd, 5000, SEEK_SET); // Jump to byte 5000
read(fd, buffer, 100); // Read 100 bytes from position 5000Indexed Access
An index (like a book's table of contents) maps keys to record positions. The system searches the index first, then directly accesses the record. Used in databases.
| "Alice" | Block 45, Record 3 |
| "Bob" | Block 12, Record 7 |
| "Carol" | Block 89, Record 1 |
| To find "Bob": Search index | Jump to Block 12, Record 7 → Read data |
Internal File Structure
How does the OS view a file's internal organization?
Unstructured (byte stream): Unix/Linux approach. A file is simply a sequence of bytes. The OS does not care about internal structure — applications interpret the bytes however they want. Maximum flexibility.
Simple record structure: File is a sequence of fixed-length or variable-length records. Each record is one logical unit (e.g., one line of text, one database row).
Complex structures: Some OS support structured files natively (indexed files, B-tree organized files). More common in mainframe systems.
Modern systems overwhelmingly use the byte-stream approach — the OS provides raw bytes, and applications (or libraries) impose structure.
Open File Table
When a process opens a file, the OS creates entries in two tables:
Per-process open file table: Contains process-specific information (current position, access mode) System-wide open file table: Contains file location on disk, size, open count (shared across processes)
| Per-process table: fd=3 | [position=0, mode=READ, ptr to system entry] |
| System table: entry | [inode, size=1024, disk_location, open_count=1] |
| Per-process table: fd=4 | [position=0, mode=READ, ptr to same system entry] |
| System table: entry | [inode, size=1024, disk_location, open_count=2] |
Real-World Analogy
File concepts are like library book management. File attributes are the catalog card (author, title, shelf location, checkout status). File types are the Dewey Decimal classifications (fiction, non-fiction, reference). Sequential access is reading a novel start to finish. Direct access is looking up a specific encyclopedia entry. The open file table is like the librarian's list of currently checked-out books and who has them.
Key Takeaways
- Files have both data (content) and metadata (attributes like name, size, permissions, timestamps)
- File types include regular files, directories, and special files (devices, links, pipes)
- Three access methods: sequential (in order), direct (any position), and indexed (key-based lookup)
- Modern OS treat files as unstructured byte streams — applications impose structure
- Open file tables track which files are in use and maintain per-process read/write positions
- Understanding file concepts is essential for efficient file system programming
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for File Concepts.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, file, system, concepts, file concepts
Related Operating Systems Topics