2,842 views
3 votes
3 votes

Q : Each process Pi , i = 1 to 9 executes the following code :

 while (TRUE)

 {

        P(mutex);

       Critical section ;

        V(mutex);

}

The process P10 executes the following code :

while(TRUE)

{

        V(mutex);

        critical section ;

        P(mutex);

}

Initial value of binary semaphore "mutex"  =  1.Then what is the maximum number of processes that may be present in the critical section at any instant of time?

a)      2

b)      3

c)      9

d)     10

e)      1

Plz explain in details which one is true out of these options.

2 Answers

Best answer
9 votes
9 votes

Initially Binary semaphore $\color{green}{mutex = 1}$, and we know that a binary semaphore can have only two states $0$ (or) $1$. So, even if we perform an UP on semaphore $s = 1$, no advantage here. So, best way here would be proceeding with an alternating sequence of $0$ and $1$.

 // Code for process 0-9            //Code for process 10
 while (TRUE) {                     while(TRUE) {
    P(mutex);                          V(mutex);
    <Critical section>                 <Critical section>
    V(mutex);                          P(mutex);
}                                   }

Suppose We start with process $P_{1}$, means $P_{1}$ enters Critical section by performing a DOWN on semaphore mutex. At this point $ \color{green}{ mutex = 0}$, Now $P_{10}$ executes and performs an UP on mutex and enters Critical section. At this point $ \color{green}{ mutex = 1}$, Now, Any one process out of $P_{2} \dots P_{9}$ enters critical section by performing a DOWN on binary semaphore mutex.Finally, $ \color{green}{ mutex = 0}$ and there is no way we could perform an UP on binary semaphore without allowing any process to leave Critical Section.

So, Maximum number of processes which are allowed to enter critical section $\color{red}{= 3}$ (two out of $P_{1} \dots P_{9}$ and $P_{10}$)

selected by
1 votes
1 votes
p1 to p9 requires mutex value to be=1 to enter into CS, right? But, p10 does UP() to enter CS,Observe the below sequence:
suppose initial value of mutex=1
p1: P(mutex), p1 is in CS, and mutex=0
now, p2 to p9 all 8 processes come to enter into CS, But as mutex=0 they can't and get blocked and enqueued in the list.
Then, p10 comes to enter CS, but according to UP function, if there are waiting processes in the list, so p10 wakes up one by one all processes and thus p2,p3,..,p9 all enter into CS.
Atlast, p10:V(mutex), p10 is in CS and now mutex=1
So, all 10 processes can enter into CS at the same time.
edited by