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)
// 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)) ;orwhile (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 waits for a mutex held by low-priority , while medium-priority CPU-bound preempts — effectively starving ! Solved by the Priority Inheritance Protocol ( temporarily inherits ’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:
- 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.
- Read accesses by either parent or child incur zero page faults.
- The first write to shared page triggers a Protection Trap (Soft Page Fault): kernel allocates new physical frame, copies the page, updates the writer’s PTE to Read-Write, and clears COW once reference count drops to !
Practice Questions on This Chapter (2)
A process owns virtual memory pages in RAM ( Read-Only code pages and Read-Write data pages) and calls fork() on an OS with Copy-on-Write (COW) paging. Before exec(), the parent writes to distinct data pages and the child writes to data pages ( of which is the same page written by the parent). How many total physical page frames are occupied by parent and child combined?
Which of the following statements about Multi-Core Process Synchronization are CORRECT?