Skip to content

Preemptive scheduling

Category: Scheduling · Status: stub

One line: The scheduler may stop a running thread at any moment, usually on a timer interrupt, and run another — no thread can hog the CPU, and any step can be interrupted.

Also called: preemption, preemptive multitasking.

How it connects

flowchart LR
  n_cooperative_scheduling["Cooperative scheduling"]
  n_earliest_deadline_first["Earliest deadline first"]
  n_preemptive_scheduling["Preemptive scheduling"]
  n_rate_monotonic_scheduling["Rate-monotonic scheduling"]
  n_scheduling_policy["Scheduling policy"]
  n_cooperative_scheduling ---|vs| n_preemptive_scheduling
  n_earliest_deadline_first -->|is a| n_preemptive_scheduling
  n_preemptive_scheduling -->|is a| n_scheduling_policy
  n_rate_monotonic_scheduling -->|is a| n_preemptive_scheduling
  classDef center stroke-width:3px
  class n_preemptive_scheduling center
  classDef outside stroke-dasharray: 4 3
  class n_cooperative_scheduling,n_earliest_deadline_first,n_rate_monotonic_scheduling,n_scheduling_policy outside

In each language

Go Goroutines have been asynchronously preemptible since Go 1.14 ↗; before that, a loop without function calls could hold up the scheduler
Python Threads get timeslices set by the switch interval ↗, and the operating system picks which runs next: the interpreter has no scheduler of its own
JavaScript Never: a function runs to completion ↗ before other code on the same thread runs
Haskell GHC preempts a thread ↗ only when it allocates memory, so a tight loop that never allocates can lock out other threads
The operating system sched(7): a higher-priority thread preempts a lower one; SCHED_RR also takes turns by time slice

Where to read more