cheat sheet

Everything in one place

One-line definitions, comparison tables, formulas, the numbers examiners love, and the questions that keep coming back. Organised by lecture, printable as a PDF.

Open terminal
L1

Introduction to Operating Systems

10 definitions · 3 tables · 0 formulas · 5 exam prompts

Full notes

Definitions in one line

Operating system
Software that manages hardware and software resources and provides common services for programs.
Four components
Hardware, operating system, application programs, users. A compiler is an application program.
Device controller
Owns one device type and a local buffer. Signals completion to the CPU with an interrupt.
Multiprocessor
Several CPUs sharing memory under one OS. Gains: throughput, economy of scale, reliability (graceful degradation).
SMP vs asymmetric
SMP: every CPU does every task (Intel Core, Ryzen). Asymmetric: each CPU has a fixed role (ARM big.LITTLE).
Dual mode
User mode (mode bit 1) for applications, kernel mode (mode bit 0) for privileged instructions.
Multiprogramming
Several jobs in memory at once so the CPU is never idle. Needs CPU scheduling and memory management.
Batch OS
Operator groups similar punch-card jobs and runs them without interaction. High throughput, no responsiveness.
Distributed OS
Autonomous computers with their own CPU and memory cooperating over a network. Network failure stops everything.
Real-time OS
Strict response-time requirements. Hard: no missed deadline (airbag). Soft: occasional lateness tolerated (gaming).

Numbers to remember

  • 4components of a computer system
  • 1 / 0mode bit in user mode / kernel mode
  • 3advantages of multiprocessors: throughput, economy of scale, reliability
  • 5types of OS on the slides: batch, multiprogramming, multiprocessor, distributed, real-time

Compare

Dual mode

AspectUser modeKernel mode
Mode bit10
PrivilegeLowerHigher
Hardware accessRestrictedUnrestricted
Memory accessLimited to own spaceFull
RunsUser applicationsOS and kernel components
Crash impactOnly that process diesWhole system can crash
Exception handlingLimitedComprehensive
Switch inReturn from system call or interruptSystem call, interrupt, or trap

Types of operating system

TypeKey ideaMain advantageMain disadvantageExample
BatchOperator runs batches of similar jobsHigh throughput, easy to repeat big workHard to debug, one failed job stalls the restPayroll, bank statements
MultiprogrammingSeveral jobs in memory, CPU switches among themHigh CPU utilisationNeeds scheduling and memory managementClassic mainframes, every modern OS
MultiprocessorSeveral CPUs, shared memory, one OSThroughput, reliability, cost sharingExpensive, complex, needs large memorySMP servers, desktop multicore
DistributedIndependent machines over a networkScalable, one node failing does not stop othersNetwork failure stops all communicationClusters, cloud
Real-timeGuaranteed response timeMaximum device utilisation, fast task switchingFew tasks at once, complex algorithmsMissile control, robots, airbags

SMP vs asymmetric multiprocessing

Symmetric (SMP)Asymmetric
RolesAll processors equal, each runs OS and user codeMaster processor schedules, others run user code
SchedulingEach processor self-schedulesMaster decides everything
ExampleIntel Core, AMD RyzenARM Cortex-A73 + A53 big.LITTLE

Classic exam questions

  1. 1Why can a crash in user mode not bring down the OS, while a crash in kernel mode can?
  2. 2Differentiate symmetric and asymmetric multiprocessing with one example each.
  3. 3List the four core functions of an OS and the five other important activities.
  4. 4Hard vs soft real-time systems: definition and two examples of each.
  5. 5What does the device controller do when its operation finishes, and why is that mechanism needed?
L2

System Calls and Interrupts

10 definitions · 3 tables · 1 formulas · 5 exam prompts

Full notes

Definitions in one line

