I'm new to semaphores, so I am not sure if there are different definitions for signal() and wait(). In my textbook, signal() is defined as:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
// remove a process P from S->list;
wakeup(P);
}
}
and more simply as:
signal(S) {
S++;
}
and wait() is defined as:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
// add this process to S->list;
block();
}
}
and more simply as:
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Say you have the following:
Process 90: wait(s1);
Process 91: wait(s1);
Process 95: wait(s1);
This produces the current values of the semaphore as:
semaphore s1 {
value = -3;
*list = { 90, 91, 95}; // processes will be awakened in order from left to right
}
I understand that wait() decrements value and signal() increments value.
I believe that if the following sequence is executed (possibly on different cores), this would be what happens.
Process 101: signal( s1); --> value = -2
Process 81: signal( s1); --> value = -1
Process 100: wait( s1); --> list = {90, 91, 95, 100}
Process 83: signal( s1); --> value = -1
Process 85: signal( s1); --> value = 0
Process 103: signal( s1); --> value = 1, list = {91, 95, 100}
Process 95: signal( s1); --> value = 2, list = {100}
- Since the processes are awakened from the queue from left to right(FIFO), would process 91 also be awakened from the queue when process 95 executes signal()? if so, is Process 91 just unblocked and can now be executed at the scheduler's discretion? Or does it affect the value?
- in the definition of signal(), it says if value is <=0, awaken a process. Does this mean when Process 101 is executed, that the list would become list = {101, 105}?