Using cooperative processes, a simple kind of operating system can be
made. In cooperative processes, each process "yields" its own time
by co-call other process. If that call is made to a special process,
let us call it "yield" process, and that "yield" co-call other process,
then "yield" process becomes a kind of "task switcher". Notice that
calling "yield" is voluntary, unlike in a preemptive OS that "timeout" is
caused by interrupt and involuntary.
Let us revisit the previous example of cooperative processes A and B.
A()
print(1)
@B()
print(2)
@B()
print(3)
@B()
B()
print("a")
@A()
print("b")
@A()
print("c")
when @A(), @B()
are co-calls. This act will output "1 a 2 b
3 c". Now with our "yield" process, it will look like:
A()
print(1)
yield()
print(2)
yield()
...
B()
print("a")
yield()
print("b")
yield()
...
yield()
...
So, it looks very much like a multitask manager. Whenever a task
wants to let other task runs, it "yields" by itself. This is a lot better
than co-calling other process directly because all a task needs to do is
to "yield" at its convenient time.
To "switch" between processes, the state of the process has to be
save/restore just like Process Control Block in the previous
chapter. In order for "yield" to co-call other process, the
continuation points must be manipulate and the list of the active
processes must be maintained, not unlike the process queue. So, let us use
the process queue to keep track of the active processes and use PCB to
keep states of the process. Now, we can sketch how "yield" work.
yield()
save the current process
state
continuation (PC)
get a next process in the process queue
restore
state
continuation
co-call that process
It looks very much like "task switch" of the preemptive operating
system. The main difference, again, is that "yield" is voluntary,
but "tswitch" occurs by interrupt signal (involuntary).
Remember the weakness of cooperative processes is that if one process
crashes, all processes will crash. Because the one that crashes
supposed to call "yield" but it didn't. In preemptive operating system,
there always is another process running and the interrupt signal always
occurs no matter what one process does. So, there is a way to recover from
a crash.
The advantage of cooperative operating system is that it is very simple
and does not require a lot of resources. Hence, it is suitable to
build on a resource constrained system.
last update 19 Feb 2017