Reputation: 3949
Why do we use wait queues in the linux kernel rather than use semaphores for synchronization ? What is the difference between using wait queues vs semaphores for synchronization ?
Upvotes: 3
Views: 3865
Reputation: 46728
A semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment. (Wikipedia)
Now, a semaphore is more of a concept, rather than a specific implementation.
The linux semaphore data structure implementation uses a wait-queue. Without a wait queue, you wouldn't know which process demanded the resource first, which could lead to very large wait times for some. The wait-queue ensures fairness, and abates the resource starvation problem.
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
};
Upvotes: 4