File System Basics ⭐⭐⭐

File System Basics ⭐⭐⭐

Overview

File systems define how operating systems store and manage data on disks. Learn core concepts including file concepts, attributes, operations, directory structures, and access methods.


Table of Contents

  1. File Concept
  2. File Attributes
  3. File Operations and System Calls
  4. Directory Structures
  5. File Access Methods
  6. File System Mounting
  7. Practice Problems

1. File Concept

1.1 What is a File?

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         File Definition                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   File = Named collection of related information                        β”‚
β”‚                                                                          β”‚
β”‚   Operating System Perspective:                                          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚                                                                  β”‚   β”‚
β”‚   β”‚   Disk (Physical Storage Device)                                β”‚   β”‚
β”‚   β”‚   β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”            β”‚   β”‚
β”‚   β”‚   β”‚Block0β”‚Block1β”‚Block2β”‚Block3β”‚Block4β”‚Block5β”‚Block6β”‚Block7β”‚    β”‚   β”‚
β”‚   β”‚   β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜            β”‚   β”‚
β”‚   β”‚       ↑         ↑     ↑         ↑     ↑     ↑                   β”‚   β”‚
β”‚   β”‚       β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜     β””β”€β”€β”¬β”€β”€β”˜                   β”‚   β”‚
β”‚   β”‚            β”‚               β”‚             β”‚                       β”‚   β”‚
β”‚   β”‚         File A          File B        File C                     β”‚   β”‚
β”‚   β”‚                                                                  β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                                          β”‚
β”‚   User Perspective:                                                      β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚                                                                  β”‚   β”‚
β”‚   β”‚   Logical Storage Units                                          β”‚   β”‚
β”‚   β”‚                                                                  β”‚   β”‚
β”‚   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚   β”‚
β”‚   β”‚   β”‚ report.docx  β”‚  β”‚ photo.jpg    β”‚  β”‚ program.exe  β”‚          β”‚   β”‚
β”‚   β”‚   β”‚ (Document)   β”‚  β”‚ (Image)      β”‚  β”‚ (Executable) β”‚          β”‚   β”‚
β”‚   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚   β”‚
β”‚   β”‚                                                                  β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                                          β”‚
β”‚   File System: Abstraction layer connecting these two views             β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1.2 File Types

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          File Types                                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚   β”‚     Type      β”‚  Extension   β”‚            Description          β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Executable    β”‚ .exe, .com   β”‚ Machine code                    β”‚    β”‚
β”‚   β”‚               β”‚ .bin, (none) β”‚                                 β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Object        β”‚ .o, .obj     β”‚ Compiled object code            β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Source Code   β”‚ .c, .py, .js β”‚ Programming language source     β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Text          β”‚ .txt, .md    β”‚ Plain text                      β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Library       β”‚ .a, .so      β”‚ Static/dynamic library          β”‚    β”‚
β”‚   β”‚               β”‚ .lib, .dll   β”‚                                 β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Image         β”‚ .jpg, .png   β”‚ Graphics data                   β”‚    β”‚
β”‚   β”‚               β”‚ .gif, .bmp   β”‚                                 β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ Archive       β”‚ .zip, .tar   β”‚ Compressed archive              β”‚    β”‚
β”‚   β”‚               β”‚ .gz, .7z     β”‚                                 β”‚    β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                                                          β”‚
β”‚   Unix doesn't require extensions (convention only)                     β”‚
β”‚   Type detection by content: file command                                β”‚
β”‚                                                                          β”‚
β”‚   $ file /bin/ls                                                        β”‚
β”‚   /bin/ls: ELF 64-bit LSB shared object, x86-64...                     β”‚
β”‚                                                                          β”‚
β”‚   $ file report.pdf                                                     β”‚
β”‚   report.pdf: PDF document, version 1.4                                 β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2. File Attributes

2.1 Basic Attributes (Metadata)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         File Attributes                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚    Attribute    β”‚                  Description                  β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Name            β”‚ Human-readable file name                      β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Identifier      β”‚ Unique number in file system (inode number)   β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Type            β”‚ Regular file, directory, symlink, device, etc β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Location        β”‚ Storage location on disk (block pointers)     β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Size            β”‚ File size in bytes                            β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Protection      β”‚ Access permissions (rwxrwxrwx)                β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Owner           β”‚ File owner (UID)                              β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Group           β”‚ File group (GID)                              β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Timestamps      β”‚ Creation, modification, access time           β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2.2 Unix/Linux stat Structure

