0% found this document useful (0 votes)
11 views

Module 3

OS module 3

Uploaded by

Lalli Krishnan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 3

OS module 3

Uploaded by

Lalli Krishnan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

MODULE 3

Deadlocks and
Memory Management
Syllabus
Deadlocks: Definition, Deadlock characteristics, Deadlock Prevention, Deadlock
Avoidance: banker’s algorithm, Deadlock detection and Recovery.

Memory Management: Basic Memory Management: Definition, Logical and Physical


address map, Memory allocation: Contiguous Memory allocation – Fixed and variable
partition – Internal and External fragmentation and Compaction, Paging: Principle of
operation – Page allocation – Hardware support for paging –, Protection and sharing –
Disadvantages of paging.
Deadlocks
Deadlocks
• Deadlock is a situation in computing where two or more processes are unable to proceed
because each is waiting for the other to release resources.

• Each process utilizes a resource as follows:


– request
– Use
– release

Diagramatic representation for Deadlock


Examples of deadlock
1.The system has 2 tape drives. P0 and P1 each hold one tape drive and each needs another
one.

2. Assume the space is available for allocation of 200K bytes, and the following sequence of
events occurs.

P0 p1

Request 80KB; Request 70KB;

Request 60KB; Request 80KB;

Deadlock occurs if both processes progress to their second


request.
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.
Methods For Handling Deadlock
There are three ways to handle deadlock
• Deadlock Prevention or Avoidance
- Ensure that the system will never enter a deadlock state
• Deadlock Recovery
- Allow the system to enter a deadlock state and then
recover
• Deadlock Ignorance
- Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
including UNIX
Deadlock Prevention
• Mutual Exclusion – not required for sharable resources; must hold for nonsharable
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 Low resource utilization; starvation possible.

• No Preemption : Preempt resources from the process when resources are required by
other high-priority processes.

• Circular Wait: To violate circular wait, we can assign a priority number to each of the
resource. A process can't request for a lesser priority resource. This ensures that not a
single process can request a resource which is being utilized by some other process and
no cycle will be formed.
Deadlock Detection and Recovery
• Another approach to dealing with deadlocks is to
detect and recover from them when they occur.
• This can involve killing one or more of the processes
involved in the deadlock or releasing some of the
resources they hold.
Deadlock Detection
• Deadlock detection is a process in computing where the
system checks if there are any sets of processes that are stuck
waiting for each other indefinitely, preventing them from
moving forward.
• Deadlock detection is the process of finding out whether any
process are stuck in loop or not. There are several algorithms
like
Resource Allocation Graph
Banker’s Algorithm
Resource Allocation Graph

• The resource allocation graph (RAG) is used to visualize the system’s


current state as a graph.
• The Graph includes all processes, the resources that are assigned to them,
as well as the resources that each Process requests.
• Sometimes, if there are fewer processes, we can quickly spot a deadlock in
the system by looking at the graph rather than the tables we use in
Banker’s algorithm. Deadlock avoidance can also be done with Banker’s
Algorithm.
Banker's Algorithm

• Banker's algorithm is a deadlock avoidance algorithm.


• It is named so because this algorithm is used in banking systems to determine
whether a loan can be granted or not.
• Consider there are n account holders in a bank and the sum of the money in all
of their accounts is S.
• Every time a loan has to be granted by the bank, it subtracts the loan
amount from the total money the bank has.
• Then it checks if that difference is greater than S.
• It is done because, only then, the bank would have enough money even if all
the n account holders draw all their money at once.
• Banker's algorithm works in a similar way in computers.
• Whenever a new process is created, it must specify the maximum instances of
each resource type that it needs, exactly.
Implementation of the Banker’s
Algorithm
• Banker’s algorithm comprises of two algorithms:
1. Safety algorithm
2. Resource request algorithm
Safety Algorithm
• A safety algorithm is an algorithm used to find whether or not a system is
in its safe state. The algorithm is as follows:
• Step 1:
Work = Available
Finish[i] =false for i = 0, 1, ... , n - 1.
• Step 2:
Finish[i] =false
Need(i) <= Work
• Step 3:
Work = Work + Allocation(i)
Finish[i] = true
• Step 4:
if Finish[i] == true for all i, then the system is in a safe state
Resource request algorithm

1) If Requesti <= Needi


Goto step (2) ; otherwise, raise an error condition, since the
process has exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are not
available.
3) Have the system pretend to have allocated the requested
resources to process Pi by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
Example 1
Considering a system with five processes P 0 through P4 and three resources of type A, B,
C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances.
Suppose at time t0 following snapshot of the system has been taken:

Q.1: What will be the content of the Need matrix?


Q.2: Is the system in a safe state? If Yes, then what is the safe sequence?
Q.3: What will happen if process P1 requests one additional instance of resource type A
and two instances of resource type C?
Q.1: What will be the content of the Need matrix?

Need [i, j] = Max [i, j] – Allocation [i, j]


Q.2: Is the system in a safe state? If Yes, then what is
the safe sequence?
Example 2

Q1. Calculate the content of the need matrix?


Q2. Check if the system is in a safe state?
Q3. Determine the total sum of each type of resource?
Memory Management
Memory management
• Memory management is the functionality of an operating system which handles or
manages primary memory and moves processes back and forth between main memory
and disk during execution.
• The task of subdividing the memory among different processes is called Memory
Management.

Role of Memory management


• Memory manager is used to keep track of the status of memory locations, whether it is
free or allocated.
• Memory manager permits computers with a small amount of main memory to execute
programs larger than the size or amount of available memory. It does this by moving
information back and forth between primary memory and secondary memory by using
the concept of swapping.
• The memory manager is responsible for protecting the memory allocated to each process
from being corrupted by another process.
• Memory managers should enable sharing of memory space between processes. Thus,
two programs can reside at the same memory location although at different times.
Logical and Physical Address Space

• Logical Address Space: An address generated by the CPU is


known as a “Logical Address”. It is also known as a
Virtual address. Logical address space can be defined as the size
of the process. A logical address can be changed.

• Physical Address Space: An address seen by the memory unit (i.e


the one loaded into the memory address register of the memory) is
commonly known as a “Physical Address”. A Physical address is
also known as a Real address. The set of all physical addresses
corresponding to these logical addresses is known as Physical
address space. A physical address is computed by MMU. The run-
time mapping from virtual to physical addresses is done by a
hardware device Memory Management Unit(MMU). The physical
address always remains constant.
Memory Management Techniques
The memory management techniques can be classified into following main
categories:
• Contiguous memory management schemes
• Non-Contiguous memory management schemes

You might also like