• edited by
61,059 views
193 193 votes

A multithreaded program $P$ executes with $x$ number of threads and uses $y$ number of locks for ensuring mutual exclusion while operating on shared memory locations. All locks in the program are non-reentrant, i.e., if a thread holds a lock $l$, then it cannot re-acquire lock $l$ without releasing it. If a thread is unable to acquire a lock, it blocks until the lock becomes available. The minimum value of $x$ and the minimum value of $y$ together for which execution of $P$ can result in a deadlock are:

  1. $x = 1, y = 2$
  2. $x = 2, y = 1$
  3. $x = 2, y = 2$
  4. $x = 1, y = 1$

13 Answers

Best answer
103 103 votes

If we see definition of reentrant Lock :

In computer science, the reentrant mutex (recursive mutex, recursive lock) is particular type of mutual exclusion (mutex) device that may be locked multiple times by the same process/thread, without causing a deadlock.   https://en.wikipedia.org/wiki/Reentrant_mutex

A Re-entrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html

Reentrant property is provided, so that a process who owns a lock, can acquire same lock multiple times. Here it is non-reentrant as given, process cant own same lock multiple times. So if a thread tries to acquire already owned lock, will get blocked, and this is a deadlock.

Here, the answer is (D).

• edited by
39 39 votes

Here is my thought
 Consider two conditions ,

First one for option C) if there are 2 threads and 2 locks available then one by one each thread acquire a lock , ensure Mutual Exclusion and enter into it's shared memory location  then release it .. and again get another lock in the same manner  , hence deadlock is Not satisfied due to Non-reentrant property !! because it said " if a thread holds a lock , then it cannot re-acquire lock  without  releasing it " .

Now consider again ,

For option D ) if there are only 1 thread and 1 lock is available then a thread after acquire a lock enter into it's critical section then come out of it again looking for another lock but due to a single lock acquire that same lock and get blocked so the possibility of Deadlock is there ..

as in question it is asked for  minimum value .. minimum is X =1 and Y = 1

In Tanenbaum book it is said " Notice that it is possible for a single process to become deadlocked if it is waiting for an event that only it can cause. "...          

    Here X threads waiting for Y locks to ensure mutual exclusion are in deadlock when X =1 and Y = 1 .

Hence Option D is correct.

10 10 votes

How single thread can deadlock with a non-reentrant lock (Option D), an example just to give idea how it is possible:

Let the thread execute a function \( t() \) which recursively calls itself.  
Since the first statement inside \( t() \) is \( \text{acquire}(L) \), the recursive call
attempts to acquire the same lock again while the same thread already holds it.  
Because the lock is non-reentrant, this second acquire cannot succeed and the thread blocks,
causing a self-deadlock.

int i = 0;    // thread variable
lock L;       // NON-reentrant lock

void t() {
    acquire(L);     // first acquire

    if (i == 0) {
        i = 1;
        t();        // recursive call → tries acquire(L) again
    }

    release(L);
}

(Note: Example is minimal and illustrative; real implementations may differ)

Execution:

1. First call to t(): acquire(L) succeeds because the lock is free.  
2. Since i = 0, the function makes a recursive call to t().  
3. The recursive call again begins with its first statement: acquire(L).  
4. But the same thread already holds L, and since L is a non-reentrant lock, this second acquire blocks.  
5. The thread is now waiting for a lock that it itself holds, so it can never reach release(L) → this results in a self-deadlock.

If L were reentrant, the second acquire(L) would simply be treated as the same thread re-entering the lock, the recursion count would increase, and the program would continue normally with no blocking and no deadlock.

3 3 votes

Non-Reentrant Locks : if a thread holds a lock $l$, it cannot re-acquire lock $l$ without releasing it first. If it tries, it will block until the lock becomes available.

 

The Minimum Deadlock Scenario

A deadlock occurs when a thread is blocked indefinitely, waiting for a resource that will never be released. Because the lock is non-reentrant, a single thread can deadlock itself (often called a self-deadlock).

Consider a program with just $1$ thread ($x = 1$) and $1$ lock ($y = 1$):

  1. The thread successfully acquires the lock.

  2. While still holding the lock (perhaps within a recursive function call or a poorly structured loop), the thread attempts to acquire the exact same lock again.

  3. Because the lock is non-reentrant, the thread blocks, waiting for the lock to become available.

  4. Since the thread itself is the current owner of the lock, it will never be able to wake up and release it.

Therefore, the minimum number of threads and locks required to cause a deadlock in this specific system is $x = 1$ and $y = 1$.

Ans: D)

ago
2 2 votes
First, you have to know multithreading, mutual exclusion and reentrant mutex. The reentrant mutex (recursive mutex, recursive lock) is a particular type of mutual exclusion (mutex) device that may be locked multiple times by the same process/thread, without causing a deadlock.
Here non-re-entrant process can’t own the same lock multiple times, so if the process tries to acquire the already owned lock, will get blocked, and deadlock will happen.
From the above option, x=1 (a single thread) and y=1 (a single lock) deadlock are possible when we consider given situations in question.

so ans is option d...
1 1 vote

This is my view on the question  -

According to one of the definition in Operating Systems by Galvin

A set of processes is deadlocked when every process in the set is waiting for a resource that is currently allocated to another process in the set ( and which can only be released when that other waiting process makes progress. )

Reference - https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/7_Deadlocks.html

When one thread tries to re-acuquire the same lock which it is currently helding could be best termed as a BUG and not deadlock. The question is ambiguous as it involves the term "dead-lock".  The question was made tricky due to the presence of non-renentrant lock, however the official key did not taken into consideration the definition and appropriate terminology of deadllock. More emphsasis was given on the non-renentrant lock rather than deadlock terminology. I feel answer should be option C)

• edited by
Answer:
Position:
Show:

Related questions

71 71 votes
7 answers 7 answers
29.0k
29.0k views
Arjun asked Feb 14, 2017
29,039 views
Recall that Belady's anomaly is that the page-fault rate may increase as the number of allocated frames increases. Now, consider the following statements:$S_1$: Random pa...
89 89 votes
12 answers 12 answers
28.8k
28.8k views
Arjun asked Feb 14, 2017
28,820 views
A cache memory unit with capacity of $N$ words and block size of $B$ words is to be designed. If it is designed as a direct mapped cache, the length of the $\textsf{TAG}$...
75 75 votes
4 answers 4 answers
30.2k
30.2k views
Arjun asked Feb 14, 2017
30,203 views
Consider the expression $(a-1) * (((b+c)/3)+d)$. Let $X$ be the minimum number of registers required by an optimal code generation (without any register spill) algorithm ...
145 145 votes
11 answers 11 answers
61.4k
61.4k views
Arjun asked Feb 14, 2017
61,418 views
Consider a $2$-way set associative cache with $256$ blocks and uses $\text{LRU}$ replacement. Initially the cache is empty. Conflict misses are those misses which occur d...