#include <sys/stat.h>
#include <time.h>

// stat structure (stores file attributes)
struct stat {
    dev_t     st_dev;      // Device ID
    ino_t     st_ino;      // inode number
    mode_t    st_mode;     // File mode (type + permissions)
    nlink_t   st_nlink;    // Number of hard links
    uid_t     st_uid;      // Owner UID
    gid_t     st_gid;      // Group GID
    dev_t     st_rdev;     // Device ID (special files)
    off_t     st_size;     // Total size in bytes
    blksize_t st_blksize;  // I/O block size
    blkcnt_t  st_blocks;   // Number of 512B blocks allocated

    // Timestamps
    struct timespec st_atim;  // Last access time
    struct timespec st_mtim;  // Last modification time
    struct timespec st_ctim;  // Last status change time
};

// File attribute query example
void print_file_info(const char* path) {
    struct stat sb;

    if (stat(path, &sb) == -1) {
        perror("stat");
        return;
    }

    printf("File: %s\n", path);
    printf("inode: %lu\n", sb.st_ino);
    printf("Size: %ld bytes\n", sb.st_size);
    printf("Blocks: %ld\n", sb.st_blocks);
    printf("Permissions: %o\n", sb.st_mode & 0777);
    printf("Owner: %d\n", sb.st_uid);
    printf("Links: %lu\n", sb.st_nlink);

    // File type
    if (S_ISREG(sb.st_mode))  printf("Type: Regular file\n");
    if (S_ISDIR(sb.st_mode))  printf("Type: Directory\n");
    if (S_ISLNK(sb.st_mode))  printf("Type: Symbolic link\n");
    if (S_ISCHR(sb.st_mode))  printf("Type: Character device\n");
    if (S_ISBLK(sb.st_mode))  printf("Type: Block device\n");

    printf("Modified: %s", ctime(&sb.st_mtime));
}

2.3 Interpreting ls -l Output

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     ls -l Output Analysis                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   $ ls -l                                                               β”‚
β”‚   -rw-r--r-- 1 user group 4096 Jan 15 10:30 document.txt               β”‚
β”‚   β”‚β”œβ”€β”€β”€β”œβ”€β”€β”‚  β”‚  β”‚     β”‚     β”‚      β”‚     β”‚     └── Filename            β”‚
β”‚   β”‚β”‚  β”‚ β”‚ β”‚  β”‚  β”‚     β”‚     β”‚      β”‚     └── Time                      β”‚
β”‚   β”‚β”‚  β”‚ β”‚ β”‚  β”‚  β”‚     β”‚     β”‚      └── Date                            β”‚
β”‚   β”‚β”‚  β”‚ β”‚ β”‚  β”‚  β”‚     β”‚     └── Size (bytes)                           β”‚
β”‚   β”‚β”‚  β”‚ β”‚ β”‚  β”‚  β”‚     └── Group                                        β”‚
β”‚   β”‚β”‚  β”‚ β”‚ β”‚  β”‚  └── Owner                                               β”‚
β”‚   β”‚β”‚  β”‚ β”‚ β”‚  └── Hard link count                                        β”‚
β”‚   β”‚β”‚  β”‚ β”‚ └── Others permissions                                        β”‚
β”‚   β”‚β”‚  β”‚ └── Group permissions                                           β”‚
β”‚   β”‚β”‚  └── Owner permissions                                             β”‚
β”‚   │└── File type                                                        β”‚
β”‚   └── - regular, d directory, l link, c char device, b block device    β”‚
β”‚                                                                          β”‚
β”‚   Permissions:                                                           β”‚
β”‚   r (4) = read                                                          β”‚
β”‚   w (2) = write                                                         β”‚
β”‚   x (1) = execute (for directory: access)                               β”‚
β”‚                                                                          β”‚
β”‚   Example: rw-r--r-- = 644 = Owner: rw, Group: r, Others: r            β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3. File Operations and System Calls

