Course contentsShow
← Linux
Lesson 516 of 1,8195. Process Management and SignalingFree lesson

Wait-Queues and Event Notification

Explain the inner mechanics of how processes sleep while waiting for kernel events.

In this lesson, you will learn how the Linux kernel puts processes to sleep and efficiently wakes them up when the specific data or resource they are waiting for becomes available.

The Problem with Polling

Imagine you are waiting for a package delivery. Instead of staring at your front door every second (a technique called "polling," which wastes CPU cycles), you go about your business and let the courier notify you when the package arrives. Linux manages this efficiency using wait-queues.

How Wait-Queues Work

A wait-queue is a kernel data structure—essentially a linked list—that holds references to process descriptors (task_struct) that are currently waiting for a specific event.

When a process needs a resource that isn't ready (like waiting for a disk read or a network packet), the kernel performs three distinct steps:

  1. Queueing: The process adds itself to the wait-queue associated with that event.
  2. State Change: The kernel sets the process state to TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE. This tells the scheduler: "Ignore this process; do not grant it any CPU time."
  3. Rescheduling: The kernel calls the scheduler to pick a different process to run, effectively putting the current process to sleep.

The Notification Mechanism

The "event notification" happens when the kernel event occurs (e.g., the data arrives). The code responsible for that resource then iterates through the wait-queue and invokes a "wake up" function. This function:

  • Changes the state of the processes in the queue back to TASK_RUNNING.
  • Moves the process back into the scheduler’s "ready" list.
  • Removes them from the wait-queue.

Here is a conceptual look at how a process places itself into a wait-queue:

// Pseudo-code representation of the waiting logic
prepare_to_wait(&my_wait_queue, &my_waiter, TASK_INTERRUPTIBLE);

if (resource_not_ready) {
    schedule(); // CPU switches to another process
}

finish_wait(&my_wait_queue, &my_waiter);

By using wait-queues, the kernel ensures that processes consume zero CPU time while idle, only waking up exactly when they have actual work to perform, which prevents the CPU-wasting overhead of busy-looping.

Key Takeaway: Wait-queues allow the kernel to pause process execution and move it off the CPU, ensuring resources are used only when an explicit event signals that the process can resume meaningful work.