Section 8 · Revision
Operating Systems formula sheet
Formulas, standard counts and quick rules for processes, synchronization, deadlock, scheduling, memory and file systems.
System calls, processes and threads
| Fact | Value or rule |
|---|---|
successive fork() calls (no conditions) |
processes, new children |
fork() return value |
0 in child, child PID in parent, on failure |
Loop for i<n { fork(); print } |
prints |
exec succeeds |
never returns; code after it does not run |
| Zombie | terminated, parent has not called wait() |
| Orphan | still running, parent has exited; adopted by init |
Shared after fork |
open-file entries (common file offset); not memory |
| Shared by threads | code, data, heap, open files, signal handlers |
| Private to a thread | registers, PC, stack, thread ID |
- Many-to-one (user-level threads): fast switches, no parallelism, one blocking call blocks all threads.
- One-to-one (kernel threads): true parallelism, kernel call per thread operation.
- Privileged: I/O instructions, timer setting, interrupt disable, MMU and page table base changes. Not privileged: reading the clock, trap/syscall instruction.
Concurrency and synchronization
| Rule | Value |
|---|---|
| Counting semaphore final value | (any order) |
| Negative value (blocking implementation) | processes blocked |
| Processes in CS with semaphore initialised to | at most |
| Ordering "A before B" | semaphore at 0; signal after A, wait before B |
Critical section requirements: mutual exclusion, progress, bounded waiting. Strict alternation (turn) fails progress; the two-flag algorithm can deadlock; Peterson (flag + turn) satisfies all three for 2 processes on sequentially consistent memory.
- Test-and-set / CAS spinlock: mutual exclusion yes, bounded waiting no. Ticket lock (fetch-and-add): FIFO, bounded waiting yes.
- Disabling interrupts works only on a uniprocessor.
- Spin when the hold time is shorter than two context switches and there are several CPUs; otherwise block.
- Bounded buffer:
empty = N,full = 0,mutex = 1. Alwayswait(empty or full)beforewait(mutex); swapping them can deadlock. - First readers-writers: writers can starve. Second: readers can starve.
- Dining philosophers fixes: at most seated, asymmetric pick-up order, pick both or none.
- Monitors: one active process inside. Condition
signalwith no waiter does nothing (a semaphoreVis remembered). - Hoare: signalled thread runs at once. Mesa: signaller continues, so wrap
waitinwhile. cond_wait(c, m): holdm; releases it and sleeps atomically; re-acquires before returning.- Barrier with turnstile: last arrival signals; each thread does
wait; signal; final value 1. Reuse needs two turnstiles. - Priority inversion fix: priority inheritance.
- Lost update with
x++(load, add, store) by 2 threads each doing increments: final value can be as low as 2 (for ).
Deadlock
Four necessary conditions: mutual exclusion, hold and wait, no preemption, circular wait.
| Prevention method | Condition removed |
|---|---|
| Request everything at start, or release all before new request | Hold and wait |
| Take resources away from a waiting process | No preemption |
| Global numbering, request in increasing order | Circular wait |
| Quantity | Formula |
|---|---|
| processes, each needs at most of identical units; deadlock impossible iff | |
| Minimum units for maxima | |
| Need matrix | Need Max Allocation |
| Banker's safety / detection cost |
- Safe implies no deadlock; unsafe does not imply deadlock.
- Banker's request: grant only if Request Need, Request Available, and the resulting state is safe.
- Detection: Work Available; repeatedly finish any process with Request Work and add its Allocation. Unfinished processes are deadlocked (or wait forever on them).
- RAG, single instance per type: cycle iff deadlock. Several instances: cycle is necessary, not sufficient.
- Wait-for graph: only for single-instance resources.
CPU and I/O scheduling
| Quantity | Formula |
|---|---|
| Turnaround | completion arrival |
| Waiting | turnaround burst (minus I/O time if any) |
| Response | first run arrival |
| Response ratio (HRRN) | |
| RR max wait between quanta ( procs, quantum , switch ) | |
| CPU efficiency with switch cost | |
| Utilisation with I/O wait fraction , processes | |
| Exponential burst prediction |
| Algorithm | Preemptive | Starvation | Note |
|---|---|---|---|
| FCFS | no | no | convoy effect |
| SJF | no | yes | optimal avg wait, all at time 0 |
| SRTF | yes | yes | optimal avg wait overall |
| Priority | either | yes | fix with aging |
| RR | yes | no | large becomes FCFS; tiny becomes processor sharing |
| HRRN | no | no | favours short jobs, ages long ones |
| LRTF | yes | yes | very poor average |
| MLFQ | yes | yes without boost | full quantum used means demotion |
- With I/O bursts, draw the CPU and each device as separate timelines; CPU utilisation busy / makespan.
Disk scheduling (head at , cylinders ):
| Algorithm | Movement |
|---|---|
| FCFS | sum of successive gaps |
| SSTF | nearest next; can starve far requests |
| SCAN (moving up) | if requests lie below |
| LOOK (moving up) | |
| C-SCAN | goes to the end, jumps to 0, continues up; count the return if the question says so |
| C-LOOK | serves up to highest, jumps to lowest pending request |
Memory management and virtual memory
| Quantity | Formula |
|---|---|
| Pages in VA space | |
| Single-level page table size | pages PTE size |
| Frames | physical memory / page size |
| Inverted page table size | frames entry size |
| Levels for multi-level paging | |
| EAT with TLB ( TLB, memory, hit , levels) | |
| EAT with page faults (fault rate , service ) | |
| TLB reach | entries page size |
| Internal fragmentation | ; average |
| Optimal page size (overhead per process) | ( process size, PTE size) |
- PTE bits: frame number, valid, dirty, reference, protection. Dirty bit 0 means no write-back on eviction.
- Segmentation: check offset limit, then base offset. Segmentation with paging: segment number, page number, offset; page table per segment.
- Paging: internal fragmentation only. Segmentation and variable partitions: external fragmentation. Compaction needs run-time relocation.
- Placement: first fit (first hole that fits), best fit (smallest that fits), worst fit (largest), next fit (resume from last position).
- Buddy system: round up to a power of 2, split halves, merge only with the buddy at address XOR size.
Page replacement:
| Algorithm | Evicts | Belady's anomaly |
|---|---|---|
| FIFO | oldest loaded | possible |
| OPT | next used farthest in future | no |
| LRU | least recently used | no (stack algorithm) |
| Clock (second chance) | first page with ref bit 0; clears bits while passing | possible |
| LFU | lowest use count; state tie-break | possible |
- Working set : distinct pages in the last references. Thrashing when total frames.
File systems
| Quantity | Formula |
|---|---|
| Pointers per block | block size / pointer size |
| Max file size (inode) | |
| Index blocks for data blocks with linked index blocks ( pointers each) | |
| FAT size | blocks entry size |
| Bitmap size | blocks / 8 bytes |
| Linked allocation, payload per block | pointer size |
| Average rotational latency | s |
| Transfer time | bytes / transfer rate, or (sectors read / sectors per track) rotation |
| Disk access time | seek rotational latency transfer ( controller) |
| Allocation | Random access | External fragmentation | Reads for block (index in memory) |
|---|---|---|---|
| Contiguous | yes | yes | 1 |
| Linked | no | no | (1-based) |
| FAT (cached) | yes | no | 1 |
| Indexed | yes | no | 1 plus index blocks not cached |
- Journaling: log metadata first, replay on recovery; avoids full fsck.
- RAID 0: striping, no redundancy. RAID 1: mirroring, capacity . RAID 5: capacity disks, survives 1 failure. RAID 6: , survives 2.