Unit 3
Memory Management
Fixed Partitioning
• The earliest and one of the simplest technique which can be used to load more than
one processes into the main memory is Fixed partitioning or Contiguous memory
allocation.
• In this technique, the main memory is divided into partitions of equal or different
sizes. The operating system always resides in the first partition while the other
partitions can be used to store user processes. The memory is assigned to the
processes in contiguous way.
• In fixed partitioning,
• The partitions cannot overlap.
• A process must be contiguously present in a partition for the execution.
Fixed Partitioning - Cons
• 1. Internal Fragmentation
• If the size of the process is lesser then the total size of the partition then some size of the partition get
wasted and remain unused. This is wastage of the memory and called internal fragmentation.
• 2. External Fragmentation
• The total unused space of various partitions cannot be used to load the processes even though there is
space available but not in the contiguous form.
• 3. Limitation on the size of the process
• If the process size is larger than the size of maximum sized partition then that process cannot be loaded
into the memory. Therefore, a limitation can be imposed on the process size that is it cannot be larger
than the size of the largest partition.
• 4. Degree of multiprogramming is less
• By Degree of multi programming, we simply mean the maximum number of processes that can be loaded
into the memory at the same time. In fixed partitioning, the degree of multiprogramming is fixed and
very less due to the fact that the size of the partition cannot be varied according to the size of processes.
Fixed Partitioning - Pros
Dynamic Partitioning
• Dynamic Partitioning
• Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In this technique, the
partition size is not declared initially. It is declared at the time of process loading.
• The first partition is reserved for the operating system. The remaining space is divided into parts. The size
of each partition will be equal to the size of the process. The partition size varies according to the need of
the process so that the internal fragmentation can be avoided.
Advantages of Dynamic Partitioning over fixed
partitioning
• 1. No Internal Fragmentation
• Given the fact that the partitions in dynamic partitioning are created according to the need of the
process, It is clear that there will not be any internal fragmentation because there will not be any unused
remaining space in the partition.
• 2. No Limitation on the size of the process
• In Fixed partitioning, the process with the size greater than the size of the largest partition could not be
executed due to the lack of sufficient contiguous memory. Here, In Dynamic partitioning, the process size
can't be restricted since the partition size is decided according to the process size.
• 3. Degree of multiprogramming is dynamic
• Due to the absence of internal fragmentation, there will not be any unused space in the partition hence
more processes can be loaded in the memory at the same time.
Disadvantages of dynamic partitioning
• External Fragmentation
• Absence of internal fragmentation doesn't
mean that there will not be external
fragmentation.
• Let's consider three processes P1 (1 MB) and
P2 (3 MB) and P3 (1 MB) are being loaded in
the respective partitions of the main memory.
• After some time P1 and P3 got completed and
their assigned space is freed. Now there are
two unused partitions (1 MB and 1 MB)
available in the main memory but they cannot
be used to load a 2 MB process in the memory
since they are not contiguously located.
• The rule says that the process must be
contiguously present in the main memory to
get executed. We need to change this rule to
avoid external fragmentation.
Non-Contiguous Memory Allocation
•
• Non-contiguous memory allocation is a
memory allocation technique.
• It allows to store parts of a single process in
a non-contiguous fashion.
• Thus, different parts of the same process
can be stored at different places in the main
memory.
•
Paging
• In Operating Systems, Paging is a storage mechanism
used to retrieve processes from the secondary storage
into the main memory in the form of pages.
• The main idea behind the paging is to divide each
process in the form of pages. The main memory will also
be divided in the form of frames.
• One page of the process is to be stored in one of the
frames of the memory. The pages can be stored at the
different locations of the memory but the priority is
always to find the contiguous frames or holes.
• Pages of the process are brought into the main memory
only when they are required otherwise they reside in the
secondary storage.
• Different operating system defines different frame sizes.
The sizes of each frame must be equal. Considering the
fact that the pages are mapped to the frames in Paging,
page size needs to be as same as frame size.
Paging
• Example
• Let us consider the main memory size 16 Kb and Frame
size is 1 KB therefore the main memory will be divided
into the collection of 16 frames of 1 KB each.
• There are 4 processes in the system that is P1, P2, P3 and
P4 of 4 KB each. Each process is divided into pages of 1
KB each so that one page can be stored in one frame.
• Initially, all the frames are empty therefore pages of the
processes will get stored in the contiguous way.
• Frames, pages and the mapping between the two is
shown in the image below.
Paging
• Let us consider that, P2 and P4 are moved to waiting
state after some time. Now, 8 frames become empty and
therefore other pages can be loaded in that empty place.
The process P5 of size 8 KB (8 pages) is waiting inside the
ready queue.
• Given the fact that, we have 8 non contiguous frames
available in the memory and paging provides the
flexibility of storing the process at the different places.
Therefore, we can load the pages of process P5 in the
place of P2 and P4.
Paging
• Translating Logical Address into Physical Address-
• CPU always generates a logical address but aphysical address is needed to access the main memory.
• Following steps are followed to translate logical address into physical address-
• Step-01: CPU generates a logical address consisting of two parts- Page Number and Page Offset
• Page Number specifies the specific page of the process from which CPU wants to read the data.
• Page Offset specifies the specific word on the page that CPU wants to read.
• Step-02: For the page number generated by the CPU,
• Page Table provides the corresponding frame number (base address of the frame) where that page is stored in
the main memory.
• Step-03: frame number combined with the page offset forms the required physical address.
• Frame number specifies the specific frame where the required page is stored.
• Page Offset specifies the specific word that has to be read from that page.
Paging
Paging
• Advantages-
• It allows to store parts of a single process in a non-contiguous fashion.
• It solves the problem of external fragmentation.
•
• Disadvantages-
• It suffers from internal fragmentation.
• There is an overhead of maintaining a page table for each process.
• The time taken to fetch the instruction increases since now two memory accesses are required.
•
Paging - Page Table
• Page table is a data structure.
• It maps the page number referenced by the CPU to the frame
number where that page is stored.
• Characteristics-
• Page table is stored in the main memory.
• Number of entries in a page table = Number of pages in which the
process is divided.
• Page Table Base Register (PTBR) contains the base address of page
table.
• Each process has its own independent page table.
• PTBR provides the base address of the page table.
• The base address of the page table is added with the page number
referenced by the CPU.
• It gives the entry of the page table containing the frame number
where the referenced page is stored.
Performance of Demand Paging
• Demand paging can significantly affect the performance of a computer system. Let's compute the
effective access time for a demand-paged memory.
• For most computer systems, the memory-access time, denoted $ma$, ranges from 10 to 200
nanoseconds.
• As long as we have no page faults, the effective access time is equal to the memory access time.
• If, however a page fault occurs, we must first read the relevant page from disk and then access the
desired word.
• Let p be the probability of a page fault. We would expect p to be close to zero -that is, we would expect
to have only a few page faults.
• The effective access time is then
• effective access time = (1 - p)*ma + p*page fault time
• We are faced with three major components of the page-fault service time:
• Service the page-fault interrupt.
• Read in the page.
• Restart the process.
• Problem-01:
• Let the page fault service time be 10 ms in a computer with average memory access
time being 20 ns. If one page fault is generated for every 10-6 memory accesses, what is
the effective access time for the memory?
• It is given that effective memory access time without page fault = 20 ns.
• Now, substituting values in the above formula, we get-
• Effective access time with page fault
• = 10-6 x { 20 ns + 10 ms } + ( 1 – 10-6 ) x { 20 ns }
• = 10-6 x 10 ms + 20 ns
• = 10-5 ms + 20 ns
• = 10 ns + 20 ns
• = 30 ns
• Problem-02:
• Suppose the time to service a page fault is on the average 10 milliseconds, while a
memory access takes 1 microsecond. Then, a 99.99% hit ratio results in average memory
access time of-
• Page fault rate
= 1 – Hit ratio
• = 1 – 0.9999
• It is given that effective memory access time without page fault = 1 μsec.
• Effective access time with page fault
• = 0.0001 x { 1 + 10 msec } + 0.99999 x 1 μsec
• = 0.0001 μsec + 0.001 msec + 0.9999 μsec
• = 1 μsec + 0.001 msec
• = 1 μsec + 1 μsec
• = 2 μsec or 0.002 msec
Frame Allocation Algorithms
• There are three types of Frame Allocation Algorithms in Operating Systems.
• 1) Equal Frame Allocation Algorithms
• Here, in this Frame Allocation Algorithm we take number of frames and number of
processes at once. We divide the number of frames by number of processes. We get
the number of frames we must provide for each process.
• This means if we have 36 frames and 6 processes. For each process 6 frames are
allocated.
• It is not very logical to assign equal frames to all processes in systems with processes
of different sizes. A lot of allocated but unused frames will eventually be wasted if a
lot of frames are given to a little operation.
Frame Allocation Algorithms
• 2) Proportionate Frame Allocation Algorithms
• Here, in this Frame Allocation Algorithms we take number of frames based on the
process size. For big process more number of frames is allocated. For small processes
less number of frames is allocated by the operating system.
• The problem in the Proportionate Frame Allocation Algorithm is number of frames are
wasted in some rare cases.
• The advantage in Proportionate Frame Allocation Algorithm is that instead of equally,
each operation divides the available frames according to its demands.
• 3) Priority Frame Allocation Algorithms
• According to the quantity of frame allocations and the processes, priority frame
allocation distributes frames. Let's say a process has a high priority and needs more
frames; in such case, additional frames will be given to the process. Processes with
lower priorities are then later executed in future and first only high priority processes
are executed first.
Common Page Replacement
Algorithms
• First In First Out (FIFO)
• Optimal Page replacement
• Least Recently Used
• Most Recently Used (MRU)
Algorithms during Thrashing
Whenever thrashing starts, the operating system tries to apply either the Global page
replacement Algorithm or the Local page replacement algorithm.
1. Global Page Replacement
Since global page replacement can bring any page, it tries to bring more pages whenever
thrashing is found. But what actually will happen is that no process gets enough frames,
and as a result, the thrashing will increase more and more. Therefore, the global page
replacement algorithm is not suitable when thrashing happens.
2. Local Page Replacement
Unlike the global page replacement algorithm, local page replacement will select pages
which only belong to that process. So there is a chance to reduce the thrashing. But it is
proven that there are many disadvantages if we use local page replacement. Therefore,
local page replacement is just an alternative to global page replacement in a thrashing
scenario.
Deadlock
⚫ System Model
s
⚫ Deadlock Characteristics
⚫ Methods for handling
Deadlocks
⚫ Deadlock Prevention
⚫ Deadlock Avoidance
⚫ Deadlock Detection
⚫ Recovery from Deadlocks
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Chapter Objectives
⚫ To develop a description of deadlocks, which prevent sets
of concurrent processes from completing their tasks
⚫ To present a number of different methods for preventing
or avoiding deadlocks in a computer system
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
System
Model
⚫ System consists of resources
⚫ Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
⚫ Each resource type Ri has Wi instances.
⚫ Each process utilizes a resource as
follows:
⚫ request
⚫ use
⚫ release
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadloc
k
⚫ Deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by some
other process.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in
front of each other. A similar situation occurs in operating systems when there are
two or more processes that hold some resources and wait for resources held by
other(s). For example, in the below diagram, Process 1 is holding Resource 1 and
waiting for resource 2 which is acquired by process 2, and process 2 is waiting for
resource 1.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadlock
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadlock
Characterization
Deadlock can arise if four conditions hold simultaneously.
⚫ Mutual exclusion: only one process at a time can use a
resource
⚫ Hold and wait: a process holding at least one resource
is waiting to acquire additional resources held by other
processes
⚫ No preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task
⚫ Circular wait: there exists a set {P0, P1, …, Pn} of
waiting processes 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 that is held by Pn, and Pn is
waiting for a resource that is held by P0.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Allocation
Graph
A set of vertices V and a set of edges E.
⚫ V is partitioned into two types:
⚫ P = {P1, P2, …, Pn}, the set consisting of all
the processes in the system
⚫ R = {R1, R2, …, Rm}, the set consisting of all
resource types in the system
⚫ request edge – directed edge Pi Rj
⚫ assignment edge – directed edge Rj Pi
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Allocation Graph
(Components)
⚫ Process
Resource Type with 4
instances
⚫ Pi requests instance of
Pi
Rj
Rj
⚫ Pi is holding an instance of
Rj Pi
Rj
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Example of a Resource Allocation Graph
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource Allocation Graph With A
Deadlock
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Graph With A Cycle But No
Deadlock
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Basic
Facts
⚫ If graph contains no cycles no deadlock
⚫ If graph contains a cycle
⚫ if only one instance per resource type,
then deadlock
⚫ if several instances per resource type,
possibility of deadlock
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Methods for Handling
Deadlocks
⚫ Deadlock Prevention
⚫ Deadlock Avoidance
⚫ Deadlock Detection and Recovery
⚫ Deadlock Ignorance
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Methods for Handling Deadlocks
⚫ The first two methods are used to ensure the system never enters a deadlock.
⚫ Deadlock Prevention
⚫ This is done by restraining the ways a request can be made. Since deadlock
occurs when all the above four conditions are met, we try to prevent any one of
them, thus preventing a deadlock.
⚫ Deadlock Avoidance
⚫ When a process requests a resource, the deadlock avoidance algorithm examines
the resource-allocation state. If allocating that resource sends the system into an
unsafe state, the request is not granted.
⚫ Therefore, it requires additional information such as how many resources of each
type is required by a process. If the system enters into an unsafe state, it has to
take a step back to avoid deadlock.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Methods for Handling
Deadlocks
⚫Deadlock Detection and Recovery
⚫ We let the system fall into a deadlock and if it happens, we detect it using a
detection algorithm and try to recover.
⚫ Some ways of recovery are as follows.
⚫ Aborting all the deadlocked processes.
⚫ Abort one process at a time until the system recovers from the deadlock.
⚫ Resource Preemption: Resources are taken one by one from a process and assigned
to higher priority processes until the deadlock is resolved.
⚫ Deadlock Ignorance
⚫ In the method, the system assumes that deadlock never occurs. Since the problem of
deadlock situation is not frequent, some systems simply ignore it. Operating systems
such as UNIX and Windows follow this approach. However, if a deadlock occurs we
can reboot our system and the deadlock is resolved automatically.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadlock
Prevention
Restrain the ways request can be made
⚫Mutual Exclusion – not required for sharable resources (e.g., read-only
files); must hold for non-sharable resources
⚫ Hold and Wait – must guarantee that whenever a process requests a resource, it
does not hold any other resources
⚫ Require process to request and be allocated all its resources before it begins execution, or
allow process to request resources only when the process has none allocated to it.
⚫ Low resource utilization; starvation possible
⚫ Three approaches - Conservative approach, Do not hold, Wait Time out
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadlock Prevention (Cont.)
⚫ No Preemption –
⚫ If a process that is holding some resources requests
another resource that cannot be immediately allocated to
it, then all resources currently being held are released
⚫ Preempted resources are added to the list of resources for
which
the process is waiting
⚫ Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
⚫ Circular Wait – impose a total ordering of all resource
types, and require that each process requests resources in an
increasing order of enumeration
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadlock Avoidance
⚫ Requires that the system has some additional a prior
information available.
⚫ Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need.
⚫ The deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that there can never be a circular-wait
condition.
⚫ Resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Safe State
⚫ When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
⚫ System is in safe state if there exists a safe sequence of all
processes.
⚫ Sequence <P1, P2, …, Pn> is safe if for each Pi, the
resources that
Pi can still request can be satisfied by currently available
resources
+ resources held by all the Pj, with j<I.
⚫ If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished.
⚫ When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
23 on. Ms. Swati Jain, Assistant Professor, VSIT, VIPS
⚫ When Pi terminates, Pi+1 can obtain its needed resources, and so
Basic Facts
⚫ If a system is in safe state ⇒ no deadlocks.
⚫ If a system is in unsafe state ⇒ possibility of
deadlock.
⚫ Avoidance ⇒ ensure that a system will never
enter an unsafe state.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Safe, Unsafe , Deadlock
State
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Avoidance
Algorithms
⚫ Single instance of a resource type
⚫ Use a resource-allocation graph
⚫ Multiple instances of a resource
type
⚫ Use the banker’s algorithm
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Allocation Graph Scheme
⚫ Claim edge Pi Rj indicated that process Pj may
request resource Rj; represented by a dashed line
⚫ Claim edge converts to request edge when a process
requests a resource
⚫ Request edge converted to an assignment edge when the
resource is allocated to the process
⚫ When a resource is released by a process, assignment
edge reconverts to a claim edge
⚫ Resources must be claimed a priori in the system
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Allocation Graph
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Unsafe State In Resource-Allocation
Graph
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Allocation Graph Algorithm
⚫ Suppose that process Pi requests a resource Rj
⚫ The request can be granted only if converting the request
edge to an assignment edge does not result in the
formation of a cycle in the resource allocation graph
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Banker’s
Algorithm
⚫ Multiple instances
⚫ Each process must have a priori claim maximum use
⚫ When a process requests a resource it may have to wait
⚫ When a process gets all its resources it must return them
in a finite amount of time
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Data Structures for the Banker’s
Algorithm
Let n = number of processes, and m = number of resources types.
⚫ Available: Vector of length m. If available [j] = k, there are
k
instances of resource type Rj available
⚫ Max: n x m matrix. If Max [i,j] = k, then process Pi may
request at most k instances of resource type Rj
⚫ Allocation: n x m matrix. If Allocation[i,j] = k then
Pi is currently allocated k instances of Rj
⚫ Need: n x m matrix. If Need[i,j] = k, then Pi may need k
more instances of Rj to complete its task
Need [i,j] = Max[i,j] – Allocation [i,j]
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Safety Algorithm
1.Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
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 +
Allocationi Finish[i] = true
go to step 2
4.If Finish [i] == true for all
i, then the system is in a
safe state Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Request Algorithm for
Process Pi
Requesti = request vector for process Pi. If Requesti
[j]
= k then process Pi wants k instances of resource type
Rj
2. If Request i ≤ Available, go to step 3. Otherwise Pi
1. If Request i ≤ Needi go to step 2. Otherwise,
wait,
raise since condition,
error
must resources are notprocess has exceeded its
since
3.maximum
Pretendclaim
available to allocate requested resources to Pi by
the modifying
state as follows:
Available = Available – Requesti;
Allocationi = Allocationi +
Requesti; Needi = Needi – Requesti;
If safe the resources are allocated to Pi
If unsafe Pi must wait, and the old resource-allocation state
is restored
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Example of Banker’s Algorithm
⚫ 5 processes P0 through
P4; 3 resource types:
A (10 instances),
B (5instances), and
Allocation
C (7 instances) Max Available
⚫ Snapshot at timeATB0: C ABC ABC
P0 010 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Example (Cont.)
⚫ The content of the matrix Need is defined to be Max
–
Allocation
Need
AB
C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
⚫ The system is in a safe state since the sequence < P1, P3,
P4, P2, P0> satisfies safety criteria
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Example: P1 Request
(1,0,2)
⚫ Check that Request Available (that is, (1,0,2) (3,3,2)
true Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 302 600
P3 211 011
P4 002 431
⚫ Executing safety algorithm shows that sequence < P1, P3, P4,
P0, P2> satisfies safety requirement
⚫ Can request for (3,3,0) by P4 be granted?
⚫ Can request for (0,2,0) by P0 be granted?
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Deadlock Detection
⚫ Allow system to enter deadlock state
⚫ Detection algorithm in the following two ways:
⚫ If the resources have single Instance – Use Wait for Graph
⚫ If the resources have multiple instance – Banker’s
Algorithm
⚫ Recovery scheme
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Single Instance of Each
Resource Type
⚫ Maintain wait-for graph
⚫ Nodes are processes
⚫ Pi → Pj if Pi is waiting for Pj
⚫ Periodically invoke an algorithm that searches for a cycle
in the graph. If there is a cycle, there exists a deadlock
⚫ An algorithm to detect a cycle in a graph requires an
order of n2 operations, where n is the number of
vertices in the graph
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Resource-Allocation Graph and Wait-for
Graph
Resource-Allocation Graph Corresponding wait-for graph
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Several Instances of a Resource
Type
⚫ Available: A vector of length m indicates the number
of available resources of each type
⚫ Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each process
⚫ Request: An 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.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a)Work = Available
(b) For i = 1,2, …, n, if Allocationi ≠ 0, then
Finish[i] = false; otherwise, Finish[i] = true
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4
Finish[i]
3. Work = true
= Work + Allocationi
go to step 2
4. If Finish[i] == false, for some i, 1 i n, then the system is in
deadlock
state. Moreover, if Finish[i] == false, then Pi is deadlocked
Algorithm requires an order of O(m x n2) operations to detect
whether the system is in deadlocked state
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Example of
Detection
⚫ FiveAlgorithm
processes P through P ; three resource
0 4
types
A (7 instances), B (2 instances), and C (6
instances)
Allocation Request Available
⚫ Snapshot at timeATB0: C ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
⚫ Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
all
i
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Example (Cont.)
⚫ P2 requests an additional instance of type
C
Reques
P0 t0A0B0
P1 C202
P2 001
P3 100
P4 002
⚫ State of system?
⚫ Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests
⚫ Deadlock exists, consisting of processes P1, P2, P3, and P4
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Detection-
Algorithm
⚫ When, andUsage
how often, to invoke depends on:
⚫ How often a deadlock is likely to occur?
⚫ How many processes will need to be rolled back?
⚫ one for each disjoint cycle
⚫ If detection algorithm is invoked arbitrarily, there
may be many cycles in the resource graph and so
we would not be able to tell which of the many
deadlocked processes “caused” the deadlock.
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Recovery from Deadlock: Process
Termination
⚫ Abort all deadlocked processes
⚫ Abort one process at a time until the deadlock cycle is
eliminated
⚫ In which order should we choose to abort?
1. Priority of the process
2. How long process has computed, and how much longer
to completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
Ms. Swati Jain, Assistant Professor, VSIT, VIPS
Recovery from Deadlock:Resource
Preemption
⚫ Selecting a victim – minimize cost
⚫ Rollback – return to some safe state, restart process for that
state
⚫ Starvation – same process may always be picked as
victim, include number of rollback in cost factor
Ms. Swati Jain, Assistant Professor, VSIT, VIPS