Directory Operations in Operating Systems
Directory Operations in Operating Systems
Directories are special structures used to organize files. Unlike regular files, you cannot directly write to directories because they store file system metadata.
👉 Instead, directories are modified indirectly through system calls.
1. Creating Directories
System Call: mkdir()
-
Used to create a new directory
-
Syntax:
Example
Internally:
What Happens When a Directory is Created?
Even an “empty” directory contains:
| Entry | Meaning |
|---|---|
. | Refers to itself |
.. | Refers to parent directory |
👉 These ensure:
-
Navigation in file system
-
Hierarchical structure
2. Reading Directories
Directories cannot be read using normal read() calls. Instead, special APIs are used.
System Calls:
-
opendir() -
readdir() -
closedir()
Basic Workflow
Directory Entry Structure
Key Idea
👉 Directory only stores:
-
Filename
-
Inode number
For more details (size, permissions), use:
-
stat()system call
Real-world Example
Command:
-
Uses
readdir()internally -
ls -ladditionally usesstat()for metadata
3. Deleting Directories
System Call: rmdir()
-
Used to remove a directory
-
Condition: Directory must be empty
Example
Important Constraint
👉 Directory must contain only:
-
. -
..
Otherwise:
-
❌
rmdir()fails
Why Restriction Exists?
-
Prevents accidental deletion of large data
-
Safer than file deletion
Important Warning (Real System Behavior)
Command:
👉 Recursively deletes:
-
All files
-
All directories
⚠️ If run at root (/):
-
Entire system data can be erased
Summary Table
| Operation | System Call | Key Idea |
|---|---|---|
| Create directory | mkdir() | Creates with . and .. |
| Read directory | opendir(), readdir() | Iterates entries |
| Delete directory | rmdir() | Only if empty |
Key Concept for Students
👉 Directories are not normal files:
-
Cannot be directly modified
-
Managed only via OS-controlled operations
👉 They act as:
Comments
Post a Comment