System call
The interface between a process and the OS. Invoked through a trap into kernel mode.
API
Library functions that wrap system calls. Three common ones: Win32, POSIX, Java API.
System call table
Indexed by system call number, each entry holds the address of the kernel routine.
Interrupt
A signal that breaks the current sequence of execution. The OS is interrupt driven: it runs only when one arrives.
Hardware interrupt
Raised by a device, asynchronous, can occur at any time (keyboard, timer, disk completion).
Trap
Software interrupt raised intentionally by a program: system call instruction, breakpoint.
Exception
Unplanned interrupt from an error. Fault is recoverable (page fault). Abort is not (divide by zero on the slides).
Maskable interrupt
Can be ignored by setting a bit in the interrupt mask register (IMR). Lower priority. 8085 RST 5.5, 6.5, 7.5.
Non-maskable interrupt
No mask bit, can never be ignored. Highest priority. 8085 TRAP.
Interrupt vector table
One entry per interrupt number holding the handler address. The number indexes the table.

Numbers to remember

  • 3common APIs: Win32, POSIX, Java
  • 6system call categories: process control, file, device, information, communication, protection
  • RST 5.5 / 6.5 / 7.5maskable interrupts of the 8085
  • TRAPthe 8085 non-maskable interrupt

Compare

Interrupt, trap, exception

Hardware interruptTrapException
SourceExternal deviceProgram, on purposeProgram, by error
TimingAsynchronous, any timeSynchronous with the instructionSynchronous with the instruction
PurposeDevice needs attentionRequest OS serviceReport a fault or abort
ExampleTimer tick, keypress, disk doneSystem call, debugger breakpointPage fault, divide by zero

Maskable vs non-maskable

MaskableNon-maskable
Can be disabled?Yes, via IMR bitNo
PriorityLowerHigher (timers, critical hardware)
Handled whenAfter the current instructionImmediately, state pushed on the stack
8085 exampleRST 5.5, RST 6.5, RST 7.5TRAP

Windows vs UNIX system calls

CategoryWindowsUNIX
Process controlCreateProcess() ExitProcess() WaitForSingleObject()fork() exit() wait()
File manipulationCreateFile() ReadFile() WriteFile() CloseHandle()open() read() write() close()
Device manipulationSetConsoleMode() ReadConsole() WriteConsole()ioctl() read() write()
Information maintenanceGetCurrentProcessID() SetTimer() Sleep()getpid() alarm() sleep()
CommunicationCreatePipe() CreateFileMapping() MapViewOfFile()pipe() shmget() mmap()
ProtectionSetFileSecurity() InitializeSecurityDescriptor()chmod() umask() chown()

Formulas

  • Interrupt handling sequence

    pause current instruction -> save context -> index vector table -> run handler -> restore context

    Priority levels in the interrupt controller decide order when several arrive together.

Classic exam questions

  1. 1What is stored in the system call table and how is it indexed?
  2. 2Why do programs use an API instead of calling system calls directly?
  3. 3Write the system call sequence for copying one file to another.
  4. 4Distinguish an interrupt, a trap and an exception with one example each.
  5. 5What determines the order when several interrupts arrive at the same time?
L3

OS Design and Structures

9 definitions · 3 tables · 0 formulas · 5 exam prompts

Full notes

Definitions in one line

User goals
Convenient, easy to learn, reliable, safe, fast.
System goals
Easy to design, implement and maintain. Flexible, reliable, error-free, efficient.
Policy vs mechanism
Policy: what will be done. Mechanism: how. Timer is a mechanism, how long to set it is policy.
Simple structure
No clear layering, any part touches any other. MS-DOS. Fast but one bad program crashes everything.
Monolithic
All services in one kernel and one address space. Original UNIX. Fast, but one failure kills all, hard to extend.
Layered
Layer 0 hardware, layer N user interface. Each layer uses only lower ones. Easy to debug, hard to plan, slow across layers.
Microkernel
Only minimal process, memory and IPC in the kernel. Rest as user programs talking via messages. Portable, secure, slower.
Modular
Core kernel plus loadable kernel modules that call each other directly. Linux, Solaris. Slide verdict: best current methodology.
System programs
Convenient environment for development and execution. Six types from file manipulation to communication.

Numbers to remember

  • 5structures: simple, monolithic, layered, microkernel, modular
  • 6types of system programs
  • layer 0 / layer Nhardware / user interface in the layered approach

Compare

OS structures

