∑GateAndNet
PlannerSign Up
ADVANCEDFrequently Tested 4 min read 5 views

OS 2027 Rank-Decider: Hardware CAS/TestAndSet, Priority Inversion, Copy-on-Write & File I/O

Why naive TestAndSet/CAS spinlocks violate Bounded Waiting, Priority Inversion & Priority Inheritance, Copy-on-Write (COW) fork() page sharing, and Contiguous vs Linked vs FAT vs Indexed disk block seeks.

1. Atomic Hardware Primitives: TestAndSet & CompareAndSwap (CAS)

c
// Atomic hardware instruction (executes indivisibly across CPU cores)
int compare_and_swap(int *word, int expected, int new_val) {
    int old = *word;
    if (old == expected) *word = new_val;
    return old;
}
  • Naive while (TestAndSet(&lock)) ; or while (compare_and_swap(&lock, 0, 1) != 0) ; guarantees Mutual Exclusion and Progress, EXCEPT it violates Bounded Waiting (under contention, one unlucky thread can starve indefinitely!). Bounded waiting requires a waiting array / ticket lock handoff.
  • Priority Inversion: High-priority PHP_H waits for a mutex held by low-priority PLP_L, while medium-priority CPU-bound PMP_M preempts PLP_L — effectively starving PHP_H! Solved by the Priority Inheritance Protocol (PLP_L temporarily inherits PHP_H’s priority until it releases the mutex).

2. fork() with Copy-on-Write (COW) Virtual Memory

When parent calls fork(), modern kernels do NOT copy physical RAM frames! Instead:

  1. Child receives a copy of the Page Table pointing to the exact same physical frames, and all writable private PTEs are marked Read-Only + COW.
  2. Read accesses by either parent or child incur zero page faults.
  3. The first write to shared page XX triggers a Protection Trap (Soft Page Fault): kernel allocates 11 new physical frame, copies the 4 KB4\text{ KB} page, updates the writer’s PTE to Read-Write, and clears COW once reference count drops to 11!
Key Formulas Summary
Disk Block Accesses to Read Block k of an Open File (Directory Entry in RAM)
Contiguous=1,Linked=k+1  (0-indexed),FAT (cached in RAM)=1,Single-Level Index (inode in RAM)=1\text{Contiguous} = 1, \qquad \text{Linked} = k + 1 \;(0\text{-indexed}), \qquad \text{FAT (cached in RAM)} = 1, \qquad \text{Single-Level Index (inode in RAM)} = 1
Note: Inserting a block in the middle of an N-block Contiguous file requires shifting (N - k) blocks!
#Operating Systems#TestAndSet#CompareAndSwap#Priority Inversion#Copy-on-Write#File Allocation

Practice Questions on This Chapter (2)

Q1GATE CS 2027 Expected NATNATMEDIUM
+2M • 0 Neg

A process owns 2020 virtual memory pages in RAM (1212 Read-Only code pages and 88 Read-Write data pages) and calls fork() on an OS with Copy-on-Write (COW) paging. Before exec(), the parent writes to 33 distinct data pages and the child writes to 22 data pages (11 of which is the same page written by the parent). How many total physical page frames are occupied by parent and child combined?

Q2GATE CS 2026–27 Expected MSQMSQHARD
+2M • 0 Neg

Which of the following statements about Multi-Core Process Synchronization are CORRECT?

☑️ Multiple Select Question — pick one or more options, then click Check Answer.
Previous TopicPage Replacement Logics (Animated): Belady’s Anomaly in FIFO vs LRU & Optimal Stack PropertyNext Topic Functional Dependencies, Candidate Keys & Normalization (1NF–BCNF)