ποΈ Operating Systems: File Systems & IPC
The Library of Your Computer
Imagine your computer is a giant library. Every book, every picture, every song you save is stored in this library. But how does the library know where everything is? How do the librarians (programs) talk to each other when they need help? Thatβs what weβll discover today!
π What is a File System?
Think of a file system as the organizing system for your library. Without it, books would be scattered everywhereβon the floor, on tables, in random piles. Nobody could find anything!
The Three Big Jobs of a File System
- Names things β Every file gets a name, like βbirthday_photo.jpgβ
- Remembers where things are β Tracks exactly which shelf (disk location) holds each file
- Keeps things safe β Makes sure nobody erases your homework by accident!
βββββββββββββββββββββββββββββββ
β YOUR COMPUTER β
β βββββββββββββββββββββββ β
β β FILE SYSTEM β β
β β βββββ βββββ βββββ β β
β β βπβ βπΌοΈβ βπ΅β β β
β β βββββ βββββ βββββ β β
β β Names + Locations β β
β βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββ
Real Life Example:
- When you save a game, the file system picks a spot on your hard drive
- It writes down: βGame save is at location 1234β
- Later, when you load the game, it looks up location 1234 instantly!
π§ File Operations: What Can You Do With Files?
Just like you can do things with library books, you can do things with files!
The Six Magic Actions
| Action | What It Does | Library Example |
|---|---|---|
| CREATE | Make a new file | Getting a brand new blank notebook |
| OPEN | Start using a file | Taking a book off the shelf |
| READ | Look at whatβs inside | Reading the book pages |
| WRITE | Add or change content | Writing in your notebook |
| CLOSE | Finish using the file | Putting the book back |
| DELETE | Remove the file | Throwing the book away |
How Opening a File Works
graph TD A["You click 'Open'"] --> B["OS finds file location"] B --> C["OS reads from disk"] C --> D["Data loaded into memory"] D --> E["You see your file!"]
Example: Opening a photo
- You double-click βvacation.jpgβ
- File system looks up: βvacation.jpg is at disk sector 5678β
- It reads that sector into your computerβs fast memory
- Photo app displays your picture!
π³ Directory Structure: Folders Inside Folders
A directory is just a fancy word for folder. And folders can contain other foldersβlike Russian nesting dolls! πͺ
The Tree Shape
Your computerβs files form a tree shape, starting from one main folder called the root.
/ (root - the very top)
βββ π Users
β βββ π Alice
β β βββ π homework.doc
β β βββ π Photos
β β βββ πΌοΈ cat.jpg
β βββ π Bob
β βββ π΅ song.mp3
βββ π Programs
β βββ π± games
βββ π System
Absolute vs Relative Paths
Think of paths like addresses:
| Path Type | What It Means | Example |
|---|---|---|
| Absolute | Full address from root | /Users/Alice/Photos/cat.jpg |
| Relative | Directions from where you ARE | Photos/cat.jpg (if youβre in Aliceβs folder) |
Simple Example:
- Absolute path: β123 Main Street, New York, USAβ
- Relative path: βGo two blocks left, then turn rightβ
πΎ File Allocation Methods: Where Do Files Live?
When you save a file, the computer needs to find empty space on the disk. There are three main ways to do this!
1. Contiguous Allocation (All Together)
Like reserving seats next to each other at a movie theater. π¬
Disk: [FILE A][FILE A][FILE A][empty][empty][FILE B][FILE B]
βββββββββββββββββββββββ
File A lives in seats 0-2
β Good: Super fast to read (everythingβs together!) β Bad: Hard to grow files, creates gaps (fragmentation)
2. Linked Allocation (Follow the Chain)
Each piece points to the next piece, like a treasure hunt! πΊοΈ
Block 5: [Data] β points to Block 12
Block 12: [Data] β points to Block 3
Block 3: [Data] β END
β Good: Files can be anywhere, easy to grow β Bad: Slow (must follow each link), one broken link = lost file!
3. Indexed Allocation (The Master List)
One special block holds a list of all locationsβlike a table of contents! π
βββββββββββββββββββ
β INDEX BLOCK β
β Block 0: loc 5 β
β Block 1: loc 12β
β Block 2: loc 3 β
βββββββββββββββββββ
β Good: Fast random access, no wasted pointers β Bad: Index block takes space, big files need multiple index blocks
graph TD subgraph Contiguous A1["Block 1"] --> A2["Block 2"] --> A3["Block 3"] end subgraph Linked B1["Block 5"] -.-> B2["Block 12"] -.-> B3["Block 3"] end subgraph Indexed C0["Index"] --> C1["Block 5"] C0 --> C2["Block 12"] C0 --> C3["Block 3"] end
π‘ Disk Scheduling: Who Gets the Disk Next?
Your hard disk has a read/write head that moves across the disk like a record player needle. πΏ
When multiple programs want to read/write at the same time, who goes first?
The Problem
Imagine the read head is at track 50. Three programs want:
- Program A: Track 20
- Program B: Track 100
- Program C: Track 30
Scheduling Algorithms
1. FCFS (First Come, First Served)
Serve requests in the order they arrive. Simple but not efficient!
Head at 50 β 20 β 100 β 30
Movement: 30 + 80 + 70 = 180 tracks total π
2. SSTF (Shortest Seek Time First)
Go to the closest request next. Greedy but faster!
Head at 50 β 30 β 20 β 100
Movement: 20 + 10 + 80 = 110 tracks total π
3. SCAN (The Elevator)
Move in one direction until the end, then reverse. Like an elevator!
Head at 50 β goes right to 100 β then left to 30 β 20
4. C-SCAN (Circular SCAN)
Go one direction, jump back to start, repeat. Always moving forward!
50 β 100 β [jump to 0] β 20 β 30
graph LR subgraph FCFS F1["50"] --> F2["20"] --> F3["100"] --> F4["30"] end subgraph SSTF S1["50"] --> S2["30"] --> S3["20"] --> S4["100"] end
Real Life: Think of SCAN like an elevatorβit doesnβt go up, down, up, down randomly. It goes all the way up, then all the way down!
π£οΈ Inter-Process Communication (IPC): Programs Talking to Each Other
When two programs need to share information, they use IPC. Itβs like how people communicate!
Why Do Programs Need to Talk?
- A web browser asks the download manager to fetch a file
- Your music player tells the volume control to go louder
- A gameβs graphics engine talks to the physics engine
The Six Ways Programs Communicate
1. Pipes (One-Way Tunnel) π
Data flows in one direction only, like water in a pipe.
[Program A] ββdataβββΊ [Program B]
Writer Reader
Example: ls | grep "txt" β the output of ls flows INTO grep
2. Named Pipes (FIFO) π¬
Like a pipe, but with a nameβany program that knows the name can use it!
[Any Program] βββΊ /tmp/my_pipe βββΊ [Any Program]
3. Shared Memory π¦
Two programs share the same chunk of memoryβthe fastest method!
βββββββββββββββββββββββββββββββ
β SHARED MEMORY β
β βββββββ βββββββ β
β βProg β ββββΊ βProg β β
β β A β β B β β
β βββββββ βββββββ β
βββββββββββββββββββββββββββββββ
β‘ Super fast! But need to be carefulβboth writing at once = chaos!
4. Message Queues π¨
Programs leave messages in a mailbox for others to pick up.
Program A drops message β [QUEUE: msg1, msg2] β Program B reads
Like: Leaving sticky notes on someoneβs desk
5. Semaphores π¦
A traffic light for programs. Controls who can access shared things.
Green (1): Go ahead and use the resource
Red (0): Wait, someone else is using it
Example: Only one program prints at a time, so pages donβt get mixed up!
6. Sockets π
Communication across the networkβeven between different computers!
[Your Computer] ββinternetββ [Server Computer]
Client Server
Example: Your browser (client) talks to google.com (server) using sockets!
π― Quick Comparison Table
| IPC Method | Speed | Across Network? | Best For |
|---|---|---|---|
| Pipes | Fast | β No | Parent-child processes |
| Named Pipes | Fast | β No | Unrelated processes |
| Shared Memory | β‘ Fastest | β No | Large data, same machine |
| Message Queues | Medium | β No | Async messaging |
| Semaphores | N/A | β No | Synchronization only |
| Sockets | Slower | β Yes | Network communication |
π You Did It!
Now you understand how your computer:
- Organizes files in a tree structure
- Stores files using allocation methods
- Schedules disk access efficiently
- Lets programs talk to each other
Think of your computer as a busy library with chatty librariansβand now you know exactly how they keep everything running smoothly! πβ¨
π§ Key Takeaways
- File System = The organizing system that names, locates, and protects your files
- File Operations = CREATE, OPEN, READ, WRITE, CLOSE, DELETE
- Directory Structure = Tree of folders starting from root (/)
- Allocation Methods = Contiguous (fast), Linked (flexible), Indexed (balanced)
- Disk Scheduling = FCFS, SSTF, SCAN, C-SCAN (like elevator algorithms)
- IPC = How programs communicate: pipes, shared memory, sockets, and more!