3.1 Basic File Operations

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        File Operations                                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚   Operation     β”‚                  Description                  β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ Create          β”‚ Create new file, add entry to directory      β”‚   β”‚
β”‚   β”‚ Open            β”‚ Prepare file access, return file descriptor  β”‚   β”‚
β”‚   β”‚ Read            β”‚ Read data from current position              β”‚   β”‚
β”‚   β”‚ Write           β”‚ Write data at current position               β”‚   β”‚
β”‚   β”‚ Seek            β”‚ Change current position in file              β”‚   β”‚
β”‚   β”‚ Close           β”‚ End file access, release resources           β”‚   β”‚
β”‚   β”‚ Delete          β”‚ Remove file, delete directory entry          β”‚   β”‚
β”‚   β”‚ Truncate        β”‚ Set file size to 0 (delete content)          β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                                          β”‚
β”‚   Additional operations:                                                 β”‚
β”‚   - Append: Add data to end of file                                     β”‚
β”‚   - Rename: Change file name                                            β”‚
β”‚   - Get/Set Attributes: Query/modify attributes                         β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3.2 System Call Examples

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

// Create and write file
void create_and_write(const char* path) {
    // O_CREAT: create if doesn't exist
    // O_WRONLY: write-only
    // O_TRUNC: delete existing content
    // 0644: permissions (rw-r--r--)
    int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
    if (fd == -1) {
        perror("open");
        exit(1);
    }

    const char* data = "Hello, File System!\n";
    ssize_t written = write(fd, data, strlen(data));

    if (written == -1) {
        perror("write");
    } else {
        printf("%zd bytes written\n", written);
    }

    close(fd);
}

