Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between cooperative multitasking and preemptive multitasking?

Recently I was understanding for how threads are different from fibers. This answer says that

Threads use pre-emptive scheduling, whereas fibers use cooperative scheduling.

In order to get more information about cooperative multitasking vs preemptive multitasking there no specific post on SO. Hope that this question will be helpful to get all the information about the topic.

like image 937
Mayur Avatar asked Apr 16 '19 08:04

Mayur


People also ask

What is preemptive multitasking and how does it work?

In preemptive multitasking, each task is given a specific amount of time and each task is then interrupted by the CPU and CPU give time to other high priority tasks. In this type of multitasking, the tasks don’t decide by themselves that how much time they can execute but the processor has to decide context switching.

What is cooperative multitasking in operating systems?

Old operating systems use cooperative multitasking. Cooperative multitasking was used in Mac OS version 8.0-9.2.2 and Windows 3.x In preemptive multitasking, each task is given a specific amount of time and each task is then interrupted by the CPU and CPU give time to other high priority tasks.

What is rtkernel-32 preemptive or cooperative multitasking?

Preemptive or Cooperative Multitasking? RTKernel-32 allows you to choose between preemptive and cooperative multitasking. The proper choice depends on the requirements of the application.

What is the difference between cooperative scheduling and preemptive scheduling?

With cooperative scheduling and the single-processor kernel, substantially fewer reentrance problems are encountered than in preemptive scheduling, because tasks cannot be interrupted arbitrarily by other tasks, but only at positions permitted by the programmer (i.e., in kernel calls). This advantages does not apply to the multiprocessor kernel.


2 Answers

Short answer:

Preemptive: threads do not decide when to run and are forced to share the CPU

Cooperative: each thread, once running decides for how long to keep the CPU, and (crucially) when it is time to give it up so that another thread can use it.

Long Answer

Preemptive

It means that threads are not in control on when and/or for how long they are going to use the CPU and run. It is the scheduler (a component of the OS) that decides at any moment which thread can run and which has to sleep. You have no strong guarantees on what will be the next time a thread will run, and for how long. It is completely up to the scheduler.

Cooperative

In cooperative multitasking, what happens is that the scheduler has no say in when a thread can run. Each thread decides for how long it keeps the CPU. If it decided not to share the CPU with any other thread, then no other threads will run causing what is known as starvation.

Note that stopping one thread and starting another incurs in a certain amount of overhead. It means that you spend time and resources not to execute code from your tasks, but purely for the sake of enabling sharing the CPU. In certain real-time low latency application (like high frequency trading), this can be quite unacceptable.

like image 58
Davide Spataro Avatar answered Oct 11 '22 10:10

Davide Spataro


Cooperative multitasking is great for embedded systems. As you normally would create a event handler in main.c and only let IRQ pass flags over to this, instead of actually running this code in the ISR. So if you expand the event handler to also allow RTC counter to let each task sleep and tell it to come back in 1ms or 60sec, great for battery life, as you sleep as often as possible even if just for 5ms.

You can't NOT have hard-waits, so your tasks have to be like state-machines, tell it to come back right away or in 5ms intervals etc.

I wrote my own OS in under 1K that does this.

like image 34
Tony Philipsson Avatar answered Oct 11 '22 11:10

Tony Philipsson