Non-Sequential File Access
Non-Sequential File Access
🔷 1. What is Non-Sequential Access?
So far:
-
Sequential access → read/write from beginning to end
👉 Now:
-
Non-sequential (random) access → read/write at any position in the file
🔷 Why is it Needed?
Used in applications like:
-
Databases
-
File indexing
-
Large file processing
👉 Example:
-
Jump directly to a specific record in a file
2. The lseek() System Call
🔷 Prototype
🔷 Arguments
| Argument | Meaning |
|---|---|
fd | File descriptor |
offset | Position to move |
whence | Reference point |
🔷 whence Options
| Option | Meaning |
|---|---|
SEEK_SET | From beginning of file |
SEEK_CUR | From current position |
SEEK_END | From end of file |
🔷 Example
👉 Moves file pointer to byte 100
3. How Non-Sequential Access Works
🔷 Important Concept: File Offset
👉 Each open file has a current offset
-
Determines where next read/write happens
-
Maintained by OS
🔷 Offset Updates
-
Implicit update
-
After
read()/write() -
Offset increases automatically
-
-
Explicit update
-
Using
lseek()
-
🔷 Execution Flow
4. Important Clarification
🔴 lseek() ≠ Disk Seek
👉 Very important exam point:
-
lseek():-
Only changes a variable in OS memory
-
NO disk I/O happens
-
-
Disk seek:
-
Physical movement of disk head
-
🔷 But…
👉 Random access using lseek() can cause disk seeks later
5. Key Insight
-
Sequential access → fast
-
Non-sequential access → may cause:
-
More disk seeks
-
Higher latency
-
6. Writing Immediately with fsync()
🔷 Problem with write()
When you call:
👉 Data is NOT immediately written to disk
Instead:
-
Stored in OS buffer (cache)
-
Written later (for performance)
🔷 Risk
If system crashes:
-
Data in buffer is lost
7. Solution: fsync()
🔷 Definition
👉 Forces all buffered data to be written to disk immediately
🔷 How it Works
-
Write data → goes to buffer
-
Call
fsync() -
OS writes data to disk
-
Returns only after completion
🔷 Example
🔷 Guarantee
👉 After fsync():
-
Data is persisted on disk
-
Safe from crashes
8. Important Subtle Point
👉 fsync() on file is NOT always enough
You may also need:
👉 Ensures:
-
File metadata (directory entry) is also saved
9. Comparison
| Operation | Behavior |
|---|---|
write() | Buffered (delayed write) |
fsync() | Immediate disk write |
lseek() | Changes file offset only |
10. Key Takeaways
-
Non-sequential access uses
lseek() -
File offset determines read/write position
-
lseek()does NOT perform disk I/O -
write()is buffered -
fsync()ensures data durability
-
Buffering (
write()) → performance -
fsync()→ reliability
Comments
Post a Comment