Consider a system consisting of π instances of a resource π
, being shared by
5 processes. Assume that each process requires a maximum of two instances of
resource π
and a process can request or release only one instance at a time. Further,
a process can request the second instance of the resource only after acquiring the
first instance.
The minimum value of π for the system to be deadlock-free is ________. (answer
in integer)
Think about the worst case scenario that could lead to deadlock. Each of the 5 processes might grab 1 instance of R and then stall, waiting for a second instance, since it must hold the first before it can even ask for the second.
If there are only \(k=5\) instances total, this worst case is exactly what could happen: all 5 processes each hold 1 instance, all 5 instances are used up, and every process is now stuck waiting forever for one more instance that no one will ever release - a real deadlock, so \(k=5\) is not safe.
Now try \(k=6\). Even in the same worst case where all 5 processes have grabbed 1 instance each, using 5 of the 6 instances, there is still 1 instance left over. That spare instance can be handed to any one waiting process, letting it reach its maximum need of 2, finish its work, and release both instances back to the pool. Those freed instances then let the remaining processes proceed one after another, so the system never gets stuck.
This matches the general safe-allocation rule: for \(n\) processes each needing at most \(m\) instances, the deadlock-free minimum is \(k = n(m-1) + 1\). Plugging in \(n=5\), \(m=2\) gives \(k = 5(1) + 1 = 6\).
Consider the following threads, T1, T2, and T3 executing on a single processor, synchronized using three binary semaphore variables, S1, S2, and S3, operated upon using standard wait() and signal(). The threads can be context switched in any order and at any time.

Consider the following pseudocode, where S is a semaphore initialized to 5 in line#2 and counter is a shared variable initialized to 0 in line #1. Assume that the increment operation in line#7 is not atomic.
1. int counter = 0;
2. Semaphore S = init(5);
3. void parop(void)
4. {
5. wait(S);
6. wait(S);
7. counter++;
8. signal(S);
9. signal(S);
10. } If five threads execute the function parop concurrently, which of the following program behavior(s) is/are possible?