StructureIdeaAdvantagesDisadvantagesExample
SimpleNo well-defined layersSimple to develop, superior performanceOne bad program breaks the OS, no data hidingMS-DOS
MonolithicEverything in one kernelSimple to design, fast (same address space)One failed service kills the system, inflexibleOriginal UNIX
LayeredStack of layers, each uses the one belowSimple construction and debuggingCareful planning needed, slow through layersTHE, early layered systems
MicrokernelMinimal kernel, services in user modePortable, secure, testable, isolated failuresMessage passing hurts performance, complexMach, QNX, Minix
ModularCore kernel plus loadable modulesSmall kernel, no message passing, modules call directlyModule interfaces must be designed wellLinux, Solaris

Policy vs mechanism

PolicyMechanism
QuestionWhat will be done?How is it done?
ChangesOften, per site or userRarely
ExampleHow long the timer runs for a userThe timer hardware and its interrupt
Why separateMaximum flexibility when policy changes later

Six types of system programs

TypeExamples
File manipulationcreate, delete, copy, rename, print, dump, list
Status informationdate, time, free memory or disk, number of users, logs
File modificationtext editors, search and transform commands
Programming language supportcompilers, assemblers, debuggers, interpreters
Program loading and executionabsolute loaders, relocatable loaders, linkage editors, overlay loaders
Communicationmessaging, browsing, email, remote login, file transfer

Classic exam questions

  1. 1Distinguish policy from mechanism with the timer example. Why separate them?
  2. 2Compare monolithic and microkernel structures: performance, reliability, portability.
  3. 3Why is the modular approach described as combining the best of layered and microkernel designs?
  4. 4Give the advantages and disadvantages of the layered approach.
  5. 5List the six types of system programs with an example of each.
L4

Processes and Operations on Processes

11 definitions · 4 tables · 3 formulas · 6 exam prompts

Full notes

Definitions in one line

Process
A program in execution: code plus current activity (program counter, registers, stack, resources). Active entity.
Program
A passive file of instructions on disk. Becomes a process when loaded into memory.
Process memory
Text (code), data (globals, statics), heap (malloc, grows up), stack (locals, grows down).
PCB
State, program counter, CPU registers, scheduling info, memory info, accounting info, I/O status.
fork()
Copies the caller. Returns 0 to the child, the child PID to the parent, negative on failure. Both continue after the call.
exec()
Replaces the process image with a new program. Same PID and PPID, no new process. Code after a successful exec never runs.
wait()
Parent blocks until a child ends. Returns the child's PID and stores its exit status. Reading the status is reaping.
exit()
Terminates the caller, returns an integer status to the parent. OS frees memory, files, I/O buffers.
Zombie
Exited child not yet reaped by wait(). Holds only a process-table entry, no memory or CPU.
Orphan
Child whose parent exited first. Adopted immediately by init (PID 1).
Cascading termination
Some systems kill all children and grandchildren when a parent exits. UNIX re-parents them to init instead.

Numbers to remember

  • 1PID of init, the root of the process tree and adopter of orphans
  • 0fork() return value in the child
  • 4sections of process memory: text, data, heap, stack
  • 7process states on the slides, including the two suspend states
  • 7children created by if(!fork()){ if(!fork()) fork(); } fork();

Compare

Program vs process

ProgramProcess
NaturePassiveActive
LivesOn disk as an executableIn main memory
HasInstructionsProgram counter, registers, stack, heap, open files
CountOne fileMany processes can run one program

fork() vs exec()

fork()exec()
Creates a new process?Yes, a childNo, replaces the caller
PIDChild gets a new PIDUnchanged (PPID too)
Address spaceCopy of the parent, separateOverwritten with the new program
Return0 in child, child PID in parent, negative on errorOnly returns on failure
After the callBoth run the next instructionOnly the new program runs; old code after exec is skipped
Typical useShell spawns a childChild becomes the command

Seven process states and where they live

