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

off_t lseek(int fd, off_t offset, int whence);

🔷 Arguments

ArgumentMeaning
fd        File descriptor
offset        Position to move
whence        Reference point

🔷 whence Options

OptionMeaning
SEEK_SET            From beginning of file
SEEK_CUR            From current position
SEEK_END            From end of file

🔷 Example

lseek(fd, 100, SEEK_SET);

👉 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

  1. Implicit update

    • After read() / write()

    • Offset increases automatically

  2. Explicit update

    • Using lseek()


🔷 Execution Flow

open → lseek → read/write → close

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:

write(fd, data, size);

👉 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

fsync(int fd);

👉 Forces all buffered data to be written to disk immediately


🔷 How it Works

  1. Write data → goes to buffer

  2. Call fsync()

  3. OS writes data to disk

  4. Returns only after completion


🔷 Example

int fd = open("foo", O_CREAT | O_WRONLY | O_TRUNC); write(fd, buffer, size); fsync(fd);

🔷 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:

fsync(directory_fd);

👉 Ensures:

  • File metadata (directory entry) is also saved


9. Comparison

OperationBehavior
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

👉 Trade-off in OS design:
  • Buffering (write()) → performance

  • fsync()reliability

Comments

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Operating Systems PCCST403 Scheme and Syllabus