CSE Roadmap

Common tier · about 5 hours · 7 parts

Part 2 of 7 · 1 min read

Avoidance

Think of it this way. A small-town bank has 10 million in cash and three business customers. Each customer has a credit line: a promise that it may borrow up to some maximum. The banker never lends so much that she could not satisfy at least one customer's full credit line, wait for the repayment, and then serve the next. That is the whole idea. The analogy stops in one place: a bank customer can repay in cash from other income, while a thread can only release what it holds when it finishes.

In plain words: For threads, this means two requirements. First, each thread must declare in advance the largest number of units of each resource it may ever hold. This is its maximum claim. Second, before granting a request, the system must check what would happen after the grant: could the threads still all finish, one after another, in some order? If yes, it grants. If not, the thread must wait, even though the resource is free right now.

Deadlock avoidance

Deadlock avoidance is a method in which every thread declares its maximum claim in advance, and the system grants a request only if the state after the grant is safe; it keeps all four deadlock conditions possible, but never enters a state from which a deadlock cannot be avoided.

Example: a free printer is not given to a thread if, after the grant, the remaining free units could not satisfy any thread's remaining claim.

Check yourself: 1. What must each thread tell the system in advance? 2. What is the difference between prevention and avoidance in how they treat the four conditions? 3. Why might the system refuse a request for a resource that is free at that moment?