Algorithm Introduction
In a multiprogramming environment, several threads may compete for a finite number of resources. A thread requests resources; if the resources are not available at that time, the thread enters a waiting state. Sometimes, a waiting thread can never again change state, because the resources it has requested are held by other waiting threads. This situation is called a deadlock. Generally speaking, we can deal with the deadlock problem in one of three ways:
- We can ignore the problem altogether and pretend that deadlocks never occur in the system. Called
Deadlock Prevention
. - We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state. Called
Deadlock Avoidance
. - We can allow the system to enter a deadlocked state, detect it, and recover. Called
Deadlock Dectection & Recovery
.
Data structure for algorithm
m
: the number of resource types. Resource types includen
: the number of threads. Threads areAvailable
: the vector of length m indicates the number of available resources of each type. For example, if , then instances of resource type are available.Max
: An matrix defines maximum demand of each thread. If , then thread request at most k instances of resource type .Allocation
: An matrix defines the number of resources of each type currently allocated to each thread. If , then thread is currently allocated k instances of resource type .Need
: An matrix indicates the remaining resource need of each thread. If , then thread may need instances of resource type to complete its task. Note that .
What is Resource-Allocation State?
Resource-Allocation State is defined by the number of instances of resources :
- available in the system
- allocated for threads
- maximum for each thread
- The system can allocate resources to each thread (up to its maximum) in some order and still avoid a deadlock.
- Exists state is safe.
- A sequence of threads is a safe sequence for the current allocation state.
For example:
Suppose that, system has 12 resources. At time , resources.
Maximum Needs | Current Needs | |
---|---|---|
10 | 5 | |
4 | 2 | |
9 | 7 |
At time , system is in safe state. Because, the sequence satifies the safety condition. With thread can immediately be allocated all its resources and then return them, ; then thread can get all its resources and return them, ; and finally thread T2 can get all its resources and return them.
Suppose that, at time ,thread requests and is allocated one more resource. At this point, only thread can be allocated all its resources. When it returns them, the system will have only four available resources. Since thread P_{0) is allocated five resources but has a maximum of ten, it may request five more resources. If it does so, it will have to wait, because they are unavailable. Similarly, thread may request six additional resources and have to wait, resulting in a deadlock. In this case, system go from safe state to unsafe state.
Safety algorithm
In this algorithm, we use vector. Consider vector X and Y. We say if and only if for all . Each row of is a vector.
Safety algorithm check current state: safe or unsafe? OR pseudo code:
Bool Safe(Current Resource-Allocation State) { Work <- Available; for(i: 1 -> n) Finish[i] = false; flag <- true; While(flag) { flag <- false; for(i: 1 -> n) { if(Finish[i] = false AND Need[i] <= Work) { finish[i] = true; Work <- Work + Allocation[i]; flag = true; }//endif }//endwhile for(i: 1->n) if(finish[i] = false) return false; return true; }
}
Resource-Request Algorithm
Data Structure:
Request[i]
: the request resource vector for thread . If then, thread wants instances of resource type .
Pseudo Code:
Void Resource-Request-Handling(Request[i]) { if(Request[i] > Need[i]) Error; // has exceeded its maximum claim if(Request[i] > Available) Block; // don't enough, wait // Setup new state Available = Available - Request[i]; Allocation[i] = Allocation[i] + Request[i]; Need[i] = Need[i] - Request[i]; // check safe or unsafe if(Safe(New Resource-Allocation State)) { Allocate(P[i]); // thread P[i] is allocated its resources } else { wait(P[i]); // P[i] wait // restore old state Available = Available + Request[i]; Allocation[i] = Allocation[i] - Request[i]; Need[i] = Need[i] + Request[i]; }
}
Practice exercise
Consider the following snapshot of a system:
- Illustrate that the system is in a safe state by demonstrating an order in which the threads may complete.
- If a request from thread arrives for , can the request be granted immediately?
Solution:
- The content of matrix is difined to be and is as follow:
Need | A | B | C | D | Allocation | A | B | C | D |
---|---|---|---|---|---|---|---|---|---|
3 | 3 | 3 | 2 | 3 | 1 | 4 | 1 | ||
2 | 1 | 3 | 0 | 2 | 1 | 0 | 2 | ||
0 | 1 | 2 | 0 | 2 | 4 | 1 | 3 | ||
2 | 2 | 2 | 2 | 4 | 1 | 1 | 0 | ||
3 | 4 | 5 | 4 | 2 | 2 | 2 | 1 |
This sequence of thread satisfies the safety criteria. This state is safe.
-
New state:
unsafe
can't be granted immediately.
References.
- Abraham Silberschatz - Operating System Concepts 10th 2018
- Silde Chater 2 - Thread Manage - Operating System, SoICT, HUST