os_revision.md

🖥️ 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.

FeatureProcessThread
MemorySeparate address spaceShared within process
CommunicationIPC (pipes, sockets)Shared memory (fast)
OverheadHeavyLightweight
Crash isolationIndependentOne crash can kill all
Creation timeSlowFast

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

PreemptiveNon-Preemptive
CPU taken mid-executionYesNo
ExamplesRound Robin, SRTFFCFS, SJF
Response timeBetterWorse
Context switch overheadHigherLower
Starvation riskLowerHigher

🔴 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):

ConditionMeaning
Mutual ExclusionResource can only be held by one process at a time
Hold and WaitA process holds one resource and waits for another
No PreemptionResources cannot be forcibly taken — must be released voluntarily
Circular WaitP1 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).
PagingSegmentation
Block sizeFixed (e.g., 4KB)Variable
FragmentationInternalExternal
ViewPhysicalLogical (user-friendly)
Address translationPage tableSegment 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.
FeatureMutexSemaphore
ValueBinary (0/1)Integer (0 to N)
OwnerSame thread must unlockAny thread can signal
Use caseMutual exclusionResource counting / sync
AnalogyBathroom keyParking 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)

  1. What are the 4 Coffman conditions for deadlock?
  2. Why is Round Robin preemptive?
  3. What is starvation and how is it prevented?
  4. Difference between process and thread in terms of memory?
  5. What is the convoy effect in FCFS?
  6. Mutex vs Semaphore — which allows multiple threads?
  7. What is internal fragmentation? Which memory scheme causes it?
  8. What is a page fault?
  9. Can a deadlock happen with a mutex? How?
  10. What does aging do in scheduling?

Answers

  1. Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait
  2. The OS forcibly takes the CPU away after the time quantum expires — the process did not yield voluntarily
  3. Starvation = low-priority process never gets CPU; prevented by Aging (gradually increasing priority)
  4. Processes have separate address spaces; threads share heap/globals but have separate stacks
  5. A long process at the front blocks all shorter ones behind it — like a slow truck on a single-lane road
  6. Semaphore (can be N greater than 1)
  7. Unused space within an allocated fixed-size block; caused by Paging
  8. Accessing a page not in RAM — OS fetches it from disk; too many = thrashing
  9. Yes — Thread A holds M1 waiting for M2; Thread B holds M2 waiting for M1 — circular wait
  10. Gradually increases priority of waiting processes to prevent starvation