CACS251 Operating System

Operating SystemUnit 612 min read

File Systems: Structure, Access, and Management

Unit 6 of Operating System explores file systems—how files and directories are organized, accessed, and managed in storage devices. This note covers file attributes, directory structures, access methods, file system implementation (FAT, NTFS, ext4), and real-world applications like eSewa’s transaction logs and Daraz’s

TAKEAWAYS:

  • A file is a named collection of related data stored on disk, with attributes like name, type, size, and permissions.
  • Directories organize files hierarchically (e.g., /home/user/documents), enabling efficient navigation and access control.
  • Access methods (sequential, direct, indexed) determine how data is read/written, critical for performance in apps like WhatsApp media storage.
  • File system implementations (FAT32, NTFS, ext4) differ in metadata handling, journaling, and scalability—NTFS uses ACLs for granular permissions (like bank transaction logs).
  • Allocation methods (contiguous, linked, indexed) trade off speed vs. fragmentation; indexed allocation (used in databases) minimizes wasted space.
  • Backup and recovery (full, incremental, differential) are essential for data integrity, as seen in NTC’s network configuration backups.

1. What is a File?

A file is a named collection of related data stored on secondary storage (e.g., HDD, SSD). Files are the fundamental unit of data storage in an OS, used by applications and users alike.

Key File Attributes

Every file has metadata (data about data) stored in the file control block (FCB). Common attributes include:

Attribute Description Example
File Name Unique identifier (e.g., report.txt). invoice_2024.pdf
Identifier Unique system-assigned number (e.g., inode in Unix). inode 12345
Type File category (e.g., text, binary, executable). .exe, .jpg, .csv
Location Disk address where data is stored (e.g., block numbers). Block 10-15
Size Current size in bytes. 4096 bytes
Protection Access permissions (read/write/execute). rwxr-xr-- (Unix)
Time/Date Creation, last modification, last access timestamps. 2024-05-20 14:30
User ID Owner of the file. UID 1001 (user)

2. File Operations

Files are manipulated using system calls like:

  • Create: Allocate space and initialize FCB (e.g., touch file.txt in Linux).
  • Delete: Free space and remove FCB (e.g., rm file.txt).
  • Open/Close: Establish or terminate access to a file.
  • Read/Write: Transfer data between memory and disk.
  • Truncate: Reduce file size to zero (e.g., clearing a log file).
Metadata lookupParent linkData storageFileFCBDirectoryDisk Blocks
File Operations: How a file is accessed via FCB and directory structure

Worked Example: eSewa Transaction Log eSewa stores each transaction as a sequential file (appended in order). When a user pays a bill:

  1. The OS opens the log file (transactions.log) in append mode.
  2. A new record (e.g., TXN12345, UserID: 5678, Amount: 500, Time: 2024-05-20 15:00) is written.
  3. The file is closed, and the OS updates the FCB with the new size.

3. Directory Structure

Directories (folders) organize files hierarchically, enabling:

  • Pathnames: Absolute (/home/user/documents/report.txt) or relative (../reports/2024.txt).
  • Namespace management: Avoid naming conflicts (e.g., two files named data.txt in different folders).
  • Access control: Permissions can be set per directory (e.g., chmod 755 folder/ in Linux).

Hierarchical Directory Example

root
├── home
│   ├── user1
│   │   ├── documents
│   │   │   └── report.txt
│   │   └── downloads
│   └── user2
└── var
    └── logs
        └── system.log

4. Access Methods

Files can be accessed in different ways, affecting performance and use cases:

Method Description Example Use Case
Sequential Read/write data in order (like a tape). Log files, video playback.
Direct Access any record instantly using a key (e.g., student_id). Database records (e.g., NEPSE stock data).
Indexed Uses an index table to map keys to disk blocks. File systems like ext4, databases.

Worked Example: Daraz Order Database Daraz uses indexed access for order records:

  • Each order has a unique order_id (key).
  • The database maintains an index mapping order_id to disk blocks.
  • When a user checks their order status, the system directly accesses the block using the index, avoiding sequential scans.

5. File System Implementation

File systems define how data is stored and managed on disks. Key implementations:

A. FAT (File Allocation Table)

  • Structure: Uses a table to map clusters (fixed-size blocks) to files.
  • Pros: Simple, widely compatible (used in USB drives).
  • Cons: No journaling (data loss risk on crashes), inefficient for large files.
  • Example: Old USB drives formatted as FAT32.

B. NTFS (New Technology File System)

  • Features:
    • Supports large files (>4GB), compression, encryption.
    • Uses ACLs (Access Control Lists) for granular permissions (e.g., user1: read-only, admin: full access).
    • Journaling for crash recovery.
  • Example: Windows systems, bank transaction logs (NTFS ensures secure, recoverable storage).

C. ext4 (Fourth Extended Filesystem)

  • Features:
    • Supports files >16TB, journaling, and fast directory searches.
    • Used in Linux systems (e.g., Ubuntu servers).
  • Example: NTC’s network configuration files stored on ext4 partitions.

Comparison Table

Feature FAT32 NTFS ext4
Max File Size 4GB 16EB (exabyte) 16TB
Journaling ❌ No ✅ Yes ✅ Yes
ACLs ❌ No ✅ Yes ✅ Yes
Use Case USB drives Windows, enterprise Linux servers

6. File Allocation Methods

