Cooperative multitasking, also known asnon-preemptive multitasking, is acomputer multitasking technique in which theoperating system never initiates acontext switch from a runningprocess to another process. Instead, in order to run multiple applications concurrently, processes voluntarilyyield control periodically or when idle or logicallyblocked. This type of multitasking is calledcooperative because all programs must cooperate for the scheduling scheme to work.
In this scheme, theprocess scheduler of an operating system is known as acooperative scheduler whose role is limited to starting the processes and letting them return control back to it voluntarily.[1][2]
This is related to theasynchronous programming approach.
Although it is rarely used as the primary scheduling mechanism in modern operating systems, it is widely used in memory-constrainedembedded systems and in specific applications such asCICS or theJES2 subsystem. Cooperative multitasking was the primary scheduling scheme for 16-bit applications employed byMicrosoft Windows beforeWindows 95 andWindows NT, and by theclassic Mac OS.Windows 9x used non-preemptive multitasking for 16-bit legacy applications, and thePowerPC Versions of Mac OS X prior toLeopard used it forclassic applications.[1]NetWare, which is a network-oriented operating system, used cooperative multitasking up to NetWare 6.5. Cooperative multitasking is still used onRISC OS systems.[3]
Cooperative multitasking is similar toasync/await in languages, such asJavaScript orPython, that feature a single-threaded event-loop in their runtime. This contrasts with cooperative multitasking in that await cannot be invoked from a non-async function, but only an async function, which is a kind ofcoroutine.[4][5]
Cooperative multitasking allows much simpler implementation of applications because their execution is never unexpectedly interrupted by the process scheduler; for example, variousfunctions inside the application do not need to bereentrant.[2]
As a cooperatively multitasked system relies on each process regularly giving up time to other processes on the system, one poorly designed program can consume all of the CPU time for itself, either by performing extensive calculations or bybusy waiting; both would cause the whole system tohang. In aserver environment, this is a hazard that is often considered to make the entire environment unacceptably fragile,[1] though as noted above, cooperative multitasking has been used frequently in server environments, including NetWare and CICS.
In contrast,preemptive multitasking interrupts applications and gives control to other processes outside the application's control.
The potential for system hang can be alleviated by using awatchdog timer, often implemented in hardware; this typically invokes ahardware reset.