in Operating System
475 views
0 votes
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 ?
in Operating System
475 views

1 comment

 if P3 wants to enter in critical section then it will call wait();

Previously,P3 wants to enter into the critical section, then it will call wait() ====> Accused ===> BLOCK

after wakeup,it will directly enter into CS, but not again call wait()...

 

if you didn't understand it...

wait() ---- first P3 blocks, after sometime wake up then go to next statement

CS

signal() --- Wakeup some other Process or increment the variable only

1
1

Please log in or register to answer this question.

Related questions