Problem Set
Problem 1 — Process States
List all possible state transitions for a process and identify which transition requires the Scheduler, which requires an I/O event, and which is initiated by the process itself.
Solution. (Revision: §2.1)
| Transition | Trigger |
|---|---|
| New → Ready | OS admission |
| Ready → Running | Scheduler (dispatch) |
| Running → Ready | Preemption (timer) |
| Running → Blocked | I/O or wait request |
| Blocked → Ready | I/O completion |
| Running → Terminated | exit() system call |
The scheduler triggers Ready → Running and Running → Ready (preemption). I/O events trigger Running → Blocked and Blocked → Ready. The process itself initiates Running → Terminated.
Problem 2 — Kernel Architecture Trade-offs
A microkernel-based OS adds 2 S of message-passing overhead per system call compared to a Monolithic kernel. If a web server makes system calls per second, what is the total overhead As a fraction of CPU time on a 3 GHz processor?
Solution. (Revision: §1.2)
Total message-passing time per second: seconds of CPU time per Second. This exceeds available CPU time, making the microkernel approach infeasible at this call Rate without optimisations such as batched IPC or shared-memory channels.
Problem 3 — FCFS Scheduling
Processes arrive in the order (burst 10, arrival 0), (burst 5, arrival 1), (burst 2, arrival 2). Compute waiting time and turnaround time for each process under FCFS.
Solution. (Revision: §2.4)
Gantt: at times 0, 10, 15, 17.
| Process | Waiting | Turnaround |
|---|---|---|
| 0 | 10 | |
| 9 | 14 | |
| 13 | 15 | |
| Avg | 7.33 | 13 |
waits the longest despite having the shortest burst — the convoy effect.
Problem 4 — SJF Scheduling
Using the same processes as Problem 3, compute the schedule under non-preemptive SJF.
Solution. (Revision: §2.4)
At Only is available. runs 0—10. At , (burst 5) and (burst 2) are both ready. SJF selects Then .
Gantt: at times 0, 10, 12, 17.
| Process | Waiting | Turnaround |
|---|---|---|
| 0 | 10 | |
| 10 | 15 | |
| 8 | 10 | |
| Avg | 6 | 11.67 |
Average waiting time improves from 7.33 to 6 compared to FCFS.
Problem 5 — Round Robin Scheduling
Using the processes from Problem 3 with quantum Draw the Gantt chart and compute the Average turnaround time.
Solution. (Revision: §2.4)
Gantt:
Times: 0, 2, 4, 6, 8, 10, 12, 13, 15, 17.
| Process | Completion | Turnaround |
|---|---|---|
| 17 | 17 | |
| 13 | 12 | |
| 6 | 4 | |
| Avg | 11 |
Round Robin gives the fastest turnaround for (4 vs 10 under SJF) at the cost of slower Average turnaround.
Problem 6 — SRTF Scheduling
Processes: (burst 8, arrival 0), (burst 4, arrival 1), (burst 2, arrival 2). Compute the schedule under SRTF (preemptive SJF).
Solution. (Revision: §2.4)
: starts (remaining 8). : arrives (remaining 4). SRTF: 4 8, preempt . starts. : arrives (remaining 2). SRTF: 2 4, preempt . starts. : completes. (remaining 4) vs (remaining 8). resumes. : completes. resumes (remaining 6). : completes.
Gantt:
| Process | Completion | Turnaround | Waiting |
|---|---|---|---|
| 14 | 14 | 6 | |
| 8 | 7 | 3 | |
| 4 | 2 | 0 | |
| Avg | 7.67 | 3 |
Problem 7 — Critical Section
Show that the following solution to the critical section problem is incorrect (Peterson”s algorithm With the order of flag[i] = true and turn = j swapped):
// Process i:turn = j;flag[i] = true;while (flag[j] && turn == j);// critical sectionflag[i] = false;Solution. (Revision: §3.1)
Both processes can set turn to the other’s value, then set their own flag. If both execute turn = j and turn = i respectively, then turn ends up as the last writer’s value. Both then Set their flags to true. The while loop checks flag[j] && turn == j. If the interleaving is:
- :
turn = 1 - :
turn = 0 - :
flag[0] = true - :
flag[1] = true
checks: flag[1] is true and turn == 1? No (turn = 0). enters. checks: flag[0] is true and turn == 0? Yes. waits.
Now consider:
- :
turn = 1 - :
turn = 0 - :
flag[1] = true - : checks
flag[0](false) → enters critical section. - :
flag[0] = true - : checks
flag[1](true) andturn == 1(no, turn = 0) → enters critical section.
Both processes are in their critical section simultaneously. Mutual exclusion is violated.
Problem 8 — Producer-Consumer with Semaphores
In the bounded buffer solution of §3.6, explain why the empty and full semaphores must be Different from the mutex. What goes wrong if we use only mutex (initialised to 1) and count (shared variable)?
Solution. (Revision: §3.6)
If we use only mutex and check count manually:
pthread_mutex_lock(&mutex);while (count == BUFFER_SIZE) { pthread_mutex_unlock(&mutex); // release while waiting // ... but how to re-acquire when space is available?}Without empty and full semaphores, the producer must busy-wait or use condition variables. Semaphores provide blocking semantics: the producer blocks on empty when the buffer is full And is automatically woken when a consumer signals empty. Using only mutex either causes Busy-waiting (wasting CPU cycles) or requires the programmer to correctly implement the Wait/signal protocol — which is exactly what semaphores encapsulate.
Problem 9 — Deadlock: Necessary Conditions
A system has three processes and three resources. Each process holds one resource and requests a Second. Is deadlock possible? If so, identify the deadlocked set.
Solution. (Revision: §4.1)
Yes. If holds and requests ; holds and requests ; holds and requests We have circular wait: . All four Coffman conditions hold (mutual exclusion, hold-and-wait, no preemption, Circular wait), so deadlock exists. The deadlocked set is .
If instead holds and requests ; holds and requests ; holds and requests The same circular wait exists.
Problem 10 — Banker's Algorithm Safety
Three processes, three resource types. Available = .
| Process | Allocation | Max | Need |
|---|---|---|---|
| (1, 0, 0) | (3, 2, 1) | (2, 2, 1) | |
| (1, 0, 1) | (2, 1, 2) | (1, 1, 1) | |
| (1, 1, 0) | (2, 1, 1) | (1, 0, 1) |
Determine if the system is in a safe state.
Solution. (Revision: §4.3)
Work = . All Finish = false.
- : Need . Execute. Work = . Finish[1] = true.
- : Need . Execute. Work = . Finish[2] = true.
- : Need . Execute. Work = . Finish[0] = true.
All Finish = true. Safe sequence: .
Problem 11 — Banker's Algorithm Request
Using the state from Problem 10, determine whether the request from for can be Granted.
Solution. (Revision: §4.3)
- . OK.
- . OK.
- Pretend to allocate: , , .
- Safety: Work = .
- : . Execute. Work = .
- : . Execute. Work = .
- : . Execute. Work = .
All finish. Request granted.
Problem 12 — Page Table Address Translation
A system uses 32-bit virtual addresses with 4 KiB pages. The page table entry is 4 bytes.
(a) How many bits for the page number and offset? (b) How many entries in the page table? (c) What is the size of the page table?
Solution. (Revision: §5.2)
(a) Offset = bits. Page number = bits. (b) entries. (c) bytes = 4 MiB.
Problem 13 — TLB and EAT Calculation
A system with a two-level page table. TLB hit ratio = 0.90, TLB access = 2 ns, memory access = 100 ns (for any level). Page fault rate = 0.001, page fault service = 6 ms. Compute the effective Access time.
Solution. (Revision: §5.6)
TLB hit, no fault (): ns. TLB miss, no fault (): ns (TLB + outer PT + inner PT + data). Fault (): ns.
ns S.
Problem 14 — Page Fault EAT
What page fault rate is needed to ensure the effective access time is no more than 200 ns? Memory Access time = 100 ns, page fault service = 8 ms.
Solution. (Revision: §5.5)
Assume no TLB for simplicity: .
The page fault rate must be below 0.00125%, or roughly 1 fault per 80,000 accesses. This Illustrates why effective caching is essential.
Problem 15 — LRU Page Replacement
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. Four frames. Compute the number of page Faults under LRU and under FIFO. Does Belady’s anomaly occur?
Solution. (Revision: §5.7)
FIFO (4 frames):
| Ref | F1 | F2 | F3 | F4 | Fault? |
|---|---|---|---|---|---|
| 1 | 1 | Yes | |||
| 2 | 1 | 2 | Yes | ||
| 3 | 1 | 2 | 3 | Yes | |
| 4 | 1 | 2 | 3 | 4 | Yes |
| 1 | 1 | 2 | 3 | 4 | No |
| 2 | 1 | 2 | 3 | 4 | No |
| 5 | 5 | 2 | 3 | 4 | Yes |
| 1 | 5 | 1 | 3 | 4 | Yes |
| 2 | 5 | 1 | 2 | 4 | Yes |
| 3 | 5 | 1 | 2 | 3 | Yes |
| 4 | 5 | 1 | 2 | 3 | No (wait, 4 not in frames) |
Let me redo FIFO more carefully. FIFO replaces the oldest page in memory.
Frames after each reference (FIFO queue, front = oldest):
| Ref | Queue (oldest→newest) | Fault? |
|---|---|---|
| 1 | [1] | Yes |
| 2 | [1, 2] | Yes |
| 3 | [1, 2, 3] | Yes |
| 4 | [1, 2, 3, 4] | Yes |
| 1 | [1, 2, 3, 4] | No |
| 2 | [1, 2, 3, 4] | No |
| 5 | [2, 3, 4, 5] | Yes |
| 1 | [3, 4, 5, 1] | Yes |
| 2 | [4, 5, 1, 2] | Yes |
| 3 | [5, 1, 2, 3] | Yes |
| 4 | [1, 2, 3, 4] | Yes |
| 5 | [2, 3, 4, 5] | Yes |
FIFO faults: 10.
With 3 frames: faults = 9 (from Belady’s anomaly example in the text). With 4 frames: faults = 10. Belady’s anomaly occurs: more frames produce more faults.
LRU (4 frames): LRU replaces the least recently used page.
| Ref | Frames | Fault? |
|---|---|---|
| 1 | [1] | Yes |
| 2 | [1, 2] | Yes |
| 3 | [1, 2, 3] | Yes |
| 4 | [1, 2, 3, 4] | Yes |
| 1 | [1, 2, 3, 4] | No |
| 2 | [1, 2, 3, 4] | No |
| 5 | [5, 2, 3, 4] | Yes |
| 1 | [5, 1, 3, 4] | Yes |
| 2 | [5, 1, 2, 4] | Yes |
| 3 | [5, 1, 2, 3] | Yes |
| 4 | [4, 1, 2, 3] | Yes |
| 5 | [4, 5, 2, 3] | Yes |
LRU faults: 10. LRU does not exhibit Belady’s anomaly (it is a stack algorithm), but in this Particular reference string, 3 and 4 frames happen to produce the same count under LRU.
Problem 16 — Belady's Anomaly
Prove that FIFO can exhibit Belady’s anomaly by constructing a counterexample with 2 and 3 frames.
Solution. (Revision: §5.7)
Reference string: 1, 2, 3, 1, 2.
2 frames: | Ref | F1 | F2 | Fault? | | --- | — | — | ------ | | 1 | 1 | | Yes | | 2 | 1 | 2 | Yes | | 3 | 3 | 2 | Yes | | 1 | 3 | 1 | Yes | | 2 | 3 | 2 | Yes |
Faults: 5.
3 frames: | Ref | F1 | F2 | F3 | Fault? | | --- | — | — | — | ------ | | 1 | 1 | | | Yes | | 2 | 1 | 2 | | Yes | | 3 | 1 | 2 | 3 | Yes | | 1 | 1 | 2 | 3 | No | | 2 | 1 | 2 | 3 | No |
Faults: 3.
Here 3 frames produces fewer faults (3 vs 5), so this is not a counterexample. The original Counterexample from the text (1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with 3 vs 4 frames) remains Valid: 3 frames → 9 faults, 4 frames → 10 faults.
Problem 17 — Working Set and Thrashing
A system has 40 frames. Process has a working set of 15 pages, has 12 pages, and Has 18 pages. Can all three run simultaneously without thrashing? What if with a working set Of 8 pages is added?
Solution. (Revision: §5.8)
Without : total working set = . Thrashing occurs. Only two Processes can run concurrently (e.g., Or ).
With : total = . Even worse. Using working set admission, we Would run at most two processes. The best combination that fits is or .
Problem 18 — File Allocation
A file system uses contiguous allocation with 512-byte blocks. A file is created at block 1000 and Grows to 5000 bytes. Blocks 1000—1009 are allocated. The file then grows by 2000 bytes, but Blocks 1010—1012 are occupied by another file. How does the file system handle this growth?
Solution. (Revision: §6.2)
The file currently occupies blocks 1000—1009 (10 blocks for 5000 bytes). To grow by 2000 bytes, it Needs 4 more blocks. Blocks 1010—1012 are occupied, so contiguous extension is impossible.
Options:
- Move the file: Copy blocks 1000—1009 to a new location with 14 consecutive free blocks. Expensive for large files.
- Extents: Use multiple extents. The file has extent 1 = blocks 1000—1009 and extent 2 = blocks 1016—1019 (wherever 4 free blocks are found). The directory stores a list of extents.
- Switch to linked or indexed allocation: Not practical once the file system is in use.
This illustrates the primary disadvantage of contiguous allocation: inability to grow files Dynamically without costly relocation.
Problem 19 — Disk Scheduling
A disk has 500 cylinders (0—499). The request queue contains: 55, 58, 39, 18, 90, 160, 150, 38, 184. The head is at cylinder 50, moving toward 0. Compute the total head movement for SCAN and C-SCAN.
Solution. (Revision: §7.3)
SCAN (moving left first): 50 → 39 → 38 → 18 → 0 → 55 → 58 → 90 → 150 → 160 → 184. Total = cylinders.
Wait, let me be more precise. Head starts at 50, moving toward 0. 50 → 39: 11 39 → 38: 1 38 → 18: 20 18 → 0: 18 0 → 55: 55 55 → 58: 3 58 → 90: 32 90 → 150: 60 150 → 160: 10 160 → 184: 24 Total = cylinders.
C-SCAN (moving left, then jump right): 50 → 39 → 38 → 18 → 0 → 499 → 184 → 160 → 150 → 90 → 58 → 55. Total = cylinders.
SCAN is far more efficient here because the requests are clustered between 18 and 184.
Problem 20 — Security: Buffer Overflow and Defences
Consider the following C function:
void vulnerable(char *input) { char buffer[64]; strcpy(buffer, input);}(a) Explain how a buffer overflow attack works against this function. (b) Which defences (from §9.3) would prevent the attack? Explain how each works in this context.
Solution. (Revision: §9.3)
(a) If input exceeds 63 characters, strcpy writes past the end of bufferOverwriting the Stack frame. On x86-64, the saved return address is located above the buffer on the stack. An Attacker can overwrite the return address with the address of injected shellcode (also placed in The buffer), gaining control of execution when the function returns.
(b) Defences:
- Stack canary: A random value is placed between
bufferand the return address. The overflow would overwrite the canary. Before returning, the function checks the canary and aborts if it has been modified. - ASLR: Randomises the stack base address, so the attacker cannot predict where
buffer(and therefore the shellcode) will be in memory. - DEP (W^X): Marks the stack as non-executable. Even if the attacker overwrites the return address to point to the shellcode in
bufferThe CPU will fault when trying to execute it. - CFI: Verifies that the return address points to a valid call site. A crafted address injected by the overflow would fail the CFI check.
Common Pitfalls
- Confusing processes and threads. Processes have separate address spaces; threads share the same address space within a process. Fix: Context switching between processes is expensive (address space swap); between threads is cheaper.
- Wrong deadlock condition. Deadlock requires four conditions: mutual exclusion, hold and wait, no preemption, circular wait. Fix: Prevent deadlock by eliminating one condition; use Banker’s algorithm for avoidance.
- Confusing virtual memory and swapping. Virtual memory maps logical addresses to physical; swapping moves entire processes between RAM and disk. Fix: Virtual memory allows partial loading (pages); swapping moves complete processes.
Worked Examples
Example 1: Process scheduling
Problem. Processes P1 (burst 6), P2 (burst 2), P3 (burst 8) arrive in order at time 0. Calculate average waiting time using SJF.
Solution. SJF order: P2 (2), P1 (6), P3 (8). Waiting times: P2 = 0, P1 = 2, P3 = 8. Average = (0 + 2 + 8)/3 = 3.33.
Example 2: Deadlock detection
Problem. Three processes P1, P2, P3 each hold one resource and request a second. Is there a deadlock?
Solution. P1 holds R1, requests R2. P2 holds R2, requests R3. P3 holds R3, requests R1. This forms a cycle (circular wait), so there is a deadlock.
Summary
- Process management: scheduling algorithms (FCFS, SJF, Round Robin), deadlock (conditions, prevention, avoidance, detection).
- Memory management: paging, segmentation, virtual memory, page replacement (LRU, FIFO, optimal).
- File systems: directory structure, allocation methods (contiguous, linked, indexed).
- Synchronisation: semaphores, monitors, critical sections; Peterson’s algorithm for two processes.
Cross-References
| Topic | Site | Link |
|---|---|---|
| [Operating Systems] | A-Level | View |
| [Operating Systems] | University | View |