StatePresent inEnter fromLeave to
NewSecondary memoryProgram initiatedReady (loaded by long-term scheduler)
ReadyMain memoryNew, Run (preempted), Wait (I/O done), Suspend ReadyRun (dispatched), Suspend Ready (swapped out)
RunMain memoryReadyTerminate, Wait (I/O), Ready (preempt)
Block / WaitMain memoryRun (needs I/O or resource)Ready (event done), Suspend Wait (swapped out)
Suspend ReadySecondary memoryReady (memory full), Suspend Wait (I/O done)Ready (memory available)
Suspend WaitSecondary memoryWait (memory full)Suspend Ready (I/O done)
TerminatenoneRun (exit)

Wait never goes straight to Run. Suspend states exist only because main memory was full.

Zombie vs orphan

ZombieOrphan
Who diedThe childThe parent
Who is aliveThe parent (has not called wait)The child (still running)
CostsOne process-table entryNothing special, re-parented to init
FixParent calls wait()init reaps it when it exits

Formulas

  • Processes after n forks

    2^n processes, 2^n - 1 children

    Three sequential fork() calls print hello 8 times and create 7 children.

  • fork() return value

    child: 0    parent: child PID (> 0)    failure: < 0

    if (fork() == 0) is the child branch. if (!fork()) means the same thing.

  • Exit status

    wait(&status); WIFEXITED(status) ? WEXITSTATUS(status) : signal

    Only the low 8 bits of the exit code survive: exit(64) gives 64, exit(256) gives 0.

Classic exam questions

  1. 1Predict the output of a program with three consecutive fork() calls followed by a printf.
  2. 2In pid = fork(); if (pid == 0) ++x; else --x; what does each process print, and why do they not share x?
  3. 3How many child processes does if(!fork()){ if(!fork()) fork(); } fork(); create? Draw the tree.
  4. 4Why does a printf placed after a successful execv() never run? What is printed if execv fails?
  5. 5Define zombie and orphan. Which system call clears a zombie, and who adopts an orphan?
  6. 6List the seven fields of a PCB and say which are saved during a context switch.
L5

Process Scheduling

9 definitions · 3 tables · 2 formulas · 5 exam prompts

Full notes

Definitions in one line

Job queue
All processes in the system. The only queue that can hold processes not yet in main memory.
Ready queue
Processes in main memory waiting for the CPU. Linked list of PCBs with a header pointing to first and last.
Device queue
Processes waiting for one particular I/O device. One per device.
Long-term scheduler
Job scheduler. Admits processes into memory. Rare (seconds, minutes), can be slow. Controls the degree of multiprogramming.
Short-term scheduler
CPU scheduler. Picks the next ready process. Runs every few milliseconds, must be fast. Often the only scheduler.
Medium-term scheduler
Swaps processes out to disk and back to reduce the degree of multiprogramming.
I/O bound vs CPU bound
I/O bound: many short CPU bursts. CPU bound: few long bursts. The long-term scheduler wants a good mix.
Context switch
Save the old process state in its PCB, load the new one. Pure overhead. Cost depends on hardware and PCB complexity.
Stack vs queue
Stack is LIFO (push, pop, one end). Queue is FIFO (enqueue at rear, dequeue at front).

Numbers to remember

  • 9%CPU wasted when a 10 ms decision precedes a 100 ms run
  • 12.5%CPU spent scheduling when 25 decisions of 5 ms happen per second
  • 3schedulers: long, short, medium term
  • 3queues: job, ready, device

Compare

The three schedulers

Long-termShort-termMedium-term
Also calledJob schedulerCPU schedulerSwapper
SelectsWhich jobs enter memory (New to Ready)Which ready process runs (Ready to Run)Which process to swap out or in
FrequencySeconds or minutesMillisecondsAs memory pressure demands
SpeedCan be slowMust be very fastModerate
ControlsDegree of multiprogramming, process mixCPU allocationReduces degree of multiprogramming
Present inBatch systems; often absent in time-sharingEvery systemSystems that swap

Scheduling queues

QueueHoldsWhere in memory
Job queueEvery process in the systemMay include secondary memory
Ready queueProcesses waiting only for the CPUMain memory
Device queueProcesses waiting for a specific deviceMain memory

What happens after dispatch

