Reputation: 97
Can anyone explain the difference between these two mechanisms, theoretical and implementation perspective in kernel. Can wait queues be used for thread synchronization, if not, why?
Upvotes: 1
Views: 4269
Reputation: 1094
Wait queues are event based mechanism and you wait for particular condition to be true.
They are not locks.
Semaphores are locks. You don't wait on certain condition to be true.
Take lock
Process data
Release lock.
Upvotes: 3
Reputation: 1287
Wait queue is already there in the Semaphore structure. Ok , let's understand it in a little detail.
Semaphore structure is as :
typedef struct
{
int value;
process *wait_queue;
}
Where "value" is the value of the semaphore variable at a time and the "wait_queue" is the list of processes waiting for a resource.Semaphore are of two types :
Counting semaphore can range in unrestricted domain i.e Semaphore.value can range from negative to positive integer. But Binary Semaphore can have values only 0 and 1 . Now we define two operations wait() and signal() as :
wait(Semaphore *S)
{
S->value--;
if(S->value < 0)
{
Block the process;
Add it to S->wait_queue;
}
}
signal(Semaphore *S)
{
S->value++;
process *temp = Take a process from "S->wait_queue";
Change the state of "temp" from block to ready state;
}
So , whenever a process will try to acquire a resource , first of all it will execute wait() operation on the semaphore associated with that resource. If that resource is free (or any of the instances of that resource is free) , then resource will be allocated to that process , otherwise process will be blocked and put in the wait_queue. If a process leaves a resource then it will execute signal() operation and status of one process in the wait_queue will be changed from waiting to ready state.
Upvotes: 4