This is a multipart blog article series, and in this series I am going to explain you the concepts of operating system. This article series is divided into multiple modules and this is the third module which consists of 10 articles.
In this we will see a question on binary semaphore and try to understand the concept of binary semaphore.
Question: What is the maximum number of processes that may present in critical section at any point of time?
Each process from P1-P9 will execute this code
repeat
p(mutex)
critical section
v(mutex)
forever
Process P10 will execute this code
repeat
v(mutex)
critical section
v(mutex)
forever
The initial value of mutex = 1
All processes P1-P10 are cooperative process
Answer:
- Here, when
P1
try to enter the critical section then it will run the first code. After executing the code the it get successfully and enter critical section. Now as the value of mutex is0
no other process can enter the critical section if they try to enter they will be sent to suspended list and get blocked. - This is happening because, to enter the critical section, they have to execute the
P()
operation which reduce the value of mutex from1
to0
, but the value of mutex is already0
asP1
has earlier executed it. - So process from
P2- P9
cannot enter the critical section, whileP1
is there. - Now, we will try to send
P10
in the critical section, for thatP10
will execute second code, and in thatv()
operation will make value of mutex from0
to1
and thus successfully get inside the critical section. Now, we haveP1
andP10
in the critical section with the value of mutex1
. - And again
P2
will try to enter the critical section, but because now the value of mutex is1
it will successfully get inside of critical section. - Now we have
P1
,P10
andP2
inside the critical section. And all the other processes fromP3-P9
cannot enter the critical section. - BUT THIS IS WRONG ANSWER.
- We can do one thing, if
P10
exit the critical section then the value of mutex will become1
andP3
can enter the critical section, now mutex value is0
, we will execute the second code and again theP10
will enter the critical section. Thus now we have four processes inside the critical section (P1, P2, P3 and P10). - This will keep happening and at the end we will have
10
process inside the critical section, and all of this is happening becauseP10
code hasv()
operation at the entry section. And we know thatv()
operation increase the value from0
to1
and if it is1
then it remains1
.
Therefore, the final answer is 10
processes (P1, P2, P3, P4, P5, P6, P7, P8, P9, and P10)
So this was all about question on binary semaphore. Hope you liked it and learned something new from it.
If you have any doubt, question, quires related to this topic or just want to share something with me, then please feel free t o contact me.
📱 Contact Me
Twitter
LinkedIn
Telegram
Instagram
Top comments (0)