EventProcess goes toThen
Issues I/O requestDevice queue (Waiting)Ready queue when the I/O completes
Creates a child and waitsWaitingReady queue when the child terminates
Interrupt or quantum expiryReady queue directlyDispatched again later
TerminatesRemoved from all queuesPCB and resources deallocated

Formulas

  • Scheduling overhead

    overhead % = decision time / (decision time + run time) x 100

    10 ms to decide, 100 ms to run: 10/110 = 9% wasted.

  • Overhead per second

    overhead % = (decisions x decision time) / total time

    25 decisions of 5 ms each in 1000 ms: 125/1000 = 12.5%.

Classic exam questions

  1. 1Compare the three schedulers on frequency, speed and what they control.
  2. 2Why must the short-term scheduler be fast while the long-term scheduler can be slow?
  3. 3A scheduler needs 5 ms per decision and makes 25 decisions per second. What fraction of CPU time is overhead?
  4. 4Which queue can contain processes that are not in main memory, and why?
  5. 5Why is context-switch time called overhead, and what hardware feature reduces it?
L6

CPU Scheduling

10 definitions · 5 tables · 9 formulas · 7 exam prompts

Full notes

Definitions in one line

Dispatcher
Gives the CPU to the chosen process: context switch, switch to user mode, jump to the right instruction.
Non-preemptive
A process keeps the CPU until it terminates or blocks. FCFS, non-preemptive SJF, non-preemptive priority.
Preemptive
A running process can be forced off the CPU (quantum expiry, higher-priority arrival). SRTF, RR, preemptive priority, MLQ, MLFQ.
Criteria
Maximise CPU utilisation and throughput. Minimise turnaround, waiting and response time.
Convoy effect
In FCFS one long burst holds up every short process behind it.
Starvation
A process never gets the CPU because others keep taking priority. Cure: aging.
Time quantum
The RR slice. Large q behaves like FCFS. Small q behaves like processor sharing but multiplies context switches.
MLQ
Fixed subqueues by process property, each with its own algorithm, higher queue first. Lower queues can starve.
MLFQ
MLQ where processes move between queues: heavy CPU users sink, long waiters rise (aging). Windows and macOS.
Exponential averaging
Predicts the next burst from history so SJF can be approximated.

Numbers to remember

  • 32Windows priority levels, 0 to 31
  • 16 to 31Windows soft real-time priorities, need privileges; 0 to 15 normal; 0 reserved for the OS
  • O(log N)Linux CFS complexity (pick in O(1), reinsert in O(log N))
  • 4macOS MLFQ bands: normal, system high, kernel only, real-time
  • 17 vs 3FCFS average WT for bursts 24, 3, 3 in order P1 P2 P3 vs P2 P3 P1
  • 4 vs 3average WT for the slide set P1=0/7 P2=2/4 P3=4/1 P4=5/4 under SJF vs SRTF
  • 73average WT for RR q=20 with bursts 53, 17, 68, 24
  • 7context switches in the slide MLQ example (P4 TAT 12, P6 TAT 16)

Compare

Scheduling algorithms

AlgorithmPreemptive?Selection ruleStarvation?Best forWeakness
FCFSNoEarliest arrival, FIFO queueNoBatch, simplicityConvoy effect, order-dependent WT, bad for time-sharing
SJFNoSmallest next CPU burst, FCFS tie-breakYes (long jobs)Minimum average WT when all arrive together, long-term schedulingBurst must be known, cannot run at short-term level
SRTFYesSmallest remaining time, re-checked on every arrivalYes (long jobs)Minimum average WT with staggered arrivalsNeeds burst estimates, more context switches
PriorityEitherHighest priority (smallest number on the slides)Yes, fixed by agingSystems where importance differsChoosing priorities, indefinite blocking
Round robinYesFIFO with a time quantum qNoTime-sharing, interactive systemsLong average WT, quantum tuning
MLQYes (between queues)Highest non-empty queue, its own rule insideYes (low queues)Mixed foreground/background workloadsPermanent assignment, inflexible
MLFQYesLike MLQ but processes migrate by behaviourNo (aging)General-purpose OSComplex, many parameters, overhead

