Understanding Linux Process Sleep and Wake‑Up: Avoiding Invalid Wake‑Ups
This article explains how Linux processes transition between running, sleeping, and waking states, describes interruptible and uninterruptible sleep, demonstrates code for putting a process to sleep and waking it, and provides strategies to prevent invalid wake‑up scenarios caused by race conditions.
1 Linux Process Sleep and Wake‑Up
In Linux, a process that is only waiting for CPU time is called a runnable process and is placed on the run‑queue with the state flag TASK_RUNNING. When its time slice expires, the scheduler removes it from the CPU and selects another runnable process.
A process can also voluntarily give up the CPU by calling the scheduling function schedule(). After being rescheduled, it resumes execution at the line following the schedule() call.
Sometimes a process must wait for a specific event (device initialization, I/O completion, timer, etc.). In this case it is removed from the run‑queue and placed on a wait‑queue, entering a sleep state.
Linux defines two sleep states:
TASK_INTERRUPTIBLE : the process sleeps until a condition becomes true (e.g., an interrupt, a released resource, or a signal).
TASK_UNINTERRUPTIBLE : similar to interruptible sleep but the process does not respond to signals.
Typical code to put a running process to sleep:
sleeping_task = current;
set_current_state(TASK_INTERRUPTIBLE);
schedule();
func1();
/* Rest of the code ... */After the above code runs, sleeping_task points to the current process structure. set_current_state() changes its state from TASK_RUNNING to TASK_INTERRUPTIBLE. If schedule() is invoked by a running process, it will select another process; if invoked by a sleeping process, the current process is removed from the run‑queue and goes to sleep.
To wake a sleeping process:
wake_up_process(sleeping_task);After wake_up_process(), the process state becomes TASK_RUNNING and the scheduler will place it back on the run‑queue.
2 Invalid Wake‑Ups
Invalid wake‑ups occur when a process checks a condition, finds it false, and then goes to sleep, but another process wakes it up after the condition has already become true. This race can cause the sleeping process to remain asleep indefinitely.
Example with two processes A and B accessing a shared list:
// Process A
spin_lock(&list_lock);
if (list_empty(&list_head)) {
spin_unlock(&list_lock);
set_current_state(TASK_INTERRUPTIBLE);
schedule();
spin_lock(&list_lock);
}
/* Rest of the code ... */ // Process B
spin_lock(&list_lock);
list_add_tail(&list_head, new_node);
spin_unlock(&list_lock);
wake_up_process(processa_task);If B wakes A before A has set its state to TASK_INTERRUPTIBLE, the wake‑up is ineffective and A may sleep forever.
3 Avoiding Invalid Wake‑Ups
The solution is to make the check‑and‑sleep sequence atomic. By setting the process state to TASK_INTERRUPTIBLE *before* testing the condition, any wake‑up that occurs after the condition becomes true will correctly transition the process back to TASK_RUNNING before it calls schedule().
set_current_state(TASK_INTERRUPTIBLE);
spin_lock(&list_lock);
if (list_empty(&list_head)) {
spin_unlock(&list_lock);
schedule();
// Process will be woken only if the condition is still false
} else {
spin_unlock(&list_lock);
set_current_state(TASK_RUNNING);
}4 Linux Kernel Example
A real kernel example (from Linux 2.6.11, sched.c) shows the same pattern:
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop()) {
schedule();
set_current_state(TASK_INTERRUPTIBLE);
}
__set_current_state(TASK_RUNNING);
return 0;The thread sets its state to TASK_INTERRUPTIBLE before checking kthread_should_stop(). Any wake‑up that occurs after the condition becomes true will not be lost, preventing invalid wake‑ups.
Conclusion
To avoid invalid wake‑ups in Linux, set the process state to a sleeping state *before* evaluating the wait condition, and reset it to TASK_RUNNING if the condition is already satisfied. This ensures the process never erroneously remains asleep.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
