1

Can you give an example of a deadlock scenario between 2 processes (Process A and Process B) where each process needs to access the resources File A, File B, and File C. Present a scenario that allows the 2 processes to become deadlocked?

How would I resolve the deadlock scenario? How would I prevent it from happening? What would be the cost of prevention?

Due to lack of experience, I can't think of a scenario.

saki18
  • 45
  • 1
  • 5
  • 1
    The question provides the scenario - you have 2 processes that both want to lock the same 3 files. Imagine what happens if the one process tries to lock file A then lock file B, and the other process tries to lock file B and then lock file A. In this case; one process can lock file A and the other can lock file B; and neither process can lock their second file (because the other process locked it) so you get deadlock. – Brendan Jul 24 '19 at 02:32

1 Answers1

2

Take two processes A and B. Also, take the general guideline when a process is trying to access a file system, It needs to acquire a resource lock, and then access the file system. When the resource lock is acquired by a process, no other process can acquire the same resource lock, and hence, cannot access the file system. Also, the locks are handled by semaphores with wait forever property, so when a process tries to acquire a lock and it's not available, it's going to wait forever at that point, without proceeding(in a pending state).

Process A is going to access filesystem A. It acquires lock for it, and is working on it. Due to context switching, it goes out of context.

Now process B is going to access filesystem B. It acquires lock for it, and is working on it. Due to context switching, it goes out of context.

Now process A, without releasing the lock for file system A, tries to acquire lock for file system B. The lock is not available, so it is going to enter pending state, and does not proceed further.

Now process B is scheduled, which again, without releasing the lock for file system B, tries to acquire lock for file system A. The lock is not available, so it is going to enter pending state, and does not proceed further.

So one process holds the resource needed for the other process to proceed further, so both processes are now deadlocked.