# OS 2027 Rank-Decider: Hardware CAS/TestAndSet, Priority Inversion, Copy-on-Write & File I/O
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Operating Systems
Canonical URL: https://gateandnet.com/exams/gate-cs/operating-systems/atomic-cas-priority-inversion-cow-and-file-allocation

> 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 $P_H$ waits for a mutex held by low-priority $P_L$, while medium-priority CPU-bound $P_M$ preempts $P_L$ — effectively starving $P_H$! Solved by the **Priority Inheritance Protocol** ($P_L$ temporarily inherits $P_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 $X$ triggers a **Protection Trap (Soft Page Fault)**: kernel allocates $1$ new physical frame, copies the $4\text{ KB}$ page, updates the writer’s PTE to Read-Write, and clears COW once reference count drops to $1$!

## Solved Practice Questions & PYQs

### [GATE CS 2027 Expected NAT | NAT]
A process owns $20$ virtual memory pages in RAM ($12$ Read-Only code pages and $8$ Read-Write data pages) and calls `fork()` on an OS with **Copy-on-Write (COW)** paging. Before `exec()`, the parent writes to $3$ distinct data pages and the child writes to $2$ data pages ($1$ of which is the same page written by the parent). How many **total physical page frames** are occupied by parent and child combined?
**Correct Answer:** 24
**Solution:** Start with $20$ shared physical frames. Each COW write to a currently shared page ($\text{refcount} = 2$) allocates $1$ new frame:
- $2$ pages written by parent only $\to +2$ frames
- $1$ page written by child only $\to +1$ frame
- $1$ page written by **both** parent and child: first writer copies ($+1$ frame, dropping original frame’s refcount to $1$), so the second writer modifies the original frame **in-place ($+0$ frames)**!
Total frames $= 20 + 2 + 1 + 1 = \mathbf{24}$!

### [GATE CS 2026–27 Expected MSQ | MSQ]
Which of the following statements about **Multi-Core Process Synchronization** are **CORRECT**?
**Correct Answer:** A,B,C
**Solution:** **A, B, C** are all fundamental OS truths! **D is False**: initializing semaphore `synch = 0`, putting `signal(synch)` after $S_1$ and `wait(synch)` before $S_2$ enforces $S_1 \prec S_2$ cleanly.
