🖥️ OS Revision — MathWorks EDG Interview Prep
⏱ Time-boxed for spoken answers under 30 seconds each 🔴 = HIGHEST PRIORITY (answer these first if time is short)
🔴 1. Process vs Thread
One-line definition:
A process is an independent program in execution with its own memory; a thread is a lightweight unit of execution inside a process that shares memory with other threads.
Key Point / Mechanism:
- Processes are isolated — if one crashes, others are unaffected. Think of processes as separate houses.
- Threads are like rooms inside that house — they share kitchen, electricity (heap/globals), but each has its own bedroom (stack/registers).
- Creating a thread is faster and cheaper than creating a new process because no new memory space is allocated.
Common Follow-up:
"What''s a race condition?" When two threads access shared data simultaneously and the final result depends on the order of execution — solved using locks/mutexes.
| Feature | Process | Thread |
|---|---|---|
| Memory | Separate address space | Shared within process |
| Communication | IPC (pipes, sockets) | Shared memory (fast) |
| Overhead | Heavy | Lightweight |
| Crash isolation | Independent | One crash can kill all |
| Creation time | Slow | Fast |
🔴 2. Process Scheduling Algorithms
2a. FCFS — First Come First Served
One-line definition:
Processes are executed in the order they arrive — like a regular queue at a bank.
Key Point / Mechanism:
- Non-preemptive: once a process starts, it runs to completion.
- Simple but suffers from the Convoy Effect — a slow/long process at the front makes everyone behind it wait.
- Average waiting time can be very high.
Common Follow-up:
"What''s the convoy effect?" A long CPU-burst process holds the CPU, causing shorter processes to wait unnecessarily — similar to a slow truck on a single-lane road.
2b. Round Robin (RR)
One-line definition:
Each process gets a fixed time quantum on the CPU; if it is not done, it is preempted and goes to the back of the queue.
Key Point / Mechanism:
- Why it is preemptive: The OS forcibly takes the CPU away when the time quantum expires — the process did not voluntarily give it up.
- Think of it like a round-table dinner — everyone gets exactly 5 minutes to eat, then passes the plate.
- Best for time-sharing systems; response time is predictable.
Common Follow-up:
"What happens if the quantum is too large or too small?" Too large degenerates into FCFS. Too small causes too many context switches, high overhead.
2c. Preemptive vs Non-Preemptive Scheduling
One-line definition:
Preemptive: the OS can interrupt a running process. Non-preemptive: the process runs until it voluntarily releases the CPU.
Key Point / Mechanism:
- Preemptive schedulers (Round Robin, SRTF) are better for interactive/real-time systems.
- Non-preemptive (FCFS, SJF) are simpler but can cause starvation.
- Starvation = a low-priority process never gets CPU time because higher-priority processes keep coming. Analogy: you are always at the back of the checkout line.
Common Follow-up:
"How do you prevent starvation?" Aging — gradually increase the priority of a waiting process the longer it waits.
| Preemptive | Non-Preemptive | |
|---|---|---|
| CPU taken mid-execution | Yes | No |
| Examples | Round Robin, SRTF | FCFS, SJF |
| Response time | Better | Worse |
| Context switch overhead | Higher | Lower |
| Starvation risk | Lower | Higher |
🔴 3. Deadlock
One-line definition:
Deadlock is a situation where a set of processes are all waiting for resources held by each other, so none can proceed — a circular wait freeze.
Key Point / Mechanism — 4 Necessary Conditions (Coffman's):
| Condition | Meaning |
|---|---|
| Mutual Exclusion | Resource can only be held by one process at a time |
| Hold and Wait | A process holds one resource and waits for another |
| No Preemption | Resources cannot be forcibly taken — must be released voluntarily |
| Circular Wait | P1 waits for P2, P2 waits for P3, P3 waits for P1 — cycle |
Memory trick: My House Never Crashes — Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait
Prevention Method:
Break Circular Wait by imposing a strict ordering on resource acquisition — all processes must request resources in the same numbered order, so a cycle can never form.
Common Follow-up:
"Difference between deadlock prevention and avoidance?" Prevention: structurally ensure at least one Coffman condition is violated. Avoidance: dynamically check if granting a resource keeps the system in a safe state (Banker Algorithm).
4. Paging vs Segmentation
One-line definition:
Paging divides memory into fixed-size blocks (pages); segmentation divides it into logical, variable-size sections (code, stack, data).
Key Point / Mechanism:
- Paging eliminates external fragmentation but causes internal fragmentation (unused space within a page).
- Segmentation matches programmer view (logical segments) but causes external fragmentation.
- Modern OSes often use both (segmented paging).
| Paging | Segmentation | |
|---|---|---|
| Block size | Fixed (e.g., 4KB) | Variable |
| Fragmentation | Internal | External |
| View | Physical | Logical (user-friendly) |
| Address translation | Page table | Segment table |
Common Follow-up:
"What is a page fault?" When a process accesses a page not currently in RAM — the OS loads it from disk. Too many page faults leads to thrashing.
🔴 5. Semaphore vs Mutex
One-line definition:
A mutex is a lock for exactly ONE thread at a time; a semaphore is a signaling counter that allows N threads simultaneously.
Key Point / Mechanism:
- Mutex = binary (0 or 1). Only the thread that locked it can unlock it — like a bathroom key.
- Semaphore = integer counter. Can be greater than 1. One thread signals another to proceed — like a parking lot with N slots.
- Semaphores are used for synchronization (producer-consumer); mutexes for mutual exclusion.
| Feature | Mutex | Semaphore |
|---|---|---|
| Value | Binary (0/1) | Integer (0 to N) |
| Owner | Same thread must unlock | Any thread can signal |
| Use case | Mutual exclusion | Resource counting / sync |
| Analogy | Bathroom key | Parking lot counter |
Common Follow-up:
"Can a mutex cause a deadlock?" Yes — if Thread A holds Mutex1 and waits for Mutex2, while Thread B holds Mutex2 and waits for Mutex1.
OS Rapid-Fire Self-Test
(Try to answer each in one breath)
- What are the 4 Coffman conditions for deadlock?
- Why is Round Robin preemptive?
- What is starvation and how is it prevented?
- Difference between process and thread in terms of memory?
- What is the convoy effect in FCFS?
- Mutex vs Semaphore — which allows multiple threads?
- What is internal fragmentation? Which memory scheme causes it?
- What is a page fault?
- Can a deadlock happen with a mutex? How?
- What does aging do in scheduling?
Answers
- Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait
- The OS forcibly takes the CPU away after the time quantum expires — the process did not yield voluntarily
- Starvation = low-priority process never gets CPU; prevented by Aging (gradually increasing priority)
- Processes have separate address spaces; threads share heap/globals but have separate stacks
- A long process at the front blocks all shorter ones behind it — like a slow truck on a single-lane road
- Semaphore (can be N greater than 1)
- Unused space within an allocated fixed-size block; caused by Paging
- Accessing a page not in RAM — OS fetches it from disk; too many = thrashing
- Yes — Thread A holds M1 waiting for M2; Thread B holds M2 waiting for M1 — circular wait
- Gradually increases priority of waiting processes to prevent starvation