Sobes.tech
Junior — Middle

How does the implementation of multitasking based on the cooperative approach differ from the implementation with a preemptive scheduler?

sobes.tech AI

Answer from AI

In cooperative multitasking, tasks decide themselves when to yield control — they explicitly invoke a context switch. This means that if a task does not yield control, other tasks cannot run.

In preemptive multitasking, the operating system or runtime scheduler automatically interrupts tasks at regular intervals (time slices) and switches context to another task, ensuring a fairer distribution of resources.

An example in Go: goroutines are scheduled by a preemptive scheduler, which automatically switches execution between them without requiring explicit yielding.