700 views
0 0 votes
Semaphore Implementation

typedef struct {
int value;
struct process *list;
} semaphore;

Each semaphore has an integer value and a list of processes list. When
a process must wait on a semaphore, it is added to the list of processes. A
signal() operation removes one process from the list of waiting processes
and awakens that process.
Now, the wait() semaphore operation can be defined as

wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
and the signal() semaphore operation can be defined as

signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}

suppose S=3, we need to be able to have 3 processes in Critical section at max. Also when counting semaphore S has negative value we need to have Absolute value of S as no of processes waiting for critical section. Suppose $P_0,P_1,P_3$ want to enter in critical section in order then they will call wait(); one after one and wait();is being called 3 times and S value will be 0 at this time all  3 will be there in critical section Now, if two more process $P_4,P_5$ will come and call the wait(); one afer one  then s value will be -2 and they will be block and added to list of waiting process now, suppose if $P_0$ comes out of critical section then it will call signal(); So S will be increment to -1 and it will see that S<=0 and it will call wakeup so, the process will be unblocked in same order($P_3,then P4$)

$\mathbf{MYDOUBT}$->now, if $P_3$ wants to enter in critical section then it will call wait(); and S value will be -2 but S-value<0 then how will P_3 will enter in critical section it will be again added to list of processs ,and if again $P_4$ comes out of critical section then again such thing will happen,

please tell me where i am making  mistake ?

Please log in or register to answer this question.

Position:
Show:

Related questions

0 0 votes
0 0 answers
1.1k
1.1k views
Arbaz__Malik asked Dec 25, 2021
1,133 views
given solution is wait(P) , wait(Q), wait(p) , wait(Q) for s1,s2,s3,s4 respectivelyI know this implementation is deadlock free just want to ask if it will follow bounded ...
0 0 votes
0 0 answers
523
523 views
legacy asked Nov 27, 2022
523 views
At the end of chapter 2, we studied a solution to the readers and writers problem, given below:-In this solution, as long as one reader is having the resource any new rea...
0 0 votes
1 answers 1 answer
1.5k
1.5k views
Shaijal Tripathi asked Nov 7, 2018
1,541 views
Assume Counting Semaphore N=8.On performing 10 down() operations by 10 different processes , first 8 will be successful and last two processes will be suspended.Does this...
3 3 votes
0 0 answers
2.0k
2.0k views
MiNiPanda asked Jan 13, 2018
2,046 views
wait(S);Critical sectionwait(S);S is a binary semaphore initialised to 1. Suppose there are n processes competing for the CS. Only one can enter into it. Is this the situ...