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

(user-readable name → inode number)

🔷 Example

("foo", 10)
  • foo → filename (user sees)

  • 10 → inode (OS uses)


🔷 Directory Structure

  • Directories can contain:

    • Files

    • Other directories

👉 Forms a hierarchical tree


🌳 Directory Tree Example

/ ├── foo │ └── bar.txt └── bar └── foo └── bar.txt




🔷 Path Names

Absolute Path

  • Starts from root /

/foo/bar.txt

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:

int fd = open("foo", O_CREAT | O_WRONLY | O_TRUNC);

🔷 Meaning of Flags

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

  1. OS checks if file exists

  2. If not → creates new file

  3. Allocates:

    • inode

    • directory entry

  4. 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

ProcessFile Descriptor → File Object → Disk Data

8. Old Method: creat()

int fd = creat("foo");

👉 Equivalent to:

open("foo", O_CREAT | O_WRONLY | O_TRUNC)

9. Important OS Perspective Points

     OS Responsibilities

  • Maintain mapping:

    name → inode → disk blocks
  • 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

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