How disk space is allocated to files impacts performance and fragmentation:

ApplicationFile RequestFile SystemFCB/IndexDisk BlocksData
File System as a Middleware Layer

A. Contiguous Allocation

  • How it works: Allocate contiguous blocks for a file (e.g., blocks 10-15 for file.txt).
  • Pros: Fast access (sequential reads).
  • Cons: External fragmentation (free blocks scattered).
  • Example: Early DOS file systems.

B. Linked Allocation

  • How it works: Each block points to the next (like a linked list).
  • Pros: No external fragmentation.
  • Cons: Slow random access (must traverse links), risk of corruption if a pointer fails.
  • Example: Some embedded systems.

C. Indexed Allocation

  • How it works: Uses an index block to map file data blocks.
  • Pros: No fragmentation, fast random access.
  • Cons: Overhead for small files (index block may be larger than the file).
  • Example: ext4, databases (e.g., MySQL’s InnoDB).

Visual: Indexed Allocation

Block 10Block 20Block 30Block 40Index Block (Pointers)
Indexed Allocation: Index block points to data blocks (ext4 example)

7. Access Control

Files and directories must restrict access to authorized users only.

A. Access Control Matrix (ACM)

  • A matrix where rows = subjects (users/processes), columns = objects (files), and entries = permissions.
  • Example:
    | User \ File | report.txt | data.csv |
    |-------------|------------|-----------|
    | Alice       | Read       | Write     |
    | Bob         | Read       | Read      |
    

B. Access Control List (ACL)

  • A list attached to each file/directory specifying permissions for users/groups.
  • Example (Linux):
    $ ls -l report.txt
    -rw-r--r-- 1 alice users 1024 May 20 15:00 report.txt
    
    • rw-: Owner (Alice) can read/write.
    • r--: Group (users) can read only.
    • r--: Others can read only.

Worked Example: Bank Transaction Files A bank’s NTFS file system uses ACLs to secure transaction logs:

  • admin: Full control (read/write/delete).
  • auditor: Read-only.
  • backup_service: Write-only (appends logs).

8. Backup and Recovery

Data loss can occur due to hardware failure, corruption, or user errors. Backup strategies:

Type Description Example
Full Backup Copies all files. Weekly backup of NTC’s configs.
Incremental Copies only changes since last backup. Daily backups of eSewa logs.
Differential Copies changes since last full backup. Monthly differential backups.

Recovery Process:

  1. Restore the last full backup.
  2. Apply incremental/differential backups in order.

9. Real-World Applications

A. eSewa: Transaction Logging

  • Idea Used: Sequential file access + indexed allocation.
  • How:
    • Each transaction is appended to transactions.log (sequential).
    • An index table maps transaction_id to log offsets for fast retrieval.
    • NTFS ACLs ensure only authorized services can write to the log.

B. Daraz: Order Management

  • Idea Used: Indexed file allocation + direct access.
  • How:
    • Orders are stored in a database with order_id as the primary key.
    • The database uses B-trees (indexed allocation) for O(log n) lookup time.
    • Example query: SELECT * FROM orders WHERE order_id = 12345 retrieves the order instantly.

C. NTC: Network Configuration

  • Idea Used: Hierarchical directories + ACLs.
  • How:
    • Config files are stored in /etc/ntc/ with subdirectories for routers, switches, etc.
    • ACLs restrict access: only ntc_admin can modify configs.
    • Full backups run weekly; incremental backups daily.

10. Exam Tip

  1. Definitions: Know the difference between file, directory, FCB, ACL, and ACM. For example:
    • "A file is a named collection of related data, while a directory is a file that contains references to other files."
  2. Worked Examples: Practice Banker’s algorithm questions (even though they’re in Unit 9, file systems often appear in combined questions). For example:
    • Given a resource allocation matrix, calculate the need matrix and check for safety using Banker’s algorithm.
  3. Diagrams: Draw:
    • A hierarchical directory structure (e.g., /home/user/documents).
    • A file allocation table (FAT) or index block for indexed allocation.
    • An ACL example (e.g., Linux permissions rwxr-xr--).
  4. Real-World Links: Connect concepts to apps:
    • Sequential access → Log files (eSewa, banks).
    • Indexed allocation → Databases (Daraz orders, NEPSE).
    • ACLs → Secure file systems (NTFS in banks).
  5. Common Pitfalls:
    • Don’t confuse contiguous allocation (fast but fragmented) with linked allocation (no fragmentation but slow random access).
    • Remember: ext4 uses inodes (like Unix), while NTFS uses MFT (Master File Table).
  6. Short-Answer Tips:
    • For "Explain hierarchical directory system", describe the root → subdirectories → files structure with an example like /var/log/system.log.
    • For "Define ACL", say it’s a list of permissions attached to a file (e.g., user: read-only).

Summary Checklist

Before the exam, ensure you can: ✅ Define file, directory, FCB, ACL, and ACM. ✅ Compare FAT, NTFS, and ext4 in a table. ✅ Explain contiguous, linked, and indexed allocation with pros/cons. ✅ Draw a hierarchical directory and label paths. ✅ Calculate a need matrix (even if it’s for Unit 9, it’s often tested with file systems). ✅ Link concepts to eSewa, Daraz, or NTC in 1-2 sentences.

Based on the TU BCA syllabus for Operating System (CACS251), unit 6.

Discussion

Loading…