top logo menu kitty mascot
AROS.ORG Forum Software Bounties

AROS Application Development Manual -- The Multitasking Kernel

Index

Avvertenza

This document is not finished! It is highly likely that many parts are out-of-date, contain incorrect information, or are simply missing altogether. If you want to help rectify this, please contact us.

Indice

Background

There are two kinds of multitasking: Preemptive and Cooperative (sometimes indicated as: Non-preemptive). In Preemptive multitasking, a task will get a certain amount of CPU time and then another task gets the CPU. In Cooperative multitasking, a task has to release the CPU, which will then allow another task to run.

Preemptive multitasking (PMT) has these advantages:

and these disadvantages:

Cooperative Multitasking (CMT) has these advantages:

and these disadvantages:

Though it would seem, from the counts of advantages and disadvantages, that CMT is better than PMT, that's not exactly true. CMT is better when you have only a few tasks and a single user. Powerful OSes, like Unix and AmigaOS, have PMT because it's simpler to use. The same holds for AROS.

How does preemptive multitasking work?

The idea is pretty simple: Every computer has a clock. This clock can generate interrupts. An interrupt means that the CPU saves its current state on the stack of the current task and starts a special routine called the interrupt handler. Basically this handler now checks for other tasks which might want to run, selects one of them and switches the stack to the one of the newly chosen task. It then finishes and tells the CPU to continue where it was before the interrupt. Since the CPU knows nothing about all this, it will just load its state from the new stack (i.e. the one of the new task) and so the new task will run, while the old one will be sleeping.

Looking at it from the perspective of the life of a task: A tasks's life consists of birth, waiting, running, and death. In computer terms, the task is created, then waits until it gets the CPU, does its work while it has the CPU (these two states may switch back and forth several times), and at some point it terminates (or is terminated).