Preemptive vs non-preemptive

Non-preemptivePreemptive
CPU released whenProcess terminates or blocksAlso on quantum expiry or higher-priority arrival
Response timeCan be poorBetter for interactive work
OverheadFewer context switchesMore context switches, needs a timer
Shared dataSafe within a burstNeeds synchronisation
AlgorithmsFCFS, SJF, priority (NP)SRTF, RR, priority (P), MLQ, MLFQ

Scheduling criteria

CriterionMeaningGoal
CPU utilisationFraction of time the CPU is busyMaximise
ThroughputProcesses completed per unit timeMaximise
Turnaround timeSubmission to completionMinimise average
Waiting timeTime spent in the ready queueMinimise average
Response timeSubmission to first responseMinimise, matters most for interactive
DeadlinesReal-time constraints metMeet all (hard) or most (soft)

MLQ vs MLFQ

Multilevel queueMultilevel feedback queue
Queue assignmentPermanent, by property (priority, type, memory)Dynamic, by observed CPU behaviour
MovementNoneDown when CPU heavy, up when waiting long (aging)
StarvationPossible in lower queuesPrevented by aging
ComplexitySimplerComplex, more overhead

Multiprocessor scheduling

AsymmetricSymmetric
Who schedulesOne master processorEvery processor for itself
Others doOnly user codeEverything
QueuesMaster's queueCommon ready queue or private per-CPU queues
IssuesMaster is a bottleneckLocking, shared data, cache coherence

Formulas

  • Turnaround time

    TAT = CT - AT

    Completion minus arrival. Also WT + BT when there is no I/O.

  • Waiting time

    WT = TAT - BT

    Time in the ready queue only.

  • Response time

    RT = first CPU allocation - AT

    Zero for a process that starts the moment it arrives.

  • CPU utilisation

    busy time / total time x 100

    Per core in multiprocessor questions: 12/17 = 70.58%.

  • Throughput

    processes completed / total time
  • Exponential averaging

    tau(n+1) = alpha * t(n) + (1 - alpha) * tau(n),   0 <= alpha <= 1

    alpha 0.5, tau1 5, bursts 4 8 5 6: tau2 4.5, tau3 6.25, tau4 5.625, tau5 5.8125.

  • Round robin bound

    no process waits more than (n - 1) q between turns

    n processes in the ready queue, quantum q.

  • Round robin limits

    q -> large: FCFS      q -> small: processor sharing at 1/n speed
  • Priority

    smallest integer = highest priority (default on the slides)

    Read the question: one slide example flips it to larger = higher.

Classic exam questions

  1. 1Draw the Gantt chart and compute average WT and TAT for FCFS, SJF, SRTF and RR (q given) on a table of arrivals and bursts.
  2. 2Explain the convoy effect with the 24, 3, 3 example and show how reordering changes average waiting time.
  3. 3Why is SJF optimal, and why can it not be implemented exactly at the short-term level?
  4. 4Estimate the next CPU burst with exponential averaging given alpha, tau1 and a list of actual bursts.
  5. 5What happens to RR as q grows very large or very small? Discuss the context-switch trade-off.
  6. 6MLQ with Q1 SRTF, Q2 FCFS, Q3 RR: draw the Gantt chart, state queue contents at a given time, count context switches.
  7. 7Three-core global preemptive priority scheduling: draw per-CPU Gantt charts and compute per-CPU utilisation.
L7

Threads

9 definitions · 4 tables · 1 formulas · 5 exam prompts

Full notes

Definitions in one line

Thread
Basic unit of CPU utilisation, a lightweight process. Own thread ID, PC, registers, stack. Shares code, data, open files.
Heavyweight process
A traditional process with a single thread of control.
Benefits
Responsiveness, resource sharing, economy, utilisation of multiprocessors.
User-level thread
Managed by a library in user space. Kernel unaware. Fast, portable, but one blocking call blocks the whole process.
Kernel-level thread
Managed by the kernel. Slower to create and switch, but blocking one thread does not block the others and they can use several CPUs.
Many-to-one
Many user threads on one kernel thread. Efficient, but no parallelism and a blocking call blocks all. Green Threads.
One-to-one
Each user thread has a kernel thread. Parallelism and non-blocking, but kernel-thread overhead. Windows, Linux.
Many-to-many
User threads multiplexed onto a smaller or equal number of kernel threads. Avoids both shortcomings. IRIX, HP-UX, Solaris 8.
Two-level
Many-to-many plus the option to bind a user thread to one kernel thread.

