Files and Directories in OS
Files and Directories in OS
1. Why Do We Need Them?
So far, the OS provides:
-
Process → abstraction of CPU
-
Address space → abstraction of memory
👉 Now we add:
-
Persistent storage abstraction
💡 Unlike memory:
-
Memory → temporary (lost on power off)
-
Disk → permanent storage
2. File: Basic Abstraction
🔷 What is a File?
A file is:
👉 A linear array of bytes that can be read or written
🔷 Key Characteristics
-
Stores data persistently on disk
-
OS does not care about file content type
-
Could be text, image, video, etc.
-
-
OS only ensures:
👉 Data written = Data read later
🔷 Inode (Low-Level Name)
-
Each file has a unique identifier:
👉 inode number
💡 Important:
-
Users see names like
file.txt -
OS internally uses inode numbers
3. Directory: Naming Mechanism
🔷 What is a Directory?
A directory is:
👉 A special file that stores mappings
🔷 Example
-
foo→ filename (user sees) -
10→ inode (OS uses)
🔷 Directory Structure
-
Directories can contain:
-
Files
-
Other directories
-
👉 Forms a hierarchical tree
🌳 Directory Tree Example
🔷 Path Names
Absolute Path
-
Starts from root
/
Key Point
-
Same filename can exist in different directories
4. Key Insight
👉 File system provides:
-
Naming
-
Organization
-
Access mechanism
💡 Everything in UNIX is treated like a file:
-
Devices
-
Pipes
-
Processes
5. File System Interface (OS View)
The OS provides system calls to:
-
Create files
-
Read/write files
-
Delete files
6. Creating Files in OS
🔷 System Call: open()
File creation is done using:
🔷 Meaning of Flags
| Flag | Meaning |
|---|---|
O_CREAT | Create file if it doesn’t exist |
O_WRONLY | Open for writing only |
O_TRUNC | Clear existing content |
🔷 What Happens Internally?
When open() is called:
-
OS checks if file exists
-
If not → creates new file
-
Allocates:
-
inode
-
directory entry
-
-
Returns a file descriptor
7. File Descriptor (Important Concept)
🔷 What is it?
A file descriptor (fd) is:
👉 An integer used by the process to access the file
🔷 Key Features
-
Unique per process
-
Acts like a handle or pointer
-
Used in:
-
read() -
write() -
close()
-
🔷 Conceptual View
8. Old Method: creat()
👉 Equivalent to:
9. Important OS Perspective Points
OS Responsibilities
-
Maintain mapping:
-
Ensure:
-
Persistence
-
Reliability
-
Correct data retrieval
-
OS Does NOT:
-
Interpret file content
-
Enforce file type (.txt, .jpg, etc.)
10. Key Takeaways
-
File = linear sequence of bytes
-
Directory = mapping of name → inode
-
Inode = internal file identifier
-
File descriptor = handle to access file
-
open()is used to create files
Final Insight
👉 File system provides abstraction + naming + persistence, making storage:
-
Easy to use
-
Organized
-
Reliable

Comments
Post a Comment