{{Short description|Computer multitasking that does not preempt processes}} {{Use mdy dates|date=August 2015}}

'''Cooperative multitasking''', also known as '''non-preemptive multitasking''', is a [[computer multitasking]] technique in which the [[operating system]] never initiates a [[context switch]] from a running [[Process (computing)|process]] to another process. Instead, in order to run multiple applications concurrently, processes voluntarily [[Yield (multithreading)|yield control]] periodically or when idle or logically [[Blocking_(computing)|blocked]]. This type of multitasking is called ''cooperative'' because all programs must cooperate for the scheduling scheme to work.

In this scheme, the [[process scheduler]] of an operating system is known as a '''cooperative scheduler''' whose role is limited to starting the processes and letting them return control back to it voluntarily.<ref name="pcmag">{{cite web | url = https://www.pcmag.com/encyclopedia/term/48051/non-preemptive-multitasking | title = Definition of non-preemptive multitasking | access-date = August 15, 2015 | website = pcmag.com }}</ref><ref name="classiccmp">{{cite web | url = http://www.classiccmp.org/cini/pdf/HT68K/HT68K%20TCJ30p37.pdf | title = Non-Preemptive Multitasking | date = November 5, 2011 | access-date = August 15, 2015 | author = Joe Bartel | website = classiccmp.org | archive-date = August 19, 2019 | archive-url = https://web.archive.org/web/20190819072905/http://www.classiccmp.org/cini/pdf/HT68K/HT68K%20TCJ30p37.pdf | url-status = dead }}</ref>

This is related to the [[asynchronous programming]] approach.

==Usage== Although it is rarely used as the primary scheduling mechanism in modern operating systems, it is widely used in memory-constrained [[embedded system]]s and in specific applications such as [[CICS]] or the [[JES2]] subsystem. Cooperative multitasking was the primary scheduling scheme for 16-bit applications employed by [[Microsoft Windows]] before [[Windows&nbsp;95]] and [[Windows&nbsp;NT]], and by the [[classic Mac&nbsp;OS]]. [[Windows&nbsp;9x]] used non-[[preemptive multitasking]] for 16-bit legacy applications, and the [[PowerPC]] Versions of Mac OS&nbsp;X prior to [[Mac OS X v10.5|Leopard]] used it for [[Classic (Mac OS X)|classic]] applications.<ref name="pcmag" /> [[NetWare]], which is a network-oriented operating system, used cooperative multitasking up to NetWare&nbsp;6.5. Cooperative multitasking is still used on [[RISC&nbsp;OS]] systems.<ref>{{cite web | url = http://www.riscos.info/index.php/Preemptive_multitasking | title = Preemptive multitasking | date = November 2, 2009 | access-date = August 15, 2015 | website = riscos.info }}</ref>

Cooperative multitasking is similar to [[async/await]] in languages, such as [[JavaScript]] or [[Python (programming language)|Python]], 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 of [[coroutine]].<ref>{{cite web |last1=Kerkour |first1=Sylvain |title=Async Rust: Cooperative vs Preemptive scheduling |date=January 25, 2022 |url=https://kerkour.com/cooperative-vs-preemptive-scheduling |access-date=30 January 2023 }}</ref><ref>{{cite web |last1=Skvortsov |first1=Victor |title=Python behind the scenes #12: How async/await works in Python |url=https://tenthousandmeters.com/blog/python-behind-the-scenes-12-how-asyncawait-works-in-python/ |access-date=30 January 2023 }}</ref>

Cooperative multitasking allows much simpler implementation of applications because their execution is never unexpectedly interrupted by the process scheduler; for example, various [[Subroutine|functions]] inside the application do not need to be [[Reentrancy (computing)|reentrant]].<ref name="classiccmp" />

==Problems== 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 by [[busy wait]]ing; both would cause the whole system to [[hang (computing)|hang]]. In a [[Server (computing)|server]] environment, this is a hazard that is often considered to make the entire environment unacceptably fragile,<ref name="pcmag" /> though as noted above, cooperative multitasking has been used frequently in server environments, including NetWare and CICS.

In contrast, [[Preemption (computing)|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 a [[watchdog timer]], often implemented in hardware; this typically invokes a [[hardware reset]].

== See also == * [[Consensus dynamics]]

== References == {{Reflist}}

{{Operating System}}

{{DEFAULTSORT:Nonpreemptive Multitasking}} [[Category:Concurrent computing]] [[de:Multitasking#Kooperatives Multitasking]]