Numbers to remember

  • 4benefits of threads: responsiveness, resource sharing, economy, multiprocessor utilisation
  • 3 + 1models: many-to-one, one-to-one, many-to-many, plus the two-level variant
  • 2maximum concurrent user threads on 2 cores in the two-level exam question

Compare

Process vs thread

ProcessThread
Address spaceOwnShared with sibling threads
OwnsCode, data, heap, files, PCBThread ID, PC, registers, stack
Creation costHigh (memory, resources)Low
Context switchSlow, changes address spaceFast, no address-space change
CommunicationShared memory or message passing, set up explicitlyShared memory by default
IsolationA crash is containedOne thread can corrupt the others

User-level vs kernel-level threads

User-level threadsKernel-level threads
Managed byThread library in user spaceThe kernel
Kernel aware?NoYes
Create and switchFast, no mode switchSlower, needs mode switch
Blocking system callBlocks the entire processBlocks only that thread
Multiprocessor useCannot run in parallelThreads on different CPUs
PortabilityAny OSOS specific

Multithreading models

ModelMappingBlocking callParallel on multicore?CostExamples
Many-to-oneN user : 1 kernelBlocks whole processNoLowestSolaris Green Threads, GNU Portable Threads
One-to-one1 user : 1 kernelBlocks one threadYesKernel thread per user threadWindows 95/98/NT/2000, OS/2, Linux
Many-to-manyN user : M kernel, M <= NBlocks one kernel threadYesModerateIRIX, HP-UX, Solaris 8
Two-levelMany-to-many plus bound threadsBound thread behaves 1:1YesModerateIRIX, HP-UX, Tru64, Solaris 8 and earlier

Per-thread vs shared

Private to each threadShared by all threads of a process
Thread ID, program counter, register set, stackCode section, data section, heap, open files, signals, other OS resources

Formulas

  • Threads that can run at once

    min(cores, kernel threads with runnable work)

    Two-level example: 5 user threads, 2 bound plus 3 over 2 kernel threads, 2 cores: at most 2 run at once.

Classic exam questions

  1. 1What is private to a thread and what is shared? Why does that make thread switching cheaper?
  2. 2Why does a blocking system call in a many-to-one system block the whole process?
  3. 3Compare user-level and kernel-level threads on creation cost, blocking, and multiprocessor use.
  4. 4Which model offers the best balance of concurrency and flexibility, and what problem does it fix?
  5. 5Why do modern operating systems prefer kernel-level threads despite their overhead?
L8

Deadlock

11 definitions · 5 tables · 4 formulas · 7 exam prompts

Full notes

Definitions in one line

Deadlock
Each process in a set waits for an event only another process in the set can cause. None can run, release, or be awakened.
Resource use
Request, use, release. If denied: block, continue without it, or fail.
Preemptable resource
Can be taken away safely (CPU, memory page). Non-preemptable cannot (printer mid-job, semaphore). Deadlocks need non-preemptable ones.
Four conditions
Mutual exclusion, hold and wait, no preemption, circular wait. All four must hold at once.
RAG
Processes (circles), resource types (boxes with instance dots). Request edge P to R, assignment edge R to P, claim edge dashed P to R.
Cycle rule
No cycle: no deadlock. Cycle with single instances: deadlock. Cycle with multiple instances: deadlock possible.
Prevention
Deny one of the four conditions in the design. No future knowledge needed, low utilisation.
Avoidance
Processes declare maximum needs. Grant a request only if the result is a safe state.
Safe state
A safe sequence exists in which each process can finish with what is free plus what earlier ones release. Unsafe does not mean deadlocked.
Banker's algorithm
Avoidance for multiple instances. Available, Max, Allocation, Need. Pretend to allocate, run the safety check.
Detection and recovery
Let deadlock happen, find it (wait-for graph or Banker-style detection), then kill, preempt or roll back.

