Cooperative Scheduling vs Preemptive Scheduling?
Asked Answered
C

6

22

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 ?

Collarbone answered 2/9, 2017 at 16:1 Comment(3)
Cell phones, especially smartphones, are very I/O intensive. They are probably the worst possible example of somewhere where cooperative scheduling is useful. Your book is umm.. poor.Woodford
@MartinJames , can you suggest a better one ... :) , other than Java docs...Collarbone
@Collarbone If you want to learn java itself only: Deitel - Java how to program.Calvities
E
9

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.

Echt answered 2/9, 2017 at 16:32 Comment(1)
Thanks for the answer !!! , so single user environment with reduced context switching and homogenous software condition favours cooperative scheduling... hope i am understanding it correctly.Collarbone
P
6

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 daemons 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.

Pliam answered 2/9, 2017 at 16:13 Comment(0)
L
5

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://learn.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.

Lactary answered 13/2, 2020 at 19:29 Comment(0)
W
2

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.

Woodford answered 2/9, 2017 at 18:1 Comment(1)
OK now i am confused.... , true android , uses preemptive time slicing , then , let me re-phrase the question , so the book got that part wrong ?Collarbone
P
0

Hard real-time control applications often demand that at least one thread/task not be preemptively interrupted while other threads are more forgiving. Additionally, the highest priority task may require that it be executed on a rigid schedule rather than being left to the mercy of a scheduler that will eventually provide a time-slot. For these applications, cooperative multitasking seems much closer to what is needed than preemptive multitasking but it still isn't an exact fit since some tasks may need immediate on-demand interrupt response while other tasks are less sensitive to the multi-tasking scheme.

Piquet answered 8/10, 2020 at 2:33 Comment(0)
U
0

Cooperative Scheduling A task will give up the CPU on a point called (Synchronization Point). It can use something like that in POSIX:

  • pthread.yield(Task_ID)

Preemptive Scheduling The main difference here is that in preemptive scheduling, the task may be forced to relinquish the CPU by the scheduler. For instance, two tasks with same priority, while one of them running, its time slice is ended.

Ung answered 14/2, 2021 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.