Sleeping Processes
Sleeping processes are placed inside of a wait queue.
The processes inside the wait queue are waiting for an event to happen.
Processes put themselves in the wait queue and mark themselves as not runnable.
A process performs the following steps to add itself to a wait queue:
- Creates a wait queue entry via the macro
DEFINE_WAIT()
. - Adds itself to a wait queue via
add_wait_queue()
.This wait queue awakens the process when the condition for which it is waiting occurs. - Calls
prepare_to_wait()
to change the process state to eitherTASK_INTERRUPTIBLE
orTASK_UNINTERRUPTIBLE
. This function also adds the task back to the wait queue if necessary - If the state is set to
TASK_INTERRUPTIBLE
, a signal wakes the process up. This is called a spurious wake up (a wake-up not caused by the occurrence of the event). So check and handle signals. - When the task awakens, it again checks whether the condition is true. If it is, it exits the loop. Otherwise, it again calls
schedule()
and repeats. - Now that the condition is true, the task sets itself to
TASK_RUNNING
and removes itself from the wait queue viafinish_wait()
.
An important note about sleeping is that there are spurious wake-ups. Just because a task is awakened does not mean that the event for which the task is waiting has occurred;