edited by
19,189 views
43 43 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

6 Answers

Best answer
75 75 votes

(C) Both process can run the critical section concorrently. Lets say $p_{1}$ starts and it enters inside if clause and just after its entrance 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 = true . So , no deadlock.

edited by
3 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 1 vote
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.
0 0 votes
a)For Mutual Exclusion:

1)If One process is in critical section then other process is not able to get into Critical Section.

2) If Both the process want to get into CS then atmost one process can get into Critical Section.

In the question It ensures the first condition we know that intial value of critical_flag=false, Let us consider p1  make if condition true and make

critical_flag=true and enter in CS and p2 wants to enter into Cs it cannot enter as it stops at if condition because the p1 has already updated the variable value as

true.

Now to satisfy second condition if Both the process want to enter into CS then both the process satisfy if condition and enter into CS.Now there are more two processes in the

Cs,so it violates the condition of mutual exclusion and hence P1 and P2 access critical section concurrently.

b)It doesnt leads to leadlock as atleast one process can enter into CS.
0 0 votes
2) not deadlock, even with `critical_flag = TRUE` initially. It becomes a progress violation (indefinite postponement), which is a different liveness failure.

 hypothetical

Initial state: `critical_flag = TRUE`, nobody has ever entered the CS.

Step    Process    Action    Result   
1    P1    `if (critical_flag == FALSE)` → FALSE    skips CS, moves on   
2    P2    `if (critical_flag == FALSE)` → FALSE    skips CS, moves on   

The flag is stuck at TRUE forever, because the only place it gets reset to FALSE is at the end of the critical region — and nobody ever reaches there. So no process can ever enter the CS.

Why this is NOT deadlock (check Coffman conditions)

Condition    Holds here?   
Mutual exclusion    (vacuously) yes   
Hold and wait    ❌ No — nobody holds any resource while waiting   
No preemption    n/a   
Circular wait    ❌ No — P1 isn't waiting for P2, and P2 isn't waiting for P1   

Two conditions fail ⇒ deadlock is impossible. Crucially: the processes are not blocked on each other — they're not even blocked at all. A failed `if` means skip and continue, not wait.

What it actually is

- Progress violated: no process is in the CS, processes want to enter, yet the "decision" of who enters is postponed indefinitely. Progress is one of the three required CS properties (mutual exclusion, progress, bounded waiting) — this construction kills it.
- If `get_exclusive_access()` is called in a loop, both processes keep testing and skipping forever — this is closer to starvation/livelock (active, but zero progress), still not deadlock.

Traps

- "Nobody ever enters" ≠ deadlock. Deadlock = a set of processes blocked, waiting for events only each other can trigger. Here the wait isn't on another process — it's on a flag nobody can reset. GATE marks these differently.
- Deadlock needs waiting — a plain `if` never waits. If the code had `while (critical_flag == TRUE);` instead, then your scenario would freeze both processes and the deadlock discussion would change.
- Note the original question initializes the flag to FALSE, so this stuck-TRUE state can't arise from the given code — it's purely a (valid) what-if.
Answer:
Position:
Show:

Related questions

50 50 votes
6 answers 6 answers
17.4k
17.4k views
Ishrat Jahan asked Oct 30, 2014
17,390 views
Synchronization in the classical readers and writers problem can be achieved through use of semaphores. In the following incomplete code for readers-writers problem, two ...
139 139 votes
18 answers 18 answers
42.6k
42.6k views
Ishrat Jahan asked Oct 30, 2014
42,613 views
The head of a hard disk serves requests following the shortest seek time first (SSTF) policy. What is the maximum cardinality of the request set, so that the head changes...
43 43 votes
2 answers 2 answers
11.0k
11.0k views
Ishrat Jahan asked Oct 30, 2014
10,994 views
The head of a hard disk serves requests following the shortest seek time first $\textsf{(SSTF)}$ policy. The head is initially positioned at track number $180$.Which of t...
98 98 votes
6 answers 6 answers
38.1k
38.1k views
Ishrat Jahan asked Oct 30, 2014
38,068 views
A demand paging system takes $100$ time units to service a page fault and $300$ time units to replace a dirty page. Memory access time is $1$ time unit. The probability o...