Linux – Wait queues and semaphores in Linux

Wait queues and semaphores in Linux… here is a solution to the problem.

Wait queues and semaphores in Linux

Why do we use wait queues in the linux kernel instead of semaphores for synchronization? What is the difference between synchronizing with semaphores using wait queues?

Solution

A semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access to public resources by multiple processes in a parallel programming environment. ( Wikipedia )

Now, semaphores are more of a concept than a concrete implementation.

The Linux semaphore data structure implementation uses a wait queue. Without a wait queue, you won’t know which process needs resources first, which can result in some processes taking very long wait times. Wait queues ensure fairness and mitigate resource scarcity.

struct semaphore {
    int count; +ve or -ve indicates resource free/busy state
    int waking; number of waiting processes
    int lock ;  /* to make waking testing atomic */
    struct wait_queue *wait; queued, to prevent starvation, ensure fairness
};

Reference

Related Problems and Solutions