Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cooperative Scheduling vs Preemptive Scheduling?

In the book Core Java : Volume 1 Fundamentals -> chapter MultiThreading .

The Author wrote as follows :

"All modern desktop and server operating systems use preemptive scheduling. However, smaller devices such as cell phones may use cooperative scheduling...."

I am aware of the definitions/workings of both types of scheduling , but want to understand reasons why cooperative scheduling is preferred over preemptive in smaller devices.

Can anyone explain the reasons why ?

like image 347
Roshan Avatar asked Sep 02 '17 16:09

Roshan


People also ask

What is cooperative scheduling?

Cooperative scheduling is a style of scheduling in which the OS never interrupts a running process to initiate a context switch from one process to another. Processes must voluntarily yield control periodically or when logically blocked on a resource.

What is the difference between a preemptive multitasking system and a cooperative system?

SR.NO. Preemptive multitasking is a task used by the OS to decide for how long a task should be executed before allowing another task to use the OS. Cooperative multitasking is a type of computer multitasking in which the operating system never initiates a context switch from a running process to another process.

What is the difference between cooperative threading model and preemptive threading model?

In cooperative models, once a thread is given control it continues to run until it explicitly yields control or it blocks. In a preemptive model, the virtual machine is allowed to step in and hand control from one thread to another at any time.

What is the difference between preemptive and Nonpreemptive scheduling?

Key Differences Between Preemptive and Non-Preemptive Scheduling: In preemptive scheduling, the CPU is allocated to the processes for a limited time whereas, in Non-preemptive scheduling, the CPU is allocated to the process till it terminates or switches to the waiting state.


4 Answers

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.

like image 179
Matt Timmermans Avatar answered Oct 09 '22 06:10

Matt Timmermans


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.

like image 20
Jose Martinez Avatar answered Oct 09 '22 07:10

Jose Martinez


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 is allowed to step in and hand control from one thread to another at any time(tasks can be forcibly suspended).

  • In cooperative model, once a thread is given control it continues to run 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.

like image 32
Nisal Dissanayake Avatar answered Oct 09 '22 06:10

Nisal Dissanayake


Cooperative scheduling has fewer synchronizaton problems.

Cooperative scheduling can have better performance in some, mostly contrived, scenarios.

Cooperative scheduling introduces constraints upon design and implementation of threads.

Cooperative scheduling is basically useless for most real purposes because of dire I/O performance, which is why almost nobody uses it.

Even small devices will prefer to use preemptive scheduling if they can possibly get away with it. Smartphones, streaming, (esp. video), and such apps that require good I/O are essentially not possible with cooperative systems.

What you are left with are trivial embedded toaster-controllers and the like.

like image 26
Martin James Avatar answered Oct 09 '22 07:10

Martin James