What is the difference between cooperative multitasking and preemptive multitasking?
Asked Answered
S

2

20

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.

Skirret answered 16/4, 2019 at 8:5 Comment(4)
More or less the same difference you have between forceful community service and volunteering. The first forces threads to share something, the second leave the choice of sharing to a single thread that, if wants can keep the resource for itself forever.Marleen
So, Is it recommended to use cooperative multitasking real-time applications?Skirret
No, it has terrible I/O performance.Logarithmic
I think the real situation is somehow in the middle, there is no one or the other. The real multitask systems will let your code to coop with each others, if your program works correctly, but if your code tries to get all CPU resources, the multitask system will preemptive grab and put the code to sleep and will schedule for later execution.Persia
M
40

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.

Marleen answered 16/4, 2019 at 8:15 Comment(1)
One of the best answer on the topic.Barbe
G
2

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.

Gustafsson answered 21/4, 2020 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.