Numbers to remember

  • 4necessary conditions for deadlock
  • 3handling strategies: never enter, detect and recover, ignore
  • P1, P0, P2safe sequence in the 12 tape drive example (max 10/4/9, allocated 5/2/2, 3 free)
  • P1, P3, P4, P0, P2safe sequence in the Banker's example with Available (3,3,2)
  • O(m n^2)detection with multiple instances
  • O(n^2)cycle search in a wait-for graph

Compare

Prevention vs avoidance

Deadlock preventionDeadlock avoidance
ApproachMake one of the four conditions impossibleGrant requests only when the result stays safe
Information neededNone about future requestsMaximum need of every process in advance
When decidedAt design time, for all requestsAt run time, per request
UtilisationLow, resources idle while held earlyBetter, but still conservative
AlgorithmsOrdering of resources, request all at start, release on waitRAG claim-edge algorithm, Banker's algorithm
CostStarvation possible, poor throughputOverhead of the safety check on every request

Four conditions and how prevention attacks each

ConditionMeaningPreventionCost
Mutual exclusionOne process at a time per resourceCannot be denied for non-sharable resourcesNot practical
Hold and waitHolding one resource while waiting for anotherRequest all resources before starting, or request only when holding noneLow utilisation, starvation
No preemptionResources released only voluntarilyIf a request cannot be met, release everything held and retry laterWasted work, restarts
Circular waitChain of processes each waiting on the nextTotal ordering of resource types, request in increasing orderProgrammer discipline, possible inefficiency

Three ways to handle deadlock

StrategyIdeaUsed by
Never enterPrevention or avoidanceReal-time and safety-critical systems
Enter and recoverDetection algorithm plus recoveryDatabases, some servers
Ignore (ostrich)Pretend deadlocks never happenMost general-purpose OSes including UNIX

Detection by resource multiplicity

Single instance per typeMultiple instances per type
StructureWait-for graph (RAG with resources removed)Available, Allocation, Request matrices
Deadlock ifThe graph has a cycleBanker-style pass leaves some process unfinished
ComplexityO(n^2) for cycle detectionO(m n^2)

Recovery methods

MethodWhat happensTrade-off
Abort allKill every deadlocked processSimple, loses all their work
Abort one at a timeKill a victim, re-check, repeatLess loss, repeated detection runs
Resource preemptionTake a resource from its owner and give it to anotherFine for a printer, bad for a half-written record
RollbackRestore a victim to a periodic checkpointNeeds checkpoint files, may repeat work

Formulas

  • Need matrix

    Need[i][j] = Max[i][j] - Allocation[i][j]
  • Safety algorithm

    Work = Available; Finish[] = false
    repeat: pick i with Finish[i] = false and Need_i <= Work; Work += Allocation_i; Finish[i] = true
    safe iff every Finish[i] = true

    The order in which you pick processes is a safe sequence.

  • Resource-request algorithm

    Request_i <= Need_i else error
    Request_i <= Available else wait
    pretend: Available -= Request; Allocation_i += Request; Need_i -= Request
    safe ? grant : restore and wait
  • Complexities

    wait-for graph cycle: O(n^2)      Banker-style detection: O(m n^2)

    n processes, m resource types.

Classic exam questions

  1. 1State the four necessary conditions and explain how prevention attacks each. Which one cannot be attacked?
  2. 2Given a RAG, decide whether deadlock exists. What changes when a resource has multiple instances?
  3. 3Given Allocation and Max with Available, compute Need, run the safety algorithm, and give a safe sequence.
  4. 4P1 requests (1,0,2). Can it be granted? Then check (3,3,0) by P4 and (0,2,0) by P0.
  5. 5Explain why an unsafe state is not necessarily a deadlocked state.
  6. 6Compare deadlock prevention and avoidance on information needed and utilisation.
  7. 7Describe the recovery options and give one application where killing a process is acceptable and one where it is not.