// Read file
void read_file(const char* path) {
    int fd = open(path, O_RDONLY);
    if (fd == -1) {
        perror("open");
        return;
    }

    char buffer[1024];
    ssize_t bytes_read;

    // read() returns bytes read, 0 is EOF
    while ((bytes_read = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
        buffer[bytes_read] = '\0';
        printf("%s", buffer);
    }

    if (bytes_read == -1) {
        perror("read");
    }

    close(fd);
}

// File position seek
void seek_example(const char* path) {
    int fd = open(path, O_RDONLY);
    if (fd == -1) return;

    // Seek to end to check size
    off_t size = lseek(fd, 0, SEEK_END);
    printf("File size: %ld bytes\n", size);

    // Return to beginning
    lseek(fd, 0, SEEK_SET);

    // Move forward 10 bytes
    lseek(fd, 10, SEEK_CUR);

    char buffer[100];
    read(fd, buffer, 10);
    buffer[10] = '\0';
    printf("10 chars from offset 10: %s\n", buffer);

    close(fd);
}

// File copy implementation
int copy_file(const char* src, const char* dst) {
    int src_fd = open(src, O_RDONLY);
    if (src_fd == -1) return -1;

    int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (dst_fd == -1) {
        close(src_fd);
        return -1;
    }

    char buffer[4096];
    ssize_t bytes;

    while ((bytes = read(src_fd, buffer, sizeof(buffer))) > 0) {
        ssize_t written = write(dst_fd, buffer, bytes);
        if (written != bytes) {
            close(src_fd);
            close(dst_fd);
            return -1;
        }
    }

    close(src_fd);
    close(dst_fd);
    return 0;
}

int main() {
    create_and_write("test.txt");
    read_file("test.txt");
    seek_example("test.txt");
    copy_file("test.txt", "test_copy.txt");
    return 0;
}

3.3 Open File Table

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Open File Table Structure                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   Process A                Process B                                    β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚   β”‚ fd table         β”‚      β”‚ fd table         β”‚                       β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                       β”‚
β”‚   β”‚ 0: stdin         β”‚      β”‚ 0: stdin         β”‚                       β”‚
β”‚   β”‚ 1: stdout        β”‚      β”‚ 1: stdout        β”‚                       β”‚
β”‚   β”‚ 2: stderr        β”‚      β”‚ 2: stderr        β”‚                       β”‚
β”‚   β”‚ 3: ─────────────┐│      β”‚ 3: ─────────────┐│                       β”‚
β”‚   β”‚ 4: ────────────┐││      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”˜                       β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”Όβ”˜                        β”‚                        β”‚
β”‚                    β”‚β”‚                         β”‚                        β”‚
β”‚                    β–Όβ–Ό                         β”‚                        β”‚
β”‚   System-wide Open File Table                 β”‚                        β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚                        β”‚
β”‚   β”‚ Entry 1:                                β”‚ β”‚                        β”‚
β”‚   β”‚   - File offset: 100                    β”‚β—€β”˜                        β”‚
β”‚   β”‚   - Access mode: O_RDONLY               β”‚                          β”‚
β”‚   β”‚   - Reference count: 2                  β”‚                          β”‚
β”‚   β”‚   - inode pointer: ──────────────────────┼───┐                      β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚                      β”‚
β”‚   β”‚ Entry 2:                                β”‚   β”‚                      β”‚
β”‚   β”‚   - File offset: 500                    β”‚   β”‚                      β”‚
β”‚   β”‚   - Access mode: O_RDWR                 β”‚   β”‚                      β”‚
β”‚   β”‚   - Reference count: 1                  β”‚   β”‚                      β”‚
β”‚   β”‚   - inode pointer: ──────────────────────┼───┼───┐                  β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚   β”‚                  β”‚
β”‚                                                 β”‚   β”‚                  β”‚
β”‚   In-Memory inode Table                         β”‚   β”‚                  β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚   β”‚                  β”‚
β”‚   β”‚ inode 1234:                             β”‚β—€β”€β”€β”˜   β”‚                  β”‚
β”‚   β”‚   - File size: 4096                     β”‚       β”‚                  β”‚
β”‚   β”‚   - Disk block locations                β”‚       β”‚                  β”‚
β”‚   β”‚   - Permissions, owner, etc.            β”‚       β”‚                  β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€       β”‚                  β”‚
β”‚   β”‚ inode 5678:                             β”‚β—€β”€β”€β”€β”€β”€β”€β”˜                  β”‚
β”‚   β”‚   - File size: 8192                     β”‚                          β”‚
β”‚   β”‚   - ...                                 β”‚                          β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                          β”‚
β”‚                                                                          β”‚
β”‚   After fork(): fd table copied, open file entry shared (offset shared!)β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4. Directory Structures

4.1 Single-Level Directory

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Single-Level Directory                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚                         Root Directory                           β”‚   β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”‚
β”‚   β”‚ cat β”‚ bo β”‚ a β”‚ test β”‚ data β”‚ mail β”‚ cont β”‚ hex β”‚ records β”‚     β”‚   β”‚
β”‚   β””β”€β”€β”¬β”€β”€β”΄β”€β”€β”¬β”€β”΄β”€β”€β”¬β”€β”΄β”€β”€β”€β”¬β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”΄β”€β”€β”¬β”€β”€β”΄β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜     β”‚   β”‚
β”‚      β”‚     β”‚    β”‚     β”‚      β”‚      β”‚      β”‚     β”‚       β”‚           β”‚   β”‚
β”‚      β–Ό     β–Ό    β–Ό     β–Ό      β–Ό      β–Ό      β–Ό     β–Ό       β–Ό           β”‚   β”‚
β”‚     file  file file  file   file   file   file  file    file         β”‚   β”‚
β”‚                                                                          β”‚
β”‚   Problems:                                                              β”‚
β”‚   - Name collision: All users cannot use same names                     β”‚
β”‚   - Management difficulty: Hard to find files as count grows            β”‚
β”‚   - No grouping: Cannot organize related files                          β”‚
β”‚                                                                          β”‚
β”‚   Used in early OS (CP/M)                                                β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4.2 Two-Level Directory

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Two-Level Directory                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚                    Master File Directory (MFD)                           β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚            user1              user2              user3          β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                   β”‚                β”‚                 β”‚                  β”‚
β”‚                   β–Ό                β–Ό                 β–Ό                  β”‚
β”‚        User File Directory (UFD)                                         β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚
β”‚   β”‚ cat   test  a   β”‚ β”‚ cat   data  b   β”‚ β”‚ hex   mail     β”‚          β”‚
β”‚   β””β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜          β”‚
β”‚      β”‚      β”‚    β”‚       β”‚      β”‚    β”‚       β”‚      β”‚                  β”‚
β”‚      β–Ό      β–Ό    β–Ό       β–Ό      β–Ό    β–Ό       β–Ό      β–Ό                  β”‚
β”‚                                                                          β”‚
β”‚   Advantages:                                                            β”‚
β”‚   - Independent namespace per user                                       β”‚
β”‚   - user1's cat β‰  user2's cat                                           β”‚
β”‚                                                                          β”‚
β”‚   Disadvantages:                                                         β”‚
β”‚   - Difficult to share files between users                              β”‚
β”‚   - No subdirectories                                                    β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4.3 Tree-Structured Directory

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Tree-Structured Directory                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚                              /                                           β”‚
β”‚                              β”‚                                           β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚           β”‚                  β”‚                  β”‚                       β”‚
β”‚          bin               home               etc                       β”‚
β”‚           β”‚                  β”‚                  β”‚                       β”‚
β”‚     β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”                  β”‚
β”‚    ls          cat   β”‚       β”‚       β”‚   passwd   hosts                β”‚
β”‚                    user1   user2   user3                                β”‚
β”‚                      β”‚       β”‚       β”‚                                  β”‚
β”‚                 β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”  β”‚    documents                             β”‚
β”‚               docs     code  data     β”‚                                  β”‚
β”‚                β”‚         β”‚    β”‚    β”Œβ”€β”€β”΄β”€β”€β”                              β”‚
β”‚            β”Œβ”€β”€β”€β”Όβ”€β”€β”€β”   main.c  β”‚   report  notes                        β”‚
β”‚          a.txt b.txt         file.txt                                   β”‚
β”‚                                                                          β”‚
β”‚   Features:                                                              β”‚
β”‚   - Arbitrary depth subdirectories allowed                               β”‚
β”‚   - Absolute path: /home/user1/docs/a.txt                               β”‚
β”‚   - Relative path: ./docs/a.txt (from current directory)                β”‚
β”‚   - Current directory (.), parent directory (..)                        β”‚
β”‚                                                                          β”‚
β”‚   Used by most modern operating systems                                  β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4.4 Acyclic Graph Directory

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Acyclic Graph Directory                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚                              /                                           β”‚
β”‚                              β”‚                                           β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚           β”‚                  β”‚                  β”‚                       β”‚
β”‚          home              shared              β”‚                        β”‚
β”‚           β”‚                  β”‚                 β”‚                        β”‚
β”‚     β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”           project β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                        β”‚
β”‚   user1       user2          β”‚ β–²                                        β”‚
β”‚     β”‚           β”‚      β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”                                    β”‚
β”‚  myproj ───────────────│           β”‚                                    β”‚
β”‚                        β”‚      shared file                                β”‚
β”‚                   ourproj β”€β”€β”€β”€β”˜                                          β”‚
β”‚                                                                          β”‚
β”‚   File/directory sharing methods:                                       β”‚
β”‚                                                                          β”‚
β”‚   1. Hard Link:                                                          β”‚
β”‚      - Different name pointing to same inode                            β”‚
β”‚      - ln target link_name                                              β”‚
β”‚      - On delete, only link count decreases                             β”‚
β”‚      - Cannot use on directories (prevents cycles)                      β”‚
β”‚                                                                          β”‚
β”‚   2. Symbolic Link:                                                      β”‚
β”‚      - Stores path to another file                                      β”‚
β”‚      - ln -s target link_name                                           β”‚
β”‚      - Broken link if original deleted (dangling link)                  β”‚
β”‚      - Can use on directories                                           β”‚
β”‚                                                                          β”‚
β”‚   $ ln /shared/project myproj          # Hard link                      β”‚
β”‚   $ ln -s /shared/project myproj       # Symbolic link                  β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5. File Access Methods

5.1 Sequential Access

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Sequential Access                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   File                                                                   β”‚
β”‚   β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”                            β”‚
β”‚   β”‚ A β”‚ B β”‚ C β”‚ D β”‚ E β”‚ F β”‚ G β”‚ H β”‚ I β”‚ J β”‚                            β”‚
β”‚   β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜                            β”‚
β”‚             ↑                                                            β”‚
β”‚          Current position                                                β”‚
β”‚                                                                          β”‚
β”‚   Operations:                                                            β”‚
β”‚   - read(): read from current position, advance                         β”‚
β”‚   - write(): write at current position, advance                         β”‚
β”‚   - reset(): return to beginning                                        β”‚
β”‚                                                                          β”‚
β”‚   Features:                                                              β”‚
β”‚   - Originated from tape-based systems                                   β”‚
β”‚   - Commonly used in editors, compilers                                  β”‚
β”‚   - Suitable for log file processing                                     β”‚
β”‚                                                                          β”‚
β”‚   Example: Sequential read to end of file                               β”‚
β”‚   while (read(fd, &buf, size) > 0) {                                    β”‚
β”‚       process(buf);                                                      β”‚
β”‚   }                                                                      β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5.2 Direct/Random Access

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Direct Access                                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   File (logical blocks)                                                  β”‚
β”‚   Block:  0     1     2     3     4     5     6     7     8     9       β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”        β”‚
β”‚   β”‚     β”‚     β”‚     β”‚     β”‚     β”‚     β”‚     β”‚     β”‚     β”‚     β”‚        β”‚
β”‚   β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜        β”‚
β”‚           ↑                       ↑                                      β”‚
β”‚        Block 1                  Block 5                                  β”‚
β”‚         read                    read                                     β”‚
β”‚                                                                          β”‚
β”‚   Operations:                                                            β”‚
β”‚   - read(n): read block n                                               β”‚
β”‚   - write(n): write block n                                             β”‚
β”‚   - seek(n): move to block n                                            β”‚
β”‚                                                                          β”‚
β”‚   Features:                                                              β”‚
β”‚   - Disk-based systems                                                   β”‚
β”‚   - Suitable for databases                                               β”‚
β”‚   - Random position access                                               β”‚
β”‚                                                                          β”‚
β”‚   Example: Direct record access                                          β”‚
β”‚   #define RECORD_SIZE 100                                               β”‚
β”‚   lseek(fd, record_num * RECORD_SIZE, SEEK_SET);                        β”‚
β”‚   read(fd, &record, RECORD_SIZE);                                       β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5.3 Indexed Access

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       Indexed Access                                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   Index File                          Data File                          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚   β”‚ Key     β”‚Block Ptr β”‚                 β”‚ Block 0                  β”‚    β”‚
β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ "Apple" β”‚     7    │────────────────▢│ Block 7: Apple data     β”‚    β”‚
β”‚   β”‚ "Banana"β”‚     3    │────────────┐   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚ "Cherry"β”‚    12    │──────────┐ β”‚   β”‚ Block 3: Banana data    │◀───
β”‚   β”‚ "Date"  β”‚     5    │────────┐ β”‚ β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”‚
β”‚   β”‚  ...    β”‚   ...    β”‚        β”‚ β”‚ β”‚   β”‚ Block 12: Cherry data   β”‚β—€β”€β”€β”˜
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚ β”‚ β”‚   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                  β”‚ β”‚ β”‚   β”‚ Block 5: Date data      │◀──┐
β”‚                                  β”‚ β”‚ └──▢│                         β”‚   β”‚
β”‚                                  β”‚ β”‚     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                  β”‚ β”‚                                    β”‚
β”‚                                  β””β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚                                    β”‚
β”‚                                    └── (Find location from index, direct access)
β”‚
β”‚   Features:                                                              β”‚
β”‚   - Fast search in large files                                          β”‚
β”‚   - Multi-level index if index itself too large                         β”‚
β”‚   - Database B+tree index                                               β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

6. File System Mounting

6.1 Mount Concept

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         File System Mounting                             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                          β”‚
β”‚   Before mount:                                                          β”‚
β”‚                                                                          β”‚
β”‚   Root file system (/)          USB drive                               β”‚
β”‚           /                          Independent tree                    β”‚
β”‚          β•±β”‚β•²                              β”‚                             β”‚
β”‚        bin home etc                      β•± β•²                            β”‚
β”‚             β”‚                         photo  doc                         β”‚
β”‚           user1                                                          β”‚
β”‚            β”‚                                                             β”‚
β”‚          mnt (empty directory)                                           β”‚
β”‚                                                                          β”‚
β”‚   After mount (mount /dev/sdb1 /home/user1/mnt):                        β”‚
β”‚                                                                          β”‚
β”‚           /                                                              β”‚
β”‚          β•±β”‚β•²                                                            β”‚
β”‚        bin home etc                                                      β”‚
β”‚             β”‚                                                            β”‚
β”‚           user1                                                          β”‚
β”‚            β”‚                                                             β”‚
β”‚          mnt ◀─────────── Mount point                                   β”‚
β”‚          β•± β•²                                                            β”‚
β”‚       photo  doc   ← USB drive contents visible here                    β”‚
β”‚                                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

6.2 Mount Commands

# Check mount status
$ mount
/dev/sda1 on / type ext4 (rw,relatime)
/dev/sdb1 on /mnt/usb type vfat (rw,user)

# Mount USB drive
$ sudo mount /dev/sdb1 /mnt/usb

# Mount with options
$ sudo mount -t ext4 -o ro,noexec /dev/sdc1 /mnt/backup

# Mount options:
# ro: read-only
# rw: read/write
# noexec: cannot execute programs
# nosuid: ignore setuid
# user: allow normal users to mount

# Unmount
$ sudo umount /mnt/usb

# /etc/fstab - automatic mount at boot
# Device            Mount point     Type    Options            Dump Pass
/dev/sda1          /               ext4    defaults           1    1
/dev/sda2          /home           ext4    defaults           1    2
/dev/sdb1          /mnt/data       ext4    defaults,nofail    0    2
UUID=xxxx-xxxx     /mnt/usb        vfat    user,noauto        0    0

Practice Problems

Problem 1: File Attribute Interpretation

Interpret each field from the following ls -l output:

-rwxr-x--- 2 alice developers 8192 Mar 15 14:30 script.sh
Show Answer
- : Regular file (d for directory, l for link)
rwx : Owner (alice) permissions - read/write/execute
r-x : Group (developers) permissions - read/execute
--- : Others permissions - none
2 : Hard link count (2 names for this file)
alice : Owner
developers : Owner group
8192 : File size (8KB)
Mar 15 14:30 : Last modification time
script.sh : File name

Numeric permissions: 750 (7=rwx, 5=r-x, 0=---)

Problem 2: System Call Sequence

Write the system call sequence to append "Hello World" to /tmp/log.txt.

Show Answer
int fd = open("/tmp/log.txt", O_WRONLY | O_APPEND | O_CREAT, 0644);
// O_APPEND: always write at end of file
// O_CREAT: create if doesn't exist
// 0644: permission setting

if (fd == -1) {
    perror("open failed");
    exit(1);
}

const char* msg = "Hello World\n";
ssize_t written = write(fd, msg, strlen(msg));

if (written == -1) {
    perror("write failed");
}

close(fd);

Explain the behavior of each link type in this scenario:

$ echo "original" > file.txt
$ ln file.txt hardlink.txt       # Hard link
$ ln -s file.txt symlink.txt     # Symbolic link
$ rm file.txt
$ cat hardlink.txt
$ cat symlink.txt
Show Answer
$ cat hardlink.txt
original
β†’ Hard link points to same inode
β†’ Data still exists even after file.txt deleted
β†’ Link count just decreased by 1

$ cat symlink.txt
cat: symlink.txt: No such file or directory
β†’ Symbolic link stores path "file.txt"
β†’ Broken link (dangling link) after file.txt deleted
β†’ Target doesn't exist, so error

Hard link characteristics:
- Shares same inode number
- Equal status with original
- Cannot use on directories

Symbolic link characteristics:
- Separate inode (stores path string)
- Dependent on original
- Can use on directories
- Can point to different file systems

Problem 4: Directory Navigation

Canonicalize the path /home/user/docs/../code/./main.c.

Show Answer
/home/user/docs/../code/./main.c

1. Navigate to /home/user/docs
2. .. goes up: /home/user
3. Navigate to code: /home/user/code
4. . is current directory (ignore): /home/user/code
5. main.c file: /home/user/code/main.c

Canonicalized path: /home/user/code/main.c

Verify in Linux:
$ realpath /home/user/docs/../code/./main.c
/home/user/code/main.c

Problem 5: Open File Table

Process A opens a file and creates process B via fork(). If both processes write 100 bytes each to the same file, what is the final file size?

Show Answer
After fork(), child copies parent's fd table.
Both processes' fds share the same open file entry.
Therefore they share the same offset!

Scenario:
1. Parent open(), offset = 0
2. fork(), child shares same open file entry
3. Parent write(100), offset = 100
4. Child write(100), offset = 200 (continues writing)

Final file size: 200 bytes

If child had separately open()ed:
- Separate open file entry created
- Separate offset
- Both starting from 0 would overwrite!
- Final size: 100 bytes

Note: In reality, results may vary based on write() atomicity
and buffering. O_APPEND usage recommended.

Next Steps

Continue to 17_File_System_Implementation.md to learn file system internals!


References

  • Silberschatz, "Operating System Concepts" Chapter 13
  • Linux man pages: open(2), read(2), write(2), stat(2)
  • POSIX file system standards
  • The Linux Programming Interface by Michael Kerrisk
to navigate between lessons