Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like to know if my solution to this problem is correct
ThreadA and ThreadB both need both resources, P and Q, in order to do some work. Each Thread can acquire a resource with lockQ() or lockP(). The resources cannot be shared. If a Task tries to acquire a resource that is already in use, it blocks. When a Task no longer needs a resource it owns it can release it with unlockP() or unlockQ().
Each task runs its own code and uses the functions lockP(), lockQ(), doWork(), unlockP() and unlockQ(). Each thread releases its resources after invokes “doWork()”; the resources are released in the opposite order they were obtained. Show a sequence of operations for each thread so that deadlock is impossible.
Solution:
Task A
lockP();
lockQ();
doWork();
unlockQ();
unlockP();
Task B
lockP();
lockQ();
doWork();
unlockQ();
unlockP();
The reason I locked P and Q first for both, was because if the processor was executing TaskA first, TaskB would become blocked. This would allow for TaskA to finish execution, and then move on to TaskB which would e unblocked.
Yes your solution is correct.
An alternative is for Task B to lockQ before lockP, while Task A does lockP and lockQ. But this has the potential for dead lock if B gets Q and A gets P. In such a case both task's are blocked.
As you mention, having both acquire the resources in the same order, ensures one is blocked without causing the other to block on the other resource.
Coffman, Elphick and Shoshani (1971) identified four conditions for occurrence of a dead lock[1],
Shared resources which are used under mutual exclusion
Incremental acquisition
No preemption (once acquired, a task will not give up a resource until it has completed its work)
Wait-for cycle
You must nullify one of these conditions for a guarantee of no deadlocks occurring. By ensuring both tasks acquire the resources in the same order, you have make sure condition 4 does not hold, i.e. A, B will never both hold a resource and wait for a second resource at the same time.
[1]: Magee, Kramer, Concurrency State Models & Java Programs, page 107
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I must solve this problem:
Two processes, A and B, each need three records, 1, 2, and 3, in a
database. If A asks for them in the order 1, 2, 3, and B asks for them
in the same order, deadlock is not possible. However, if B asks for
them in the order 3, 2, 1, then deadlock is possible. With three
resources, there are 3! or six possible combinations in which each
process can request them. What fraction of all the combinations is
guaranteed to be deadlock free?
And I've seen the solution to this problem in a book:
123 deadlock free
132 deadlock free
213 possible deadlock
231 possible deadlock
312 possible deadlock
321 possible deadlock
Since four of the six may lead to deadlock, there is a 1/3 chance of
avoiding a deadlock and a 2/3 chance of getting one.
But I can't figure out what logic is behind of this solution.
Would someone please explain why this solution is correct?
I've searched a lot but didn't find anything and all of the answers to this problem was without clear explanation.
Deadlock occurs when both threads must wait to acquire a lock that the other thread already acquired (causing both threads to wait forever).
If both threads try to acquire the same lock first then only one thread can succeed and the other must wait, and the waiting thread will be waiting while not holding any locks, so deadlock can't happen because the thread that acquired lock 1 will be able to acquire all other locks it wants and will be able to release lock 1 when its finished (which allows the waiting thread to continue).
E.g. if A and B try to acquire lock 1 first and A wins, then B waits while not holding any lock and A can acquire any other locks in any order it wants because B isn't holding any locks (and then A will release lock 1 and B can stop waiting).
In a given set of processes if some of them can be executed and rest can't because of resources they are requesting for are being held by some other processes. Do we call such a situation as deadlock?
A deadlock situation occurs when none of the process's requests is fulfilled. Each process will be in circular wait, waiting for resources held by other processes.
Necessary Condition for deadlock is
Mutual exclusion
Hold and wait
No pre-emption
Circular wait
Here in this situation, some processes can execute and rest are not able to, so the processes that are not allocated resources will surely be in circular wait.
Hence so this situation can't be called clearly a deadlock situation.
You can go thorugh Operating systems text book by ABRAHAM SILBERSCHATZ (Wiley) 'The Dinasour Book'.
According to Operating System Concepts book, Resource-Allocation-Graph Algorithm can prevent deadlocks as follow:
If we have the following allocation graph
https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/images/Chapter7/7_07_DeadlockAvoidance.jpg
And P1 tried to allocate resource R2, the system prevents it and makes it wait, because that will lead to an unsafe state.
My question is as shown from the graph, P2 is waiting for P1 to release R1, and P1 is now waiting to allocate R2 and that leads to a deadlock. How this algorithm can prevent this type of deadlocks ?
I don't have a copy of your book, but I suspect a typo. The idea is to return an error (EDEADLOCK) to the resource allocation request that would complete the cycle; thus detecting pending deadlock rather than actively avoiding it. It is still up to the process with the failed request to take some corrective action, like dropping all its resources and trying to re-acquire them.
If you replace resources with semaphore or mutex, it should be clear that waiting isn't going to help anything.
To actively avoid deadlock, you pretty much need to either use semaphore sets -- that is acquire all the locks that a particular code path will need in one place (see system V semaphores) -- or arrange your code to use a particular ordering of locks. An example of the latter is to allocate locks by increasing address, thus all actors will attempt the allocation in the same order. Neither is practical for finely grained general purpose code, but possible for transaction processing applications.
Consider a system that has three processes and three identical resources. Each process needs a maximum of two resources. Is deadlock possible in this system?
It it my understanding that deadlock is possible if the four conditions hold simultaneously:
Mutual exclusion, hold and wait, no pre-emption, and circular wait.
If each process is allocated one resource, then all three resources will be held. There is no forth resource available.
How do I go about proving that deadlock is not possible, and how would I calculate how many resources should be available to make the system deadlock free?
In such cases the trick is to evaluate the CIRCULAR WAIT condition and see if it holds or not. 3 Processes and 3 identical resources. Let us give 1 to each of them. 0 resources left, yet no process requirement is complete(as each needs 2),which means that every process is waiting for some other process to release the resources. Circular wait condition satisfied. Therefore the given scenario can lead to deadlock.
Suppose we have n processes and m identical resources with maximum demands as d1,d2,d3......dn.
If m > (d1-1) + (d2-1) + (d3-1)......(dn-1)., then its deadlock free,
otherwise it can lead to deadlock
Consider a system with m resources of the same type being shared by n
processes. Resources can be requested and released by processes only on at a
time. The system is deadlock free if and only if the sum of all max needs is < m+n.
For example:
A system has 3 processes sharing 4 resources. If each process needs a maximum of 2 units, then:
To make a system deadlock free, assign each process with one less then their max need. After doing so if we are left with one or more resources then there is no deadlock.
Assign 1 resource (max need -1) to each process.
allocated resources=1+1+1=3
we are still left with 1 resource to avoid deadlock.
so deadlock can never occur.
Deadlock-
A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does.
For deadlock to happen all these four conditions must hold simultaneously
Mutual exclusion
Hold and wait
No preemption
Circular wait
we apply deadlock detection algorithm to check whether the system is in deadlock or not. But if any of the above criterion fails(For example No preemption fails, so some resource is being released) which incur the system to be deadlock free. So what I think, if the deadlock detection algorithm finds the state to be unsafe and all the above four criterion holds true simultaneously then we can say the system is in deadlock.
Unsafe state may or may not lead to deadlock.
But unsafe state with all these 4 conditions holding simultaneously must incur deadlock.
Am I thinking right?
I have another question in my mind. How can we say deadlock has occurred definitely because the next moment some process may release their resources to get rid of deadlock.
Am I thinking right?
Yes, you are correct.
See this link to see why unsafe may not lead to deadlock.
I have another question in my mind. How can we say deadlock has occurred definitely because the next moment some process may release their resources to get rid of deadlock.
Say deadlock has occurred. All processes causing the deadlock are waiting for some resource to be acquired. And because of "No preemption" no such process will get preempted and therefore release resources. Also because of "Hold and wait" property, process needs some more resources to continue but is not going to give up or release whatever it is holding now and will wait till its required resources are met. Once there is deadlock, nothing can happen (there cannot be any progress) until you break one of the above condition. Breaking a condition will make some other process to meet its requirements and ensure progress and completion.