Pre-emptive vs. Cooperative Multitasking

Jecel Mattos de Assumpcao Jr. jecel@lsi.usp.br
Mon, 17 Jul 1995 17:43:53 -0300


The discussion reproduced here only examines one aspect of the difference
between the two types of multitasking. Don't forget that:

  - Cooperative Multitasking: Mostly avoids the need to explicitly
    control resource sharing. Allows you to violate "invariants"
    to boost performance as long as things are restored to normal
    before the next task switching point.

  - Pre-emptive Multitasking: Doesn't lose control to buggy
    applications.

Some intersting hybrid schemes have been built. The Transputer is
an example. Tasks are divided into low-priority ones and high
priority tasks. Within each priority level, the tasks are
cooperatively multitasked, yielding the processor to the others
on a message send. If a high-priority task wakes up when the CPU
is executing at a low-priority level, the state is saved in special
"registers" and a task switch occurs. When no more high-priority tasks
are ready to execute, the state is restored and the low-priority task
continues. If a low-priority task executes for too long, the time-slice
timer will cause a task switch at the next branch instruction. Note
that all registers are defined as invalid after such an instruction,
so there is very little state to save ( unlike the previous case ).
So this scheme works mostly like cooperative multitasking, except
when it doesn't :-)

I think I wasn't very clear in this explanation. I can go into more
details, if there is interest. Merlin was changed from a cooperative
multitasking OS to a pre-emptive one in order to allow the user to
write arbitrary "primitive" in assembly language without the scheduler
losing control. This had a very high cost in terms of saving state,
however, but I hope to do some optimizations that will mostly eliminate
the extra overhead.

-- Jecel