Reading and Writing Files (OS Perspective)

 

Reading and Writing Files (OS Perspective)

🔷 1. Basic Idea

Once a file is created, the OS provides system calls to:

  • Read data from a file

  • Write data to a file

👉 These operations are typically sequential:

  • Data is processed from beginning to end, byte by byte


2. Reading Files Sequentially

🔷 Example Flow (cat foo)

When you run:

cat foo

The OS performs the following steps:


🔹 Step 1: Open the File

open("foo", O_RDONLY)
  • Opens file in read-only mode

  • Returns a file descriptor (fd)

👉 Example:

fd = 3

🔹 Why fd = 3?

Because:

  • 0 → Standard Input

  • 1 → Standard Output

  • 2 → Standard Error

👉 First available descriptor = 3


🔹 Step 2: Read Data

read(fd, buffer, size)
  • fd → which file

  • buffer → where data is stored

  • size → how much to read

👉 Example:

read(3, "hello\n", 4096) = 6
  • Reads 6 bytes

  • Includes newline \n


🔹 Step 3: Write to Output

write(1, "hello\n", 6)
  • 1 = standard output (screen)

👉 Displays:

hello

🔹 Step 4: Repeat Until EOF

read(3, "", 4096) = 0

👉 Return value 0 = End of File (EOF)


🔹 Step 5: Close File

close(3)

👉 Releases file descriptor


Sequential Reading Summary

openreadread → ... → read(0) → close

👉 Data is read in order, not randomly


 3. Writing Files Sequentially

🔷 Steps

  1. Open file for writing:

open("foo", O_WRONLY)
  1. Write data:

write(fd, buffer, size)
  1. Repeat if needed

  2. Close file:

close(fd)

Sequential Writing Flow

open → write → write → ... → close

👉 Data is written continuously in sequence


4. What is strace?

🔷 Definition

strace is a system call tracing tool.

👉 It shows:

  • What system calls a program makes

  • Arguments passed

  • Return values


🔷 Why is it Useful?

  • Understand OS behavior

  • Debug programs

  • Learn how programs interact with kernel


🔷 Example

strace cat foo

Output (simplified):

open("foo", O_RDONLY) = 3 read(3, "hello\n", 4096) = 6 write(1, "hello\n", 6) = 6 read(3, "", 4096) = 0 close(3) = 0

5. Key Observations from strace

🔹 1. Everything is a system call

  • File operations go through OS


🔹 2. File descriptor usage

  • Identifies which file is accessed


🔹 3. Sequential behavior

  • Repeated read() calls until EOF


🔹 4. Output handling

  • write() to fd = 1 → screen


 6. Important Concepts

🔷 End of File (EOF)

  • read() returns 0

  • Indicates no more data


🔷 Buffering

  • Data is read in chunks (e.g., 4KB)

  • Improves efficiency


🔷 Abstraction

  • User sees simple commands (cat)

  • OS handles complexity internally


 7. Key Takeaways

  • Reading:

    open → read → close
  • Writing:

    open → write → close
  • File descriptor:

    • 0 → input

    • 1 → output

    • 2 → error

  • strace:

    • Tracks system calls

    • Helps understand OS internals

👉 Sequential file access is efficient because:
  • Minimal seek operations

  • Continuous data flow

👉 strace reveals:

How high-level programs interact with the OS at a low level

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