Chapter 9 Sleep and Wakeup
Scheduling and locks help conceal the actions of one thread
from another,
but we also need abstractions that help
threads intentionally interact.
For example, the reader of a pipe in xv6 may need to wait
for a writing process to produce data;
a parent’s call to wait may need to
wait for a child to exit; and
a process reading the disk needs to wait
for the disk hardware to finish the read.
The xv6 kernel uses a mechanism called sleep and wakeup
in these situations (and many others).
Sleep allows a kernel thread to
wait for some condition to be true; another thread
or an interrupt handler can cause the condition
to be true (typically by modifying some variable(s))
and then call wakeup
to indicate that threads waiting for the condition should resume.
Sleep and wakeup are often called
sequence coordination
or
conditional synchronization
mechanisms.
Before proceeding, please read the functions sleep() and wakeup() in kernel/proc.c, and all of file kernel/pipe.c.