Cooperative Scheduling vs Preemptive Scheduling? Cooperative Scheduling vs Preemptive Scheduling? multithreading multithreading

Cooperative Scheduling vs Preemptive Scheduling?


Preemptive scheduling has to solve a hard problem -- getting all kinds of software from all kinds of places to efficiently share a CPU.

Cooperative scheduling solves a much simpler problem -- allowing CPU sharing among programs that are designed to work together.

So cooperative scheduling is cheaper and easier when you can get away with it. The key thing about small devices that allows cooperative scheduling to work is that all the software comes from one vendor and all the programs can be designed to work together.


The big benefit in cooperative scheduling over preemptive is that cooperative scheduling does not use "context switching". Context switching involves storing and restoring the state of an application (or thread). This is costly.

The reason why smaller devices are able to get away with cooperative scheduling for now has to do with the fact that there is only one user on a small device. The problem with cooperative scheduling is that one application can hog up the CPU. In preemptive scheduling every application will eventually be given an opportunity to use the CPU for a few cycles. For bigger systems, where multiple demons or users are involved, cooperative scheduling may cause issues.

Reducing context switching is kind of a big thing in modern programming. You see it in Node.js, Nginx, epoll, ReactiveX and many other places.


First you have to find the Meaning of the word Preemption

Preemption is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time. Such changes of the executed task are known as context switches.(https://en.wikipedia.org/wiki/Preemption_(computing))

Therefore, the difference is

  • In a preemptive model, the operating system's thread scheduler isallowed to step in and hand control from one thread to another at anytime(tasks can be forcibly suspended).

  • In cooperative model, once a thread is given control it continues torun until it explicitly yields control(handover control of CPU to the next task) or until it blocks.

Both models have their advantages and disadvantages. Preemptive scheduling works better when CPU have to run all kinds of software which are not related to each other. And cooperative scheduling works better when running programs that are designed to work together.

Examples for cooperative scheduling threads:

  1. Windows fibers (https://docs.microsoft.com/en-gb/windows/win32/procthread/fibers?redirectedfrom=MSDN)
  2. Sony’s PlayStation 4 SDK (http://twvideo01.ubm-us.net/o1/vault/gdc2015/presentations/Gyrling_Christian_Parallelizing_The_Naughty.pdf)

If you want to learn underline implementations of these cooperative scheduling fibers refer this book (https://www.gameenginebook.com/)

Your book states that "smaller devices such as cell phones", may be author is referring to cell phones from several years back. They had only few programs to run and all are provided by the phone manufacturer. So we can assume those programs are designed to work together.