Waiting for Something (Slideshow)

Waiting for Something

  • Processes wait for something to happen

    • Network arrived

    • Switch pressed

    • Some event happened

  • Polling: busy looping, constantly checking the condition

    • ⟶ power inefficient

    • ⟶ stupid

  • Sleeping

    • If that something hasn’t yet happened, a process is put to sleep

    • A process that make the event happen, is supposed to wake any waiters

Wait Queues: Initializing

#include <linux/wait.h>

struct wait_queue_head_t wq;
init_waitqueue_head(&wq);

(No destructor)

Wait Queue: Waiting

#include <linux/wait.h>
#include <linux/sched.h>

int wait_event(wq, something != 1);
int wait_event_interruptible(wq, something != 1);
int wait_event_timeout(wq, something != 1, timeout_jiffies);
int wait_event_interruptible_timeout(wq, something != 1, timeout_jiffies);

Note no pointers ⟶ obscure programming style in the early days of Linux

Wait Queue: Waking

#include <linux/wait.h>
#include <linux/sched.h>

wake_up(&wq);
wake_up_interruptible(&wq);
  • wake_up_interruptible() wakes only processes in state TASK_INTERRUPTIBLE

  • wake_up() wakes all processes

Documentation

wait_event

wait_event_interruptible

wait_event_timeout

wait_event_interruptible_timeout

wake_up

wake_up_interruptible hm?