edited by
11,170 views
32 votes
32 votes

Processes $P1$ and $P2$ use critical_flag in the following routine to achieve mutual exclusion. Assume that critical_flag is initialized to FALSE in the main program.

get_exclusive_access ( )
{
    if (critical _flag == FALSE) {
        critical_flag = TRUE ;
        critical_region () ;
        critical_flag = FALSE;
    }
}


Consider the following statements.

  1. It is possible for both $P1$ and $P2$ to access critical_region concurrently.
  2. This may lead to a deadlock.

Which of the following holds?

  1. (i) is false (ii) is true
  2. Both (i) and (ii) are false
  3. (i) is true (ii) is false
  4. Both (i) and (ii) are true
edited by

3 Answers

Best answer
63 votes
63 votes

(C) Both process can run the critical section concorrently. Lets say $p_{1}$ starts and it enters inside if claus and just after its entertence and before execution of critical_flag = TRUE, a context switch happens and $p_{2}$ also gets entrance since the flag is still false. So, now both process are in critical section! So, (i) is true. (ii) is false there is no way that flag is true and no process' are inside the if clause, if someone enters the critical section, it will definetly make flag = false. So. no. deadlock.

edited by
3 votes
3 votes
Say P1 starts first and executes statement 1, after that system context switches to P2 (before executing statement 2), and it enters inside if statement, since the flag is still false.
So now both processes are in critical section!! so (i) is true.. (ii) is false
By no way it happens that flag is true and no process’ are inside the if clause, if someone enters the critical section, it will definitely make flag = false. So no deadlock.
1 votes
1 votes
here deadlock won't occur as the condition here is "if(critical_flag==false)"  not while(critical_flag==false);.

consider process P1 currently in critical section  preempts before setting critical _flag=False.

process P2 performs condition (True==false) gives false and hence comes out of the if condition and hence its execution is completed.there is no loop here (unlike while();)and hence no dependency on other processes.

Hence no deadlock.

for first answer both can enter critical sections as preemption could be done after if condition and before setting critical_flag as true  resulting multiple processes accessing critical sections at same time.
Answer:

Related questions