This document discusses deadlocks in a multiprogramming system. It defines deadlock as a situation where a set of processes are waiting indefinitely for resources held by each other in a circular chain. Four necessary conditions for deadlock are explained: mutual exclusion, hold and wait, no preemption, and circular wait. Methods for handling deadlocks include prevention, avoidance, detection and recovery. Prevention methods aim to enforce restrictions to ensure at least one condition cannot be met, such as allocating all resources for a process upfront or not allowing processes to hold resources while waiting for others.
2. DEADLOCKS
⚫ In a multiprogramming environment, several processes may compete for a
finite number of resources. A process requests resources; and if the
resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because
the resources it has requested are held by other waiting processes. This
situation is called a deadlock
System Model:
⚫ Every Process needs resources to perform its tasks.
⚫ Every Process will do the following w. r.t resource:-
1)Request the Resource
2)Use the Resource
3)Release the Resource
3. DEADLOCKS
• There are 2 types of Resources:-
1)Physical Resources [Memory, CPU, Printer,…..]
2)Logical Resources [Files, Semaphores,….]
• There can be multiples instances of each resource type [4-printers, 3-
scanners]
• Resources can be classified into 2 types:-
1)Sharable Resources:- Reading a File [n-processes can use [read] the file at a
time]
2)Non Sharable Resources:- Writing a File [Only 1-process is allowed at a
time]
• When process requests a resource,
the following 2 possibilities may happen:-
1)If Resource is Free - Resource will be allocated to the requested process
2)If Resource is Busy - Process will wait until resource will be free [device
queue]
4. DEADLOCKS
⚫ Deadlock:- A set of processes {P1, P2, …..,Pn} are in deadlock state, if and
only if every process in the set is waiting for a resource that is held by
another process in the set.
• Waiting (vs) Deadlock ?
• Deadlock -Indefinite waiting
Necessary Conditions
A deadlock situation can arise if the following four conditions hold
simultaneously in a system:
1. Mutual exclusion:
At least one resource must be held in a nonsharable mode; that is, only one
process at a time can use the resource. If another process requests that
resource, the requesting process must be delayed until the resource has been
released.
5. DEADLOCKS
⚫ 2) Hold and Wait A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held by other
processes.
⚫ 3) No Preemption Resources cannot be preempted.; that is, a resource can
be released only voluntarily by the process holding it, after that process has
completed its task
⚫ 4) Circular Wait A set of processes { P0, P1, P2, . . ., Pn } waiting must
exist such that P0 is waiting for a resource that is held by P1 ,P1 is waiting
for a resource that is held by P2, ….Pn-1 is waiting for a resource held by
Pn, and Pn, is waiting for a resource held by P0.
6. Resource-Allocation-Graph
Resource-Allocation-Graph
• The resource-allocation-graph (RAG) is a directed graph that can be used to
describe the deadlock situation.
• RAG consists of a → set of vertices (V) and → set of edges (E).
• V is divided into two types of nodes
1) P={P1,P2…..... Pn} i.e., set consisting of all active processes in the system.
2) R={R1,R2… ........Rn} i.e., set consisting of all resource types in the
system.
• E is divided into two types of edges:
1) Request Edge - A directed-edge Pi → Rj is called a request edge. - Pi
→ Rj indicates that process Pi has requested a resource Rj.
2) Assignment Edge - A directed-edge Rj → Pi is called an assignment
edge. - Rj → Pi indicates that a resource Rj has been allocated to
process Pi.
7. Resource-Allocation-Graph
• Pictorially,
⚫ → each process Pi is represented as a circle.
⚫ → each resource-type Rj is represented as a rectangle.
⚫ →resource type Rj may have more than one instance, each such instance is
represented as a dot within the rectangle.
⚫ →a request edge points to only the rectangle R →an assignment edge must
also designate one of the dots in the rectangle.
⚫ When process P, requests an instance of resource type Rj, a request edge is
inserted in the resource-allocation graph.
• When this request can be fulfilled, the request edge is instantaneously
transformed to an assignment edge.
• When the process no longer needs access to the resource, it releases the
resource; as a result, the assignment edge is deleted
9. Resource-Allocation-Graph
• The sets P, R, and E:
P={P1 ,P2, P3,}
R= {R1, R2, R3, R4}
E = {P1→ R1, P2→R3,R1→P2,R2→P2,R2→P1,R3→P3 }
• Resource instances:
-One instance of resource type R1
-Two instances of resource type R2
-One instance of resource type R3
– Three instances of resource type R4
• Process states:
- Process P1 is holding an instance of resource type R2 and is waiting for an
instance of resource type R1.
- Process P2 is holding an instance of R1 and an instance of R2 and is waiting
for an instance of R3.
- Process P3 is holding an instance of R3.
10. Resource-Allocation-Graph
⚫ Case I :Cycle with Deadlock wit all Resource Types having only 1-instance.
⚫ Case II :Cycle with Deadlock with at least one Resource type having
multiple instances.
⚫ Case III :Cycle with No Deadlock with at least one Resource type having
multiple instances
12. Resource-Allocation-Graph
Case II :Cycle with Deadlock with at least one Resource type having
multiple instances.
• If each resource type has several instances, then a cycle does not necessarily
imply that a deadlock has occurred.
• In this case, a cycle in the graph is a necessary but not a sufficient condition
for the existence of deadlock.
To illustrate this concept, consider the resource-allocation graph depicted in
Figure(a)
13. Resource-Allocation-Graph
•
Suppose that process P3 requests an instance of resource type R2.
• Since no resource instance is currently available, a request edge P3 —> R2
is added to the graph as shown Figure(b).
• At this point, two minimal cycles exist in the system:
• Processes P1, P2, and P3 are deadlocked.
• Process P2 is waiting for the resource R3, which is held by process P3
• Process P3 is waiting for either process P1 or process P2 to release resource R2.
• Process P1 is waiting for process P2 to release resource R1.
14. Resource-Allocation-Graph
⚫ Case III :Cycle with No Deadlock with at least one Resource type having
multiple instances.
consider the resource-allocation graph in Figure
• In this example, we also have a cycle P1 → R1 → P3→ R2 → P1
• However, there is no deadlock.
• Observe that process P4 may release its instance of resource type R2.
• That resource can then be allocated to P3, breaking the cycle
15. Methods for Handling Deadlocks
The following are the 3 ways for handling deadlocks:-
⚫ 1) Ensure that system will never enter into a deadlock state by
⚫ 1.Deadlock Prevention -Deadlock prevention provides a set of methods for
ensuring that at least one of the necessary conditions cannot hold.
⚫ 2. Deadlock Avoidance -Deadlock avoidance requires that the operating
system be given in advance additional information concerning which
resources a process will request and use during its lifetime. -With this
additional knowledge, it can decide for each request whether or not the
process should wait.
16. Methods for Handling Deadlocks
2) Allow the system to enter into deadlock state, detect it & recover from it by
⚫ 1.Deadlock Detection -an algorithm that examines the state of the
system to determine whether a deadlock has occurred
⚫ 2. Recovery from Deadlock - an algorithm to recover from the deadlock
3) Ignore the problem altogether and pretend that deadlocks never occur in the
system
⚫ -In this case, the undetected deadlock will result in deterioration of the
system's performance
⚫ -Eventually, the system will stop functioning and will need to be restarted
manually.
⚫ -this method is used in most operating systems, In many systems, deadlocks
occur infrequently (say, once per year);
⚫ -thus, this method is cheaper than the prevention, avoidance, or detection
and recovery methods
17. Deadlock-Prevention
Deadlock-Prevention
⚫ • Deadlocks can be eliminated by preventing at least one of the four
required conditions:
⚫ 1) Mutual exclusion 2) Hold-and-wait 3) No preemption 4) Circular-wait.
Mutual Exclusion
• This condition must hold for non-sharable resources.
• For example: A printer cannot be simultaneously shared by several
processes.
• On the other hand, shared resources do not lead to deadlocks.
• For example: Simultaneous access can be granted for read-only file.
• A process never waits for accessing a sharable resource.
• In general, we cannot prevent deadlocks by denying the mutual-exclusion
condition because some resources are non-sharable by default.
18. Deadlock-Prevention
Hold and Wait
• To prevent this condition: The processes must be prevented from holding
one or more resources while simultaneously waiting for one or more other
resources.
• Two possible solution to this problem.
• For example: Consider a process that
→ copies the data from a tape drive to the disk
→ sorts the file and
→ then prints the results to a printer.
Protocol-1 :Each process must be allocated with all of its resources before
it begins execution.
-All the resources (tape drive, disk files and printer) are allocated to the
process at the beginning.
19. Deadlock-Prevention
⚫ Protocol-2 :A process must request a resource only when the process
has none.
⚫ - Initially, the process is allocated with tape drive and disk file.
⚫ - The process performs the required operation and releases both tape drive
and disk file.
⚫ - Then, the process is again allocated with disk file and the printer
⚫ - the process performs the required operation & releases both disk file and
the printer.
Disadvantages of above 2 methods:
1).Resource utilization may be low, since resources may be allocated but
unused for a long period.
2) Starvation is possible. A process that needs several popular resources may
have to wait indefinitely, because at least one of the resources that it needs
is always allocated to some other process.
20. Deadlock-Prevention
⚫ No Preemption
• To prevent this condition: the resources must be preempted.
• There are several solutions to this problem.
Protocol-1
• If a process is holding some resources and requests another resource that
cannot be immediately allocated to it, then all resources currently being held
are preempted.
• The preempted resources are added to the list of resources for which the
process is waiting.
• The process will be restarted only when it regains the old resources and the
new resources that it is requesting.
21. Deadlock-Prevention
⚫ Protocol-2
• When a process request resources, we check whether they are available or
not.
• If they are, we allocate them.
• If they are not, we check whether they are allocated to some other process
that is waiting for additional resources.
• If so, we preempt the desired resources from the waiting process and allocate
them to the requesting process.
• If the resources are neither available nor held by a waiting process, the
requesting process must wait.
• While it is waiting, some of its resources may be preempted, but only if
another process requests them.
• A process can be restarted only when it is allocated the new resources it is
requesting and recovers any resources that were preempted while it was
waiting.
22. Deadlock-Prevention
• These 2 protocols may:
- be applicable for resources whose states are easily saved and restored, such
as registers and memory.
� not applicable to other devices such as printers and tape drives.
Circular-Wait
• One way to ensure that this condition never holds is to impose a total
ordering of all resource types and to require that each process requests
resources in an increasing order of enumeration.
• Each resource type a unique integer number, which, allows us to compare
two resources and to determine whether one precedes another in our
ordering.
• we define a one-to-one function F: R —> N, where N is the set of natural
numbers. i.e F(R1) = 1 F(R2) = 2 F(R3) = 3
23. Deadlock-Prevention
• Deadlock can be prevented by using the following 2 protocol:
⚫ Protocol :1 - Require the processes to request resources only in
increasing/decreasing order.
⚫ Protocol-2 -If process wants to request ―Rj, then it must release all
allocated resources ―Ri where F(Ri) > F(Rj). [R2, R3 to be released to
request R1].
24. Deadlock Avoidance
• The general idea behind deadlock avoidance is to avoid deadlocks from ever
happening.
• Deadlock-avoidance algorithm
→ Requires more information about each process.
• With the knowledge of the complete sequence of requests and releases for
each process, the system can decide for each request whether or not the
process should wait in order to avoid a possible future deadlock.
• Each request requires in making the decision based on
→ The resources currently available,
→The resources currently allocated to each process, and
→The future requests and releases of each process.
25. Deadlock Avoidance
⚫ Various algorithms that use this approach differ in the amount and type of
information required.
1) In simple algorithms, the algorithm only needs to know the maximum
number of each resource that a process might potentially use.
2) In complex algorithms, the algorithm can also take advantage of the
schedule of exactly what resources may be needed in what order.
• The resource-allocation state is defined by
→ the number of available resources
→allocated resources
→ the maximum demand of each process.
• A deadlock-avoidance algorithm dynamically examines the resources
allocation state to ensure that a circular-wait condition never exists.
26. Deadlock Avoidance
⚫ Safe State
• A state is safe if the system can allocate all resources requested by all
processes without entering a deadlock state.
• A state is safe if there exists a safe sequence of processes {P0, P1, P2, ...,
PN} such that the requests of each process(Pi) can be satisfied by the
currently available resources.
• If a safe sequence does not exist, then the system is in an unsafe state, which
may lead to deadlock.
• All safe states are deadlock free, but not all unsafe states lead to deadlocks as
shown in figure.
28. Deadlock Avoidance
⚫Safe state
To illustrate, we consider a system with 12 magnetic tape drives and three
processes: P0, P1, and P3.
• Table below shows maximum resource needed , current allocation and how
much they need to complete the work.
Thus, there are 3 free tape drives.
• At time t0, the system is in a safe state.
• The sequence < P1, P0, P2> satisfies the safety condition.
29. Deadlock Avoidance:Safe state
Since 2 tape drive is left ,Process P1 can immediately be allocated all
its tape drives, and then return them
•Therefore the system will then have 5 available tape drives
• Then process P0 can get all its tape drives and return them
• the system will then have10 available tape drives
• process P0 can get all its tape drives and return them
• thus the system will then have all 12 tape drives available.
30. Safe state
⚫ At time t2, process P2 requests one more tape drive and is allocated
one more tape drive, then table shows current state.
Thus, there are 2 free tape drives left.
• Now process P1 can complete its work as it needs only 2 tape drive, thus
returns them
• Now System have 4 tape drives
• Neither P0 nor P2 can complete as P0 require 5 tape drives and P2
requires 6 tape drives.
• A system can go from a safe state to an unsafe state, resulting in a
deadlock.
• mistake was in granting the request from process P2 for one more tape
drive.
31. Deadlock Avoidance
⚫ Resource-Allocation-Graph Algorithm
• If resource categories have only single instances of their resources, then deadlock
states can be detected by cycles in the resource-allocation graphs.
• In this case, unsafe states can be recognized and avoided by augmenting the
resource-allocation graph with claim edges (denoted by a dashed line).
• Claim edge Pi → Rj indicated that process Pi may request resource Rj at some
time in future.
The important steps are as below:
1) When a process Pi requests a resource Rj, the claim edge Pi → Rj is converted
to a request edge.
2) Similarly, when a resource Rj is released by the process Pi, the assignment edge
Rj → Pi is reconverted as claim edge Pi → Rj.
3) The request for Rj from Pi can be granted only if the converting request edge to
assignment edge do not form a cycle in the resource allocation graph.
32. Deadlock Avoidance
⚫ To apply this algorithm, each process Pi must know all its claims before it starts
executing.
• Conclusion:
1) If no cycle exists, then the allocation of the resource will leave the system in a
safe state.
2) If cycle is found, system is put into unsafe state and may cause a deadlock.
For example:
Consider a resource allocation graph shown in Figure(a).
- Suppose P2 requests R2.
� Though R2 is currently free, we cannot allocate it to P2 as this action will create
a cycle in the graph as shown in Figure (b).
- This cycle will indicate that the system is in unsafe state: because, if P1 requests
R2 and P2 requests R1 later, a deadlock will occur.
34. Deadlock Avoidance
• Problem: The resource-allocation graph algorithm is not applicable when
there are multiple instances for each resource.
• Solution: Use banker's algorithm.
35. Banker's Algorithm
Banker's Algorithm
• This algorithm is applicable to the system with multiple instances of each
resource types.
• However, this algorithm is less efficient than the resource-allocation-graph
algorithm.
• When a process starts up, it must declare the maximum number of
resources that it may need.
• This number may not exceed the total number of resources in the system.
• When a request is made, the system determines whether granting the
request would leave the system in a safe state.
• If the system in a safe state, the resources are allocated; else the process
must wait until some other process releases enough resources.
36. Banker's Algorithm
⚫• Assumptions:
Let n = number of processes in the system
Let m = number of resources types.
• Following data structures are used to implement the banker‘s
algorithm.
1) Available: - A vector of length m, indicates the no. of available
resources of each type. - If Available[j]=k, then k instances of
resource type Rj is available.
2) Max - an n X m matrix indicates the maximum demand of each
process of each resource. - If Max[i][j]=k, then process Pi may
request at most k instances of resource type Rj.
3) Allocation - an n X m matrix ,indicates no. of resources currently
allocated to each process. - If Allocation[i][j]=k, then Pi is currently
allocated k instances of Rj.
4) Need - an n X m matrix, indicates the remaining resources need of
each process. -If Need[i][j]=k, then Pi may need k more instances of
resource Rj to complete its task.
- So, Need[i][j] = Max[i][j] - Allocation[i][j]
37. Banker's Algorithm
Safety Algorithm
Algorithm to verify whether the system is safe (or) not after allocating the
requested resource
1. Let Work and Finish be vectors of length m and n, respectively. Initialize
Work = Available and Finish[i] =false for i= 0,1, ..., n-l.
2. Find an i such that both
a. Finish[i] ==false
b. Needi ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation, Finish[i] = true Go to step 2.
4. If Finish[i] = true for all. i then the system is in a safe state.
38. Banker's Algorithm
⚫ Resource-Request Algorithm
• This algorithm determines if a new request is safe, and grants it only if it is
safe to do so.
• When a request is made ( that does not exceed currently available resources ),
pretend it has been granted, and then see if the resulting state is a safe one.
• If so, grant the request, and if not, deny the request.
• Let Requesti be the request vector of process Pi.
• If Requesti[j]=k, then process Pi wants K instances of the resource type Rj.
• When a request for resources is made by process Pi, the following actions are
taken:
40. Bankers algorithm
⚫ An Illustrative Example Question: Consider the following snapshot of a
system:
• The content of the matrix Need
• given by Need = Max - Allocation
• So, the content of Need Matrix is:
41. Banker's Algorithm
⚫ Step 1: Initialization Work = Available i.e. Work =3 3 2
…P0……P1…P2…..P3……P4.
Finish = | false | false | false | false | false |
⚫ Step 2: For i=1 Finish[P1] = false and Need[P1]<=Work i.e. (1 2 2)<=(3 3
2) - true So P1 can complete its task and hence must be kept in safe
sequence.
⚫ Step 3: Work = Work + Allocation[P1] =(3 3 2)+(2 0 0)=(5 3 2)
…….P0…P1…….P2….P3…..P4……
Finish = | false | true | false | false | false |
⚫ Step 2: For i=2 Finish[P2] = false and Need[P2]<=Work i.e. (6 0 0)<=(5 3
2) - false So P2 must wait.
42. Banker's Algorithm
⚫ Step 2: For i=3 Finish[P3] = false and Need[P3]<=Work i.e. (0 1 1)<=(5 3
2) - true So P3 can complete its task and hence must be kept in safe
sequence.
⚫ Step 3: Work = Work + Allocation[P3] = (5 3 2)+(2 1 1)=(7 4 3)
……P0…P1…P2…..P3….P4….
Finish = | false | true | false | true | false |
Step 2: For i=4 Finish[P4] = false and Need[P4]<=Work i.e. (4 3 1)<=(7 4 3) -
true So P4 can complete its task and hence must be kept in safe sequence.
Step 3: Work = Work + Allocation[P4] =(7 4 3)+(0 0 2)=(7 4 5)
…P0……P1… P2… P3….P4…..
Finish= | false | true | false | true | true |
43. Banker's Algorithm
⚫ Step 2: For i=0 Finish[P0] = false and Need[P0]<=Work i.e. (7 4 3)<=(7 4
5) -true So P0 can complete its task and hence must be kept in safe
sequence.
⚫ Step 3: Work = Work + Allocation[P0] =(7 4 5)+(0 1 0)=(7 5 5)
…..P0….P1…P2….P3….P4….
Finish= | true | true | false | true | true |
⚫ Step 2: For i=2 Finish[P2] = false and Need[P2]<=Work i.e. (6 0 0) <=(7 5
5) - true So P2 can complete its task and hence must be kept in safe
sequence.
Step 3: Work = Work + Allocation[P2] =(7 5 5)+(3 0 2)=(10 5 7) ..P0…
.P1……P2….P3…P4…. Finish= | true | true | true | true | true |
⚫ Step 4: Finish[Pi] = true for 0<=i<=4 Hence, the system is currently in a
safe state. The safe sequence is <P1, P3, P4, P0, P2>. Conclusion: Yes, the
system is currently in a safe state.
44. Deadlock Detection:
⚫ Deadlock Detection:
• If a system does not employ either deadlock-prevention or deadlock-
avoidance algorithm then a deadlock may occur.
• In this environment, the system must provide
1) An algorithm to examine the system-state to determine whether a
deadlock has occurred.
2) An algorithm to recover from the deadlock.
▪ Single Instance of Each Resource Type
• If all the resources have only a single instance, then deadlock detection-
algorithm can be defined using a
• A wait-for-graph (WAG) is a variation of the resource-allocation-graph.
• The wait-for-graph is applicable only to a resource type with single instance.
• The wait-for-graph can be obtained from the resource-allocation-graph by
→ removing the resource nodes
→ collapsing the appropriate edges.
45. Deadlock Detection:
• An edge from Pi to Pj implies that process Pi is waiting for process Pj to
release a resource that Pi needs.
• An edge Pi → Pj exists if and only if the corresponding graph contains two
edges
1) Pi → Rq and 2) Rq → Pj.
• For example: Consider resource-allocation-graph shown in Figure (a) and its
corresponding wait-for-graph is shown in Figure(b).
46. Deadlock Detection:
• A deadlock exists in the system if and only if the wait-for-graph contains a
cycle.
• To detect deadlocks, the system needs to
→ maintain the wait-for-graph
→ periodically execute an algorithm that searches for a cycle in the graph.
▪ Several Instances of a Resource Type
• The wait-for-graph is applicable to only a single instance of a resource type.
• Problem: However, the wait-for-graph is not applicable to a multiple
instance of a resource type.
• Solution: The following detection-algorithm can be used for a multiple
instance of a resource type.
• Assumptions: Let ‗n‘ be the number of processes in the system
Let ‗m‘ be the number of resources types. •
47. Deadlock Detection:
⚫ Following data structures are used to implement this algorithm.
⚫ 1) Available - A vector of length m, indicates the no. of available resources
of each type. - If Available[j]=k, then k instances of resource type Rj is
available.
⚫ 2) Allocation -A n X m matrix indicates no. of resources currently allocated
to each process. - If Allocation[i][j]=k, then Pi is currently allocated k
instances of Rj.
⚫ 3) Request - A n X m matrix indicates the current request of each process. -
If Request [i][j] = k, then process Pi is requesting k more instances of
resource type Rj.
49. Deadlock Detection:
To illustrate this algorithm,
⚫ consider a system with five processes P0 through P4 and three resource types
A, B, and C.
⚫ Resource type A has seven instances, resource type B has two instances, and
resource type C has six instances.
⚫ Suppose that, at time To, we have the following resource-allocation state:
⚫ the system is not in a deadlocked state.
⚫ if we execute our algorithm, we will find that the sequence <P0, P2, P3, P1,
P4> results in Finish[i]== true for all i.
52. Deadlock Detection:
Suppose now that process Pj makes one additional request for an instance of type C.
• The Request matrix is modified as follows:
P0:000<=000 true
Work=000+010=010
P1: 202<=010 Fasle
P2:001<=010 Fasle
P3:100<=010 false
P4:002<=010 False
The system is now deadlocked.
• Although we can reclaim the resources held by process Po, the number of available
resources is not sufficient to fulfil the requests of the other processes.
• Thus, a deadlock exists, consisting of processes P1, P2, P3, and P4.
53. Detection-Algorithm Usage
• The detection-algorithm must be invoked based on following factors:
1. How often is a deadlock likely to occur?
2. How many processes will be affected by deadlock when it happens?
• If deadlocks occur frequently, then the detection-algorithm should be
executed frequently.
• Resources allocated to deadlocked-processes will be idle until the deadlock is
broken.
Problem: Deadlock occurs only when some processes make a request that
cannot be granted immediately.
• Solution 1: -The deadlock-algorithm must be executed whenever a request
for allocation cannot be granted immediately.
-In this case, we can identify
→ set of deadlocked-processes
→ specific process causing the deadlock.
54. ⚫-Problem: if the deadlock-detection algorithm is invoked for every resource
request, this will incur a considerable overhead in computation time.
• Solution 2: -The deadlock-algorithm must be executed in periodic intervals.
- For example:
→ once in an hour
→ whenever CPU utilization drops below certain threshold
-Problem: -
If the detection algorithm is invoked at arbitrary points in time, there may be
many cycles in the resource graph.
-In this case, we would not be able to tell which of the many deadlocked
processes "caused" the deadlock
55. Recovery from deadlock
When a detection algorithm determines that a deadlock exists, several
alternatives are available.
One possibility is to inform the operator that a deadlock has occurred and to
let the operator deal with the deadlock manually.
Another possibility is to let the system recover from the deadlock
automatically.
There are two options for breaking a deadlock.
One is simply to abort one or more processes to break the circular wait.
The other is to preempt some resources from one or more of the deadlocked
processes.
.
56. Process Termination
Two methods to remove deadlocks:
1) Abort all deadlocked-processes.
-This method will definitely break the deadlock-cycle.
- However, this method incurs great expense.
This is because
→ Deadlocked-processes might have computed for a long time.
→ Results of these partial computations must be discarded.
→ Probably, the results must be re-computed later.
2) Abort one process at a time until the deadlock-cycle is eliminated.
-This method incurs large overhead.
-This is because after each process is aborted,
-deadlock-algorithm must be executed to determine if any other process is still
deadlocked
57. Resource Preemption
• Some resources are taken from one or more deadlocked-processes.
• These resources are given to other processes until the deadlock-cycle is
broken.
Three issues need to be considered:
1) Selecting a victim
⮚ Which resources/processes are to be pre-empted (or blocked)?
⮚ - The order of pre-emption must be determined to minimize cost.
⮚ - Cost factors includes
1. The time taken by deadlocked-process for computation.
2. The no. of resources used by deadlocked-process.
58. Resource Preemption
2) Rollback - If a resource is taken from a process, the process cannot
continue its normal execution. - In this case, the process must be
rolled-back to some safe state -This method requires the system to
keep more information about the state of all running processes. - it is
difficult to determine what a safe state is,
- the simplest solution is a total rollback: Abort the process and then
restart it.
3) Starvation Problem: In a system where victim-selection is based on
cost-factors, the same process may be always picked as a victim. -As
a result, this process never completes its designated task, leading to
starvation.
Solution: - Ensure a process is picked as a victim only a (small) finite
number of times. - example: based on number of rollbacks.
59. MEMORY MANAGEMENT
• Memory management is concerned with managing the primary
memory.
• Memory consists of array of bytes or words each with their own
address.
• The instructions are fetched from the memory by the cpu based on the
value program counter.
⚫ Basic Hardware
• Program must be
→ brought (from disk) into memory and
→ placed within a processor for it to be run.
• Main-memory and registers are only storage CPU can access directly.
• Register can be access in one CPU clock.
• Main-memory can take many cycles.
• hence Cache a memory buffer, which sits between main-memory and
CPU registers.
60. MEMORY MANAGEMENT
we also must ensure correct operation, has to protect the operating system from
access by user processes and in addition to protect user processes from one
another.
This protection must be provided by the hardware.
• each process has a separate memory space.
• also has the range of legal addresses that the process may access and the
process can access only these legal addresses.
• this protection is provided by using two registers, a base and a limit,
• The base register holds the smallest legal physical memory address;
• the limit register specifies the size of the range
61. MEMORY MANAGEMENT
• For example, if the base register holds 300040 and limit register is
120900, then the program can legally access all addresses from 300040
through 420940 (300040+120900) (inclusive). illustrated in Figure (a)
62. MEMORY MANAGEMENT
⚫ Protection of memory space is accomplished by having the CPU hardware
compare every address generated in user mode with the registers(base and
limit).
• Any attempt by a program executing in user mode to access operating-system
memory or other users' memory results in a trap to the operating system, which
treats the attempt as a fatal error (as shown in Figure b).
• This scheme prevents a user program from (accidentally or deliberately)
modifying the code or data structures of either the operating system or other
users.
• The base and limit registers can be loaded only by the operating system, which
uses a special privileged instruction.
• The operating system, executing in kernel mode, is given unrestricted access to
both operating system and users' memory.
64. MEMORY MANAGEMENT
⚫ Address Binding
• A program resides on a disk as a binary executable file.
• To be executed, the program must be brought into memory and placed within a
process.
• The processes on the disk that are waiting to be brought into memory for
execution form the input queue.
• A user process to reside in any part of the physical memory.
• Although the address space of the computer starts at 00000, the first address of
the user process need not be 00000.
• This approach affects the addresses that the user program can use.
65. ⚫ Multistep processing of a user program
• A user program will go through several steps—some of which maybe
optional-—before being executed (as shown in Figure below).
• Addresses may be represented in different ways during these steps
• Addresses in the source program are generally symbolic (such as count).
• A compiler will typically bind these symbolic addresses to relocatable
addresses
• The linkage editor or loader will in turn bind the relocatable addresses to
absolute addresses
• Each binding is a mapping from one address space to another.
67. ⚫ Classically, the binding of instructions and data to memory addresses can
be done at any step along the way:
• Compile time.
• If you know at compile time where the process will reside in memory, then
absolute code can be generated.
• For example, if you know that a user process will reside starting at location
R, then the generated compiler code will start at that location and extend up
from there.
• If, at some later time, the starting location changes, then it will be necessary
to recompile this code.
68. • Load time.
• If it is not known at compile time where the process will reside in memory,
then the compiler must generate relocatable code.
• In this case, final binding is delayed until load time.
• If the starting address changes, we need only reload the user code to
incorporate this changed value.
• Execution time.
• If the process can be moved during its execution from one memory segment
to another, then binding must be delayed until run time.
• Special hardware must be available for this scheme to work,
69. Logical versus Physical Address Space
• Logical-address is generated by the CPU (also referred to as virtual-address).
• Physical-address is the address seen by the memory-unit i.e, the one loaded into the
MAR(memory address register)
• The set of all logical addresses generated by a program is a logical address space
• The set of all physical addresses corresponding to these logical addresses is a physical
address space.
• Logical & physical-addresses are identical in compile-time & load-time address-
binding methods.
• Logical and physical-addresses differ in execution-time address-binding method.
• The run-time mapping from virtual to physical addresses is done by a hardware
device called the memory-management unit (MMU).
71. • The user program never sees the real physical addresses.
• The user program deals with logical addresses.
• The memory-mapping hardware converts logical addresses into physical
addresses
• The base register is now called a relocation register.
• The value in the relocation register is added to every address generated by a
user process that is logical address at the time it is sent to memory to get the
physical address as shown in figure.
• For example, if the base is at 14000, then an attempt by the user to access a
location 346 is mapped to location 14346.
72. Dynamic Loading
• Dynamic Loading can be used to obtain better memory-space utilization.
• A routine is not loaded until it is called.
This works as follows:
1) Initially, all routines are kept on disk in a relocatable-load format.
2) Firstly, the main-program is loaded into memory and is executed.
3) When a main-program calls the routine, the main-program first
checks to see whether the routine has been loaded.
4) If routine has been not yet loaded, the loader is called to load desired routine into
memory.
5) Finally, control is passed to the newly loaded-routine.
73. Advantages:
1).An unused routine is never loaded.
2) Useful when large amounts of code are needed to handle infrequently
occurring cases.
3) Although the total program-size may be large, the portion that is used (and
hence loaded) may be much smaller.
4) Does not require special support from the OS. -It is the responsibility of the
users to design their programs to take advantage of such a method.
-Operating systems may help the programmer, however, by providing library
routines to implement dynamic loading
74. Dynamic Linking and Shared Libraries
Static linking
• system language libraries are treated like any other object module and are
combined by the loader into the binary program image.
• This requirement wastes both disk space and main memory.
Dynamic linking:
The concept of dynamic linking is similar to that of dynamic loading.
• Linking postponed until execution-time.
• A stub is included in the image for each library-routine reference.
• The stub is a small piece of code used to locate the appropriate memory-
resident library-routine.
75. Dynamic Linking and Shared Libraries
• When the stub is executed, it checks to see whether the needed
routine is already in memory. If not, the program loads the routine
into memory.
• Stub replaces itself with the address of the routine, and executes the
routine.
• Thus, the next time that particular code-segment is reached, the
library-routine is executed directly, incurring no cost for dynamic-
linking.
• All processes that use a language library execute only one copy of the
library code.
•
76. Dynamic Linking and Shared Libraries
•Shared libraries:
• A library may be replaced by a new version, and all programs that reference
the library will automatically use the new one. programs linked before the
new library was installed will continue using the older library. This system
is also known as shared libraries.
• Version info. is included in both program & library so that programs won't
accidentally execute incompatible versions.
• Dynamic linking generally requires help from the operating system.
• If the processes in memory are protected from one another, then the operating
system is the only entity that can check to see whether the needed routine is
in another process's memory space or that can allow multiple processes to
access the same memory addresses.
77. Swapping
⚫ Swapping
⚫ • A process must be in memory to be executed.
⚫ • A process can be
→ swapped temporarily out-of-memory to a backing-store and
→ then brought into memory for continued execution.
⚫ • Backing-store is a fast disk which is large enough to accommodate
copies of all memory-images for all users.
Why we need to swap out the process?
⚫ 1) In Round Robin When -a quantum expires, the memory manager will
start to swap out the process that just finished and to swap another process
into the memory space that has been freed as shown in Figure.
79. Swapping
⚫ 2) Roll out/Roll in is a swapping variant used for priority- based
scheduling algorithms.
-Lower-priority process is swapped out so that higher-priority process can be
loaded and executed.
-Once the higher-priority process finishes, the lower-priority process can be
swapped back in and continued
⚫ • Swapping depends upon address-binding:
1)If binding is done at load-time, then process cannot be easily moved to a
different location.
2) If binding is done at execution-time, then a process can be swapped into a
different memory space, because the physical-addresses are computed
during execution-time.
• Major part of swap-time is transfer-time; i.e. total transfer-time is directly
proportional to the amount of memory swapped.
80. Swapping
Disadvantages:
⚫ 1) Context-switch time is fairly high.
⚫ 2) If we want to swap a process, we must be sure that it is completely idle.
[reason: -if the I/O is asynchronously accessing the user memory for I/O
buffers, then the process cannot be swapped. -Assume that the I/O operation
is queued because the device is busy. - If we were to swap out process P1
and swap in process P2, the I/O operation might then attempt to use
memory that now belongs to process P2.]
Two solutions:
i) Never swap a process with pending I/O.
ii) Execute I/O operations only into OS buffers
81. Contiguous Memory Allocation
⚫ Contiguous Memory Allocation
• Memory is usually divided into 2 partitions:
→ One for the resident OS.
→ One for the user-processes.
• Each process is contained in a single contiguous section of memory.
Memory Mapping & Protection
• Memory-protection means
→ protecting OS from user-process and
→ protecting user-processes from one another.
• Memory-protection is done using
→ Relocation-register: contains the value of the smallest physical-address.
→ Limit-register: contains the range of logical-addresses.
• Each logical-address must be less than the limit-register.
• The MMU maps the logical-address dynamically by adding the value in the
relocation-register. This mapped-address is sent to memory (as shown in
Figure).
83. Memory Mapping & Protection
⚫ When the CPU scheduler selects a process for execution, the dispatcher
loads the relocation and limit-registers with the correct values.
• Because every address generated by the CPU is checked against these
registers, we can protect the OS from the running user process.
• The relocation-register scheme provides an effective way to allow the OS
size to change dynamically.
For example,
-the operating system contains code and buffer space for device drivers.
-If a device driver (or other operating-system service) is not commonly used,
we do not want to keep the code and data in memory, as we might be able to
use that space for other purposes.
-Such code is sometimes called transient operating-system code; it comes and
goes as needed. -Thus, using this code changes the size of the operating
system during program execution.
84. Memory Allocation
⚫ Memory Allocation
• One of the simplest methods for allocating memory is to divide memory into
several fixed-sized partitions.
- Each partition may contain exactly one process.
-The degree of multiprogramming is bound by the number of partitions.
• When a partition is free, a process is
→ selected from the input queue and
→ loaded into the free partition.
• When the process terminates, the partition becomes available for another
process.
• The OS keeps a table indicating
→ which parts of memory are available
→ which parts are occupied.
85. Memory Allocation
• A hole is a block of available memory.
• Normally, memory contains a set of holes of various sizes.
• Initially, all memory is
→ available for user-processes
→ considered one large hole.
• In general, at any given time we have a set of holes of various sizes scattered
throughout memory.
• When a process arrives and needs memory, the system searches the set for a
hole that is large enough for this process.
• If the hole is too large, it is split into two parts.
- One part is allocated to the arriving process;
-The other is returned to the set of holes.
• When a process terminates, it releases its block of memory, which is then
placed back in the set of holes.
86. • If the new hole is adjacent to other holes, these adjacent holes are merged to
form one larger hole.
• At this point, the system may need to check whether there are processes
waiting for memory and whether this newly freed and recombined memory
could satisfy the demands of any of these waiting processes.
• This procedure is a particular instance of the general dynamic storage
allocation problem, which concerns how to satisfy a request of size n from a
list of free holes.
87. Three strategies used to select a free hole from the set of available holes.
1) First Fit
-Allocate the first hole that is big enough.
- Searching can start either
→ at the beginning of the set of holes or
→ at the location where the previous first-fit search ended.
2) Best Fit
- Allocate the smallest hole that is big enough.
-We must search the entire list, unless the list is ordered by size.
- This strategy produces the smallest leftover hole.
3) Worst Fit
- Allocate the largest hole.
-Again, we must search the entire list, unless it is sorted by size.
-This strategy produces the largest leftover hole.
89. Fragmentation
• Two types of memory fragmentation:
1).Internal fragmentation
2). External fragmentation
Internal Fragmentation
• The general approach is to
→ break the physical-memory into fixed-sized blocks
→ allocate memory in units based on block size
• The allocated-memory to a process may be slightly larger than the requested-
memory.
• The difference between requested-memory and allocated-memory is called
internal fragmentation i.e. Unused memory that is internal to a partition.
90. 2) External Fragmentation
• External fragmentation occurs when there is enough total memory-space to
satisfy a request but the available-spaces are not contiguous. (i.e. storage is
fragmented into a large number of small holes).
• Both the first-fit and best-fit strategies for memory-allocation suffer from
external fragmentation.
• Statistical analysis of first-fit reveals that
-given N allocated blocks, another 0.5 N blocks will be lost to fragmentation.
-This property is known as the 50-percent rule.
91. Two solutions to external fragmentation :
1) Compaction
-The goal is to shuffle the memory-contents to place all free
memory together in one large hole. Compaction is possible only if
relocation is
→ dynamic
→ done at execution-time.
2) Permit the logical-address space of the processes to be non-
contiguous.
- This allows a process to be allocated physical-memory wherever such
memory is available.
-Two techniques achieve this solution:
⚫ 1) Paging and 2) Segmentation.
92. Paging
•Paging is a memory-management scheme. This permits the physical-address
space of a process to be non-contiguous.
• This also solves the considerable problem of fitting memory-chunks of
varying sizes onto the backing-store.
• Traditionally: Support for paging has been handled by hardware.
• Recent designs: The hardware & OS are closely integrated.
Basic Method
• Physical-memory is broken into fixed-sized blocks called frames.
• Logical-memory is broken into same-sized blocks called pages.
• The backing-store is divided into fixed-sized blocks that are of the same size
as the memory-frames.
• When a process is to be executed, its pages are loaded into any available
memory-frames from the backing- store.
93. • Every address generated by the CPU is divided into two parts:
- a page number (p)
- a page offset (d).
• The page number is used as an index into a page table.
• The page table contains the frame number of each page in physical memory
• Then, base address= frame no. x page size
• This base address is combined with the page offset to define the physical
memory address that is sent to the memory unit.
94. The page size (like the frame size) is defined by the hardware.
• The size of a page is typically a power of 2, varying between 512 bytes and
16 MB per page, depending on the computer architecture.
• The selection of a power of 2 as a page size makes the translation of a
logical address into a page number and page offset particularly easy
• If -the size of logical address space is 2m -a page size is 2n addressing units
(bytes or words), then -the high-order m – n bits of a logical address
designate the page number, and - the n low-order bits designate the page
offset.
where p is an index into the page table and d is the displacement within the
page.
95. • When a process arrives in the system to be executed, its size, expressed in
pages, is examined. Each page of the process needs one frame.
• Thus, if the process requires n pages, at least n frames must be available in
memory.
• If n frames are available, they are allocated to this arriving process.
• The first page of the process is loaded into one of the allocated frames, and
the frame number is put in the page table for this process.
• The next page is loaded into another frame, and its frame number is put into
the page table, and so on as shown in Figure below
. • Free frames (a) before allocation and (b) after allocation
97. Paging
Paging example (as shown in figure below)
• Page size 4bytes -22 bytes, hence 2 bits of logical address represent page no
• a 32-byte memory ( 32bytes/4bytes=8 pages)
• process has 4 pages
-hence 4*4 =16 bytes, so 16 logical address i.e, 0 to 15,
-hence 4 bit logical address(2 bit page no. and 2 bit offset that has to be added
to base address in the page table)
• hence page table has 4 entries (2bit sufficient to represent page number i.e
0,1,2,3 )
• page table contains the base address of each page in physical memory
• this base address is add with the offset in the logical address to get the exact
location in physical location
98. Paging
logical address = 3 (address of ‗d‘)-in binary = 0011
-1st two bits (00) is the page no i.e, page 0 and -in page table for page0 , 5 is the frame no - hence
base address is i.e 5x4(page size)=20
-Next 2 bits (11) of logical address i.e, 3 the offset is added to base address 20 , i.e., 20+3=23
which is the is the physical address of letter ‗d‘ - i.e logical address 3 maps to (5x4)+3=23
• similarly, logical address 4 maps to (6x4)+0=24 (4 in binary 0100 , so page no. 1 and offset 00
,page table entry is 6]
99. Paging
⚫ The hardware support for paging is illustrated in Figure
Valid Invalid Bit : to trap illegal addresses
• This bit is attached to each entry in the page-table as shown in the figure
below
1) Valid bit: The page is in the process‘ logical-address space.
2) Invalid bit: The page is not in the process‘ logical-address space.
• The OS sets this bit for each page to allow or disallow access to the page.
100. Paging
Example: Given
• a system has a 14-bit address space (0 to 16383 i.e, (214 - 1)),
• a program that use only addresses 0 to 10468 (i.e, 10469 bytes)
• a page size of 2 KB(2048 bytes),
• Then program requires 6 pages (10469/2048= 5.11 =6pages)
• thus we get the situation shown in Figure .
101. Paging
• Addresses in pages 0,1,2,3,4, and 5 are mapped normally through the page
table and the valid-invalid bit is set to valid
• Any attempt to generate an address in pages 6 or 7, however, will find that
the valid-invalid bit is set to invalid, and the computer will trap to the
operating system (invalid page reference).
Disadvantage:
• Because the program extends to only address 10468, any reference beyond
that address is illegal.
• However, references to page 5 are classified as valid, so accesses to
addresses up to 12287 are valid.
• This problem is a result of the 2-KB page size and the internal fragmentation
of paging.