For some (embedded) applications you may want to control the scheduling of processes.
In this example, we show how a program with 4 processes may be scheduled using a mix of cooperative and preemptive scheduling.
The example has 2 processes, F and keyboard that must be scheduled regularly and decide themselves when to suspend. The Keyboard is described on the next page.
When F and keyboard are not running, two other processes P1 and P2 may run, but these are both preempted after some time.
MixedScheduling: obj LIB.BasicSystem.BasicSystem
F: obj BasicProcess("F")
cycle
-- do something
if (done) :then
F.suspend
P1: obj BasicProcess("P1")
cycle
-- do something until preempted
P2 : obj BasicProcess("P2")
cycle
-- do something until preempted
cycle
keyboard.resume
F.resume
P1.attach(10)
P2.attach(10)
The object MixedScheduling, constantly executes a loop (using cycle):
- The objects keyboard and F are resumed.
- Then P1 and P2 are attached and preempted after 10 time units.
Supposed that keyboard and F together use 5 time units, then keyboard and F are guaranteed to be executed every 25 time units.
The objects F, P1 and P2 may read characters from the keyboard, and since keyboard is non-blocking they will be suspended if no input is available.