DEV Community

Cover image for Question on Binary Semaphore | Operating System - M03 P07
Rahul Mishra
Rahul Mishra

Posted on • Originally published at programmingport.hashnode.dev

Question on Binary Semaphore | Operating System - M03 P07

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
Enter fullscreen mode Exit fullscreen mode

Process P10 will execute this code

repeat
    v(mutex)
    critical section
    v(mutex)
forever
Enter fullscreen mode Exit fullscreen mode

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 is 0 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 from 1 to 0, but the value of mutex is already 0 as P1 has earlier executed it.
  • So process from P2- P9 cannot enter the critical section, while P1 is there.
  • Now, we will try to send P10 in the critical section, for that P10 will execute second code, and in that v() operation will make value of mutex from 0 to 1 and thus successfully get inside the critical section. Now, we have P1 and P10 in the critical section with the value of mutex 1.
  • And again P2 will try to enter the critical section, but because now the value of mutex is 1 it will successfully get inside of critical section.
  • Now we have P1, P10 and P2 inside the critical section. And all the other processes from P3-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 become 1 and P3 can enter the critical section, now mutex value is 0, we will execute the second code and again the P10 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 because P10 code has v() operation at the entry section. And we know that v() operation increase the value from 0 to 1 and if it is 1 then it remains 1.

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

📧 Write a mail

rahulmishra102000@gmail.com

🚀 Other links

GitHub
HackerRank

Top comments (0)