Sobes.tech
Senior

What is the difference between cooperative and preemptive multitasking?

sobes.tech AI

Answer from AI

Cooperative multitasking is a model where tasks (processes or threads) voluntarily yield control, allowing other tasks to run. The scheduler does not forcibly interrupt a task; instead, the task must explicitly invoke a switch.

Preemptive multitasking is a model where the operating system scheduler forcibly interrupts a task at regular intervals (time quanta) and switches control to another task.

Difference:

  • In cooperative multitasking, tasks should be "friendly" and regularly yield control; otherwise, one task can block the system.
  • In preemptive multitasking, the scheduler controls the switching, which ensures a more even distribution of resources and prevents hangs.

In Go, the runtime uses a cooperative model for goroutines: goroutines voluntarily yield control during blocking operations or when calling special functions, enabling efficient switching without the overhead of interrupts.