Cooperative multitasking

Also known as: non-preemptive multitasking

On this page 7

Cooperative multitasking, also called non-preemptive multitasking, is a scheduling model in which a running task keeps the processor until it voluntarily yields control back to the scheduler, which then selects the next task to run. In the field of operating systems, and specifically within processes & scheduling, it is the historical alternative to preemptive multitasking: the scheduler never forcibly interrupts a task, so the correctness and responsiveness of the whole system depend on every program behaving well.

The model powered early personal computer platforms such as Windows 3.x and classic Mac OS, and it survives today inside user-space runtimes, event loops, coroutine libraries and many small embedded kernels, where its low overhead and predictable switching points remain valuable.

How cooperative multitasking hands over the processor

Cooperative multitasking works by placing explicit yield points in program code, at which a task returns control to the scheduler. A task yields either by calling a dedicated function such as yield(), or implicitly when it makes a blocking call such as waiting for input, reading a message queue or sleeping. Between those points, the task runs without interruption from the scheduler.

When a task yields, the scheduler saves its context (registers, stack pointer and program counter), places it back in the run queue and restores the context of the next ready task. Because switches happen only at known locations, the saved state is often smaller than in a preemptive design, and the code between yields can assume that no other task in the same address space modifies shared data at the same moment.

The following table summarizes the typical ways a task gives up the processor in a cooperative design.

Yield mechanismTriggerTypical example
Explicit yieldTask calls a yield functionyield() in an embedded loop
Blocking waitTask waits for an event or messageMessage loop retrieving the next event
Sleep or delayTask requests a timed pauseDelay call in a small real-time kernel
Await pointCoroutine awaits an unfinished operationawait on a network read in an event loop
Task completionTask returns from its entry functionA finished job leaving the scheduler
Common yield mechanisms

Cooperative multitasking compared with preemptive multitasking

The central difference is who decides when a switch occurs. In cooperative multitasking the running task decides; in preemptive multitasking a timer interrupt lets the kernel suspend a task at the end of its time slice or when a higher-priority task becomes ready, a mechanism known as preemption.

This single difference shapes robustness, latency and programming effort, as the comparison below shows.

PropertyCooperative multitaskingPreemptive multitasking
Switch decisionRunning task yieldsKernel interrupts via timer
Effect of an infinite loopEntire system can freezeOnly the faulty task is affected
Context switch overheadLow, at predictable pointsHigher, at arbitrary points
Shared data protectionOften needs no locks between yieldsRequires locks or atomic operations
Worst-case latencyBounded by the longest code path between yieldsBounded by time slice and priorities
Typical modern useEvent loops, coroutines, small kernelsGeneral-purpose desktop and server systems
Cooperative and preemptive scheduling side by side

Neither model is strictly superior. Preemption provides isolation between untrusted programs, while cooperation provides simplicity and determinism inside a single trusted program. For that reason, most modern systems combine both: a preemptive kernel schedules threads, and individual applications run cooperative tasks on top of those threads.

Where cooperative multitasking is used today

Cooperative multitasking is now used mainly inside a single process or a single trusted firmware image, rather than as the scheduler of a general-purpose operating system. In these settings all tasks are written by the same team, so the main weakness, a task that never yields, can be controlled by code review and testing.

The table below lists representative environments and the reason the cooperative model suits each one.

EnvironmentCooperative unitWhy the model fits
Windows 3.x and classic Mac OSApplication message loopLimited hardware and no memory protection at the time
Event-loop runtimesCallback or coroutineOne thread serves thousands of I/O-bound connections
Coroutine librariesFiber, green thread or async taskCheap switches and simple shared state
Small embedded kernelsTask in a super loopTiny memory footprint and predictable timing
Real-time systems with optional cooperationTask configured as non-preemptibleAvoids switch overhead for short critical work
Representative environments

Cooperative scheduling suits I/O-bound processes particularly well, because such tasks spend most of their time waiting and therefore yield naturally. CPU-bound processes are a poor fit unless their long computations are split into chunks with explicit yields, or moved to a separate preemptively scheduled thread.

Limitations and failure modes

The defining weakness of cooperative multitasking is that one misbehaving task can starve all others. If a task enters an infinite loop or performs a long computation without yielding, no other task runs, input is not processed and the user perceives a frozen system. On Windows 3.x and classic Mac OS, this was a common cause of complete lockups that required a restart.

Other problems are subtler and appear in modern event-loop code as well:

  • Hidden blocking calls, such as a synchronous file read inside an asynchronous handler, stall every task sharing the loop.
  • Latency spikes occur when a rarely executed code path runs much longer between yields than the common path.
  • False safety assumptions arise when code that relied on running without interruption is later moved onto preemptive threads.
  • No multi-core parallelism is available to tasks sharing one cooperative scheduler, because only one task runs at a time.

The following table maps typical symptoms to their usual cause and a practical remedy.

SymptomLikely causeRemedy
Whole application stops respondingTask loops without yieldingAdd yield points or a watchdog timer
Periodic stutter in an event loopBlocking call inside a handlerReplace with an asynchronous call or worker thread
One core busy, others idleAll work on one cooperative schedulerRun several schedulers or offload to threads
Missed deadlines in firmwareLong path between yieldsSplit the work into shorter steps
Symptoms, causes and remedies

Designing tasks for a cooperative scheduler

Good cooperative design starts with a yield budget: a maximum time any task may run between yields, chosen from the latency the system must guarantee. Developers then measure or estimate the longest execution path in each task and split work that exceeds the budget into smaller steps, often by turning loops into state machines that resume where they stopped.

Robust systems also add defensive mechanisms. Embedded designs commonly use a hardware watchdog timer that resets the device if the scheduler does not run within a set interval. Event-loop runtimes provide diagnostics that flag callbacks running longer than a threshold, and teams often route CPU-heavy work to separate threads so that the cooperative loop stays responsive.

A non-technical view

Cooperative multitasking is like a group of people sharing one microphone at a meeting with no chairperson. Each speaker talks, then hands the microphone to the next person. If everyone keeps their remarks short, the meeting runs smoothly and nobody waits long.

The problem appears when one speaker refuses to stop. Nobody can take the microphone away, so the whole meeting is stuck. Modern computers usually add a chairperson who can cut a speaker off, but the polite hand-over approach still works well inside programs where every speaker is known and trusted.

A concrete example

A firmware engineer builds a battery-powered environmental sensor on a microcontroller with 32 KB of RAM. The device runs four tasks: reading the temperature sensor, filtering the readings, updating a small display and sending data over a low-power radio. A full preemptive kernel would consume memory and power the design cannot spare, so the engineer uses a cooperative scheduler in which each task performs a short step and yields.

The engineer sets a yield budget of 2 milliseconds. Measurements show that the radio task occasionally runs for 15 milliseconds while encoding a packet, which delays display updates. The engineer rewrites the encoder as a state machine that processes one block per call and yields between blocks, and adds a watchdog timer with a 1-second timeout.

After the change, every task runs at least every few milliseconds, the display no longer stutters and the device has run for months without a lockup. The result shows the model at its best: minimal overhead and predictable behavior, provided that every task respects its budget.