






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







How to use this guide. Each topic starts with the core ideas, then works a complete numerical example of the kind set in operating systems tests: scheduling tables, Banker's algorithm, address translation and page-replacement traces. Every number in the worked examples and answers was produced by small Python simulators written for this guide, so you can use them to check your own working. Where a method depends on a tie-breaking rule, the rule is stated, because a different rule can change the answer.
Contents
Processes and threads CPU scheduling (FCFS, SJF, SRTF, Priority, Round Robin) Process synchronization Deadlock and the Banker's algorithm Memory management and paging Virtual memory and page replacement File systems basics Summary table Practice questions and answers
A program is a passive file on disk. A process is a program in execution: its code plus a program counter, registers, a stack, a heap, data and open files. The OS keeps each process's details in a Process Control Block (PCB) : process ID, state, program counter, CPU registers, scheduling information (priority, queue pointers), memory-management information (page table), accounting data and I/O status.
Process states
State Meaning Typical transition out New Being created Admitted → Ready Ready Waiting in memory for the CPU Dispatched → Running Running Instructions are executing Timer interrupt → Ready; I/O request → Waiting; exit → Terminated Waiting (Blocked)
Waiting for an event such as I/O completion
Event occurs → Ready (never straight to Running)
Terminated Finished; PCB awaits cleanup (removed)
A context switch saves the state of the running process into its PCB and loads the saved state of the next one. It is pure overhead: no user work is done during the switch. The dispatcher performs the switch; the time it takes is the dispatch latency.
Threads. A thread is a unit of CPU execution inside a process. Threads of one process share code, data, heap and open files, but each has its own thread ID, program counter, registers and stack.
Process Thread Memory Own address space Shares the process's address space Creation / switch cost
High Low
Communication Needs IPC (pipes, message queues, shared memory)
Directly through shared variables (needs synchronization) Failure A crash usually affects only that process A crashing thread can bring down the whole process
Multithreading models: many-to-one (many user threads on one kernel thread; one blocking call blocks all), one-to-one (each user thread has a kernel thread; true parallelism; used by Linux and Windows) and many-to-many (user threads multiplexed onto a smaller or equal number of kernel threads).
2. CPU scheduling
Scheduling criteria. Maximise CPU utilisation and throughput (processes finished per unit time); minimise turnaround time , waiting time and response time.
Measure Formula Turnaround time (TAT) Completion time (CT) − Arrival time (AT) Waiting time (WT) TAT − Burst time (BT) = total time spent in the ready queue Response time (RT) Time first given the CPU − Arrival time
Non-preemptive scheduling lets a process keep the CPU until it finishes or blocks. Preemptive scheduling can take the CPU away, for example when a higher-priority or shorter job arrives, or when a time quantum expires.
2.1 The worked process set
All algorithms below use the same five processes. For priority scheduling, a lower number means a higher priority. Ties are broken by earlier arrival, then by lower process number.
Process Arrival Burst Priority P1 0 6 3 P2 1 4 1 P3 3 2 4 P4 4 5 2 P5 7 3 5
Total burst = 20, so every schedule finishes at time 20 (there is no idle time).
2.2 First-Come, First-Served (FCFS)
Run processes in arrival order, each to completion. Simple and fair in order, but a long job at the front makes everyone wait (the convoy effect ).
| P1 | P2 | P3 | P4 | P5 | 0 6 10 12 17 20
2.5 Priority scheduling
Non-preemptive. At 0 only P1 is ready, so it runs to 6. At 6 the ready priorities are P2 = 1, P3 = 4, P4 = 2 → P2, then P4 (2), then P3 (4), then P5 (5).
| P1 | P2 | P4 | P3 | P5 | 0 6 10 15 17 20
Process Arrival Burst Completion Turnaround (CT − AT) Waiting (TAT − BT) Response P1 0 6 6 6 0 0 P2 1 4 10 9 5 5 P3 3 2 17 14 12 12 P4 4 5 15 11 6 6 P5 7 3 20 13 10 10 Average 53/5 = 10.60 33/5 = 6.60 33/5 = 6.
Preemptive. P2 (priority 1) arrives at 1 and preempts P1 (3). P4 (priority 2) arrives at 4 but cannot preempt P2. At 5, P4 runs to 10, then P1 finishes its remaining 5 units, then P3 and P5.
| P1 | P2 | P4 | P1 | P3 | P5 | 0 1 5 10 15 17 20
Process Arrival Burst Completion Turnaround (CT − AT) Waiting (TAT − BT) Response P1 0 6 15 15 9 0 P2 1 4 5 4 0 0 P3 3 2 17 14 12 12 P4 4 5 10 6 1 1 P5 7 3 20 13 10 10 Average 52/5 = 10.40 32/5 = 6.40 23/5 = 4.
Low-priority processes can wait indefinitely ( starvation ). The usual fix is aging : gradually raise the priority of processes that have waited a long time.
2.6 Round Robin (RR)
Each process gets at most one time quantum q, then goes to the back of the ready queue. Convention used here: a process that arrives at the same instant a quantum ends joins the queue before the preempted process.
Trace for q = 2: 0–2 P1 (queue then P2, P1) → 2–4 P2 (P3 and P4 arrive: queue P1, P3, P4, P2) → 4–6 P → 6–8 P3 finishes (P5 arrived at 7) → 8–10 P4 → 10–12 P2 finishes → 12–14 P1 finishes → 14–16 P5 → 16– 18 P4 → 18–19 P5 finishes → 19–20 P4 finishes.
| P1 | P2 | P1 | P3 | P4 | P2 | P1 | P5 | P4 | P5 | P4 | 0 2 4 6 8 10 12 14 16 18 19 20
Process Arrival Burst Completion Turnaround (CT − AT) Waiting (TAT − BT) Response P1 0 6 14 14 8 0 P2 1 4 12 11 7 1 P3 3 2 8 5 3 3 P4 4 5 20 16 11 4 P5 7 3 19 12 9 7 Average 58/5 = 11.60 38/5 = 7.60 15/5 = 3.
With q = 4:
Average turnaround 11.60, average waiting 7.60 and average response 4.60: the same turnaround and waiting as q = 2, but with 6 context switches instead of 10.
RR gives good response time and no starvation, but usually a higher average turnaround than SJF. If q is very large, RR becomes FCFS; if q is very small, the system spends most of its time context switching. A common rule of thumb is that about 80% of CPU bursts should be shorter than q.
2.7 Comparison for this process set
Algorithm Avg turnaround Avg waiting Avg response Context switches FCFS 10.00 6.00 6.00 4 SJF (non-preemptive) 9.00 5.00 5.00 4 SRTF (preemptive SJF) 8.40 4.40 2.60 5 Priority (non-preemptive) 10.60 6.60 6.60 4 Priority (preemptive) 10.40 6.40 4.60 5 Round Robin, q = 2 11.60 7.60 3.00 10 Round Robin, q = 4 11.60 7.60 4.60 6
Context switches count only changes from one process to another. SRTF wins on turnaround and waiting time, as theory predicts; RR with q = 2 has the second-best response time but the most switches.
Other schedulers to know: multilevel queue (separate queues, e.g. foreground RR and background FCFS, with fixed priority between queues) and multilevel feedback queue (processes move between queues: CPU-heavy jobs sink to lower levels, waiting jobs are aged upward).
3. Process synchronization
A race condition occurs when the result depends on the timing of threads that access shared data. The statement count++ is really three machine steps: LOAD the value into a register, ADD 1, STORE it back.
Worked example. count = 5. Thread A runs count++ and thread B runs count--. The correct result is
Final value Number of interleavings 4 9 5 2 6 9
For example, A.load (5), A.add (6), B.load (5), B.add (4), A.store (6), B.store (4) leaves 4 : A's update is lost. Only the 2 interleavings where one thread finishes all three steps before the other starts give the right answer.
The code that touches shared data is the critical section. A correct solution must provide mutual exclusion (one thread at a time), progress (if nobody is inside, a waiting thread can enter) and bounded waiting (no thread waits forever).
Tools
Mutex lock: acquire() before the critical section, release() after. A spinlock busy-waits in a loop; useful only for very short waits on multiprocessors. Semaphore S (an integer) with two atomic operations: wait(S) (also called P or down) decrements S and blocks the caller if the result is negative; signal(S) (V or up) increments S and wakes a waiting process if any. A binary semaphore (0/1) acts like a mutex; a counting semaphore controls access to N identical resources. With this definition, a negative value −k means k processes are blocked.
Process Allocation (A B C) Max (A B C) Need = Max − Allocation P0 1 1 2 5 4 3 4 3 1 P1 2 0 1 3 2 2 1 2 1 P2 2 2 1 6 3 3 4 1 2 P3 0 1 1 2 2 3 2 1 2 P4 1 0 2 3 1 4 2 1 2
Available = Total − sum of Allocation = (9, 7, 8) − (6, 4, 7) = (3, 3, 1).
Safety algorithm. Set Work = Available. Repeatedly find an unfinished process with Need ≤ Work, pretend it runs to completion and returns its allocation (Work = Work + Allocation). Convention: on each pass, scan P0 to P4 and take the first one that fits.
Step: run Check Need ≤ Work New Work = Work + Allocation P1 (1, 2, 1) ≤ (3, 3, 1) ✓ (3, 3, 1) + (2, 0, 1) = (5, 3, 2) P0 (4, 3, 1) ≤ (5, 3, 2) ✓ (5, 3, 2) + (1, 1, 2) = (6, 4, 4) P2 (4, 1, 2) ≤ (6, 4, 4) ✓ (6, 4, 4) + (2, 2, 1) = (8, 6, 5) P3 (2, 1, 2) ≤ (8, 6, 5) ✓ (8, 6, 5) + (0, 1, 1) = (8, 7, 6) P4 (2, 1, 2) ≤ (8, 7, 6) ✓ (8, 7, 6) + (1, 0, 2) = (9, 7, 8)
P0 (Need 4, 3, 1) does not fit the first Work of (3, 3, 1), so P1 goes first. All five finish, so the state is safe with safe sequence ⟨P1, P0, P2, P3, P4⟩. The final Work equals the total (9, 7, 8), a useful check.
Resource-request algorithm. When Pi requests Request: 1. If Request > Need(i), raise an error (it exceeds the declared maximum). 2. If Request > Available, Pi must wait. 3. Otherwise pretend to grant it: Available −= Request, Allocation(i) += Request, Need(i) −= Request, then run the safety algorithm. Grant only if the new state is safe.
Request 1: P1 asks for (1, 0, 1). (1, 0, 1) ≤ Need (1, 2, 1) and ≤ Available (3, 3, 1). Pretend: Allocation P1 = (3, 0, 2), Need P1 = (0, 2, 0), Available = (2, 3, 0). Safety: P1 fits (0, 2, 0) ≤ (2, 3, 0) → Work (5, 3, 2); then P0 → (6, 4, 4), P2 → (8, 6, 5), P3 → (8, 7, 6), P4 → (9, 7, 8). Safe → grant , sequence ⟨P1, P0, P2, P3, P4⟩.
Request 2 (starting again from the original state): P4 asks for (0, 1, 1). It passes steps 1 and 2. Pretend: Allocation P4 = (1, 1, 3), Need P4 = (2, 0, 1), Available = (3, 2, 0). Every process now needs at least one unit of C, and none is available, so no process can finish. Unsafe → P4 must wait , even though the resources are physically free.
5. Memory management and paging
Logical (virtual) addresses are generated by the CPU; physical addresses are seen by memory. The MMU translates between them at run time.
Contiguous allocation places each process in one block. Placement strategies are first fit , best fit and worst fit. It suffers from external fragmentation (enough total free memory, but in scattered holes), fixed by compaction or by paging. Internal fragmentation is wasted space inside an allocated block, for example the unused part of a process's last page.
Paging divides physical memory into fixed-size frames and logical memory into pages of the same size. A page table maps page number → frame number. There is no external fragmentation, only internal fragmentation in the last page.
Address translation: page number p = LA div page size; offset d = LA mod page size; physical address = frame × page size + d. With page size 2ⁿ, the low n bits are the offset and the remaining bits are the page number.
5.1 Worked example
Page size 4 KiB (4,096 bytes, 12-bit offset). Page table: page 0 → frame 5, 1 → 9, 2 → 11, 3 → not in memory, 4 → 7.
Logical address Page p Offset d Frame Physical address 6716 (0x1A3C) 1 2620 (0xA3C) 9 9 × 4096 + 2620 = 39484 (0x9A3C) 12032 (0x2F00) 2 3840 (0xF00) 11 48896 (0xBF00) 16675 (0x4123) 4 291 (0x123) 7 28963 (0x7123) 10000 (0x2710) 2 1808 (0x710) 11 46864 (0xB710) 12292 (0x3004) 3 4 (0x004) — Page fault (page not in memory)
Notice the hexadecimal shortcut: with 4 KiB pages the last three hex digits are the offset, and the leading digit is the page number, so 0x1A3C becomes frame 9 followed by A3C = 0x9A3C.
Page table size. A 32-bit address with 4 KiB pages has a 12-bit offset and a 20-bit page number, so 2²⁰ = 1,048,576 entries. At 4 bytes per entry the table is 4 MiB per process , which is why real systems use multi-level (hierarchical) page tables, hashed page tables or inverted page tables.
5.2 The TLB and effective access time
Every memory reference needs a page-table lookup, which would double memory access time. The Translation Lookaside Buffer (TLB) is a small, fast cache of recent page → frame mappings.
EAT = h × (TLB time + memory time) + (1 − h) × (TLB time + 2 × memory time), where h is the TLB hit ratio.
With a 20 ns TLB and 100 ns memory: at h = 0.80, EAT = 0.8 × 120 + 0.2 × 220 = 140.0 ns ; at h = 0.98, EAT = 122.0 ns. Without a TLB every access costs 200 ns.
Segmentation divides a program into variable-size logical units (code, data, stack). A logical address is (segment, offset); the segment table gives base and limit, and offset ≥ limit causes a trap. Segmentation matches the programmer's view but brings back external fragmentation.
6. Virtual memory and page replacement
Demand paging loads a page only when it is first referenced. A reference to a page not in memory causes a page fault : the OS finds the page on disk, finds a free frame (or chooses a victim to evict), reads the page in, updates the page table and restarts the instruction.
Cost of faults. With 200 ns memory access and an 8 ms fault service time, EAT = (1 − p) × 200 + p × 8,000,000 ns. At p = 1/1,000, EAT = 8,199.80 ns , about 41 times slower. At p = 1/100,000, EAT = 280.00 ns. To keep the slowdown under 10% (EAT ≤ 220 ns), p must be at most about 2.5 × 10⁻⁶, roughly 1 fault in 400,000 accesses.
Thrashing happens when processes do not have enough frames for their working set , so they fault constantly and CPU utilisation collapses. Remedies: reduce the degree of multiprogramming, or allocate frames by working set or page-fault frequency.
6.1 Worked example: FIFO, LRU and Optimal
Reference string (15 references): 2 5 1 2 4 5 3 2 6 5 2 4 1 5 2 , with 3 frames , all initially empty. F marks a fault and · marks a hit.
FIFO evicts the page that has been in memory the longest. 11 faults , 4 hits.
Ref 2 5 1 2 4 5 3 2 6 5 2 4 1 5 2 F1 2 2 2 2 4 4 4 4 6 6 6 6 1 1 1 F2 – 5 5 5 5 5 3 3 3 5 5 5 5 5 2 F3 – – 1 1 1 1 1 2 2 2 2 4 4 4 4 Result F F F · F · F F F F · F F · F
7.1 Worked example: Unix-style inode
An inode holds 12 direct pointers plus one single-, one double- and one triple-indirect pointer. Block size 4 KiB, pointer size 4 bytes, so one block holds 4096 ÷ 4 = 1,024 pointers.
Part Blocks reachable Data size 12 direct 12 48 KiB Single indirect 1,024 4 MiB Double indirect 1,024² = 1,048,576 4 GiB Triple indirect 1,024³ = 1,073,741,824 4 TiB Total 1,074,791,436 4,402,345,721,856 bytes ≈ 4.0039 TiB
Which pointer serves byte offset 1,000,000? Logical block = 1,000,000 div 4096 = 244. Blocks 0–11 are direct and 12–1,035 are single indirect, so block 244 is reached through the single-indirect pointer. Byte 10,000,000 is in block 2,441, which is beyond 1,035, so it uses the double-indirect pointer.
8. Summary table
Topic Key facts to remember Process vs thread Threads share code, data, heap and files; each has its own stack, registers and PC Scheduling formulas
TAT = CT − AT; WT = TAT − BT; RT = first run − AT
FCFS Non-preemptive; convoy effect; example average WT 6. SJF / SRTF Minimum average WT (non-preemptive / preemptive); needs burst prediction; starvation. Example WT 5.00 / 4. Priority Lower number = higher priority (stated convention); starvation fixed by aging Round Robin Quantum q; best response; large q becomes FCFS; small q means switching overhead Race condition (^) count++ = load, add, store; 2 of 20 interleavings correct in the example Semaphores wait decrements and may block; signal increments and may wake; producer–consumer uses empty = N, full = 0, mutex = 1 Coffman conditions
Mutual exclusion, hold and wait, no preemption, circular wait: all four needed
RAG Single instance: cycle = deadlock; multiple instances: cycle = possible deadlock Banker's Need = Max − Allocation; safe if a full safe sequence exists; example ⟨P1, P0, P2, P3, P4⟩ Paging p = LA div size, d = LA mod size, PA = frame × size + d; no external fragmentation TLB EAT = h(t + m) + (1 − h)(t + 2m) Page replacement OPT ≤ others; LRU is a stack algorithm; FIFO can show Belady's anomaly Inode Pointers per block = block size ÷ pointer size; add direct + k + k² + k³ blocks
9. Practice questions
State two things threads of the same process share and two things each thread has of its own. Can a process move directly from Waiting to Running? Explain. Processes P1 (arrival 0, burst 8), P2 (1, 4) and P3 (2, 2). Draw the FCFS Gantt chart and compute the average waiting and turnaround times. Repeat question 3 using non-preemptive SJF. Repeat question 3 using SRTF. Repeat question 3 using Round Robin with q = 3 (an arrival joins the queue before a preempted process). What happens to Round Robin when the quantum is (a) very large, (b) very small? Define starvation. Which algorithms in this guide can cause it, and what is the standard remedy?
x = 5. Thread A does x++ and thread B does x-- without locking. List all possible final values and explain how 6 can happen. A counting semaphore starts at 3. Five processes call wait , then one calls signal , then another calls wait. What is the semaphore's value and how many processes are blocked? In the bounded-buffer solution with N = 5, what are empty , full and mutex after 3 items have been produced and 1 consumed (with no process inside its critical section)? Name the four Coffman conditions and give one way to break circular wait. A RAG with single-instance resources has edges P1 → R1, R1 → P2, P2 → R2, R2 → P3, P3 → R3, R → P1. Is there a deadlock? What if the edge P3 → R3 is removed? Using the Banker's example in section 4.1 (original state), can P3's request for (0, 1, 0) be granted? Give the safe sequence if so. In the same original state, what happens if P2 requests (4, 0, 0)? If P3 requests (3, 0, 0)? A system uses 1 KiB pages. Page table: 0 → 3, 1 → 7, 2 → not in memory, 3 → 1, 4 → 6. Translate logical addresses 1500, 5000 and 2600. A 32-bit system uses 8 KiB pages and 4-byte page-table entries. How many entries does a single- level page table have and how large is it? For the reference string 4 7 4 1 7 3 4 1 2 7 with 3 frames, count the page faults for FIFO, LRU and Optimal. TLB lookup takes 10 ns, a memory access takes 80 ns and the TLB hit ratio is 90%. Find the effective access time and compare it with no TLB. An inode has 10 direct pointers, one single-indirect and one double-indirect pointer. Blocks are 1 KiB and pointers are 4 bytes. What is the maximum file size?
Answers
1. Shared: code, global data, heap, open files (any two). Own: thread ID, program counter, register set, stack (any two). 2. No. When the awaited event occurs, the process moves to Ready and must wait for the scheduler to dispatch it. 3. Gantt: | P1 0–8 | P2 8–12 | P3 12–14 |. CT = 8, 12, 14; TAT = 8, 11, 12; WT = 0, 7, 10. Average WT = 17/3 = 5.67; average TAT = 31/3 = 10.33. The two short jobs wait behind the long one (the convoy effect). 4. Only P1 is present at 0, so it still runs 0–8; then P3 (shorter) 8–10, then P2 10–14. TAT = 8, 13, 8; WT = 0, 9, 6. Average WT = 15/3 = 5.00; average TAT = 29/3 = 9.67. 5. | P1 0–1 | P2 1–2 | P3 2–4 | P2 4–7 | P1 7–14 |. P2 preempts P1 at 1 (4 < 7); P3 preempts P2 at 2 (2 < 3). CT = 14, 7, 4; TAT = 14, 6, 2; WT = 6, 2, 0. Average WT = 8/3 = 2.67; average TAT = 22/3 = 7.33. Every process starts the moment it arrives, so average response time is 0.00. 6. | P1 0–3 | P2 3–6 | P3 6–8 | P1 8–11 | P2 11–12 | P1 12–14 |. CT = 14, 12, 8; TAT = 14, 11, 6; WT = 6, 7,