Professional Documents
Culture Documents
Resources
Examples of computer resources
Printers
Tape drives
Tables
Preemptable resources
Can be taken away from a process with no ill
effects
Nonpreemptable resources
Will cause the process to fail if taken away
Reusable resources
Used by one process at a time and not
depleted by that use
Examples: Processors, I/O channels, main and
secondary memory, files, databases, and
semaphores
Shared and exclusive resources
Example of shared resource: FILE
Example of exclusive resource: PRINTER
Consumable resources
Created (produced) and destroyed (consumed)
by a process
Examples: Interrupts, signals, messages, and
information in I/O buffers
7.1
System Model
A system consists of a number of resources to be
distributed among a number of competing
processes.
There are different types of resources R1, R2,...,
Rm.
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances. For
example, if two CPUs then resource type CPU has
two instances.
Sequence of Events Required to Use a Resource
Each process utilizes a resource as follows:
Request a resource:
Request is made through a system call
Process must wait if request is denied
Requesting process may be blocked
may fail with error code
Use the resource:
The process can operate on the resource.
Release the resource:
The process releases the resource. A
resource is released through a system call.
7.2
Deadlock
Formal Definition
A set of processes is deadlocked if each
process in the set is waiting for an event that
only another process in the set can cause
Usually the event is release of a currently held
resource
None of the processes can
Run
Release resources
Be awakened
Involve conflicting needs for resources by two or
more processes
Examples of Deadlock
Example 1
7.3
Example 3
Space is available for allocation of 200K bytes,
and the following sequence of events occur
P0
P1
.
Request 80KB;
Request 60KB;
Request 70KB;
Request 80KB;
arise
if
four
conditions
hold
7.4
Resource-Allocation Graph
Deadlocks can be described more precisely in terms
of a directed graph, called a system resourceallocation graph.
This graph consists of 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.
7.5
Request
edge
directed edge P1 Rj
Assignment
edge
directed edge Rj Pi
Different symbols are used to represent processes
and resources as given below:
Process:
Resource type of 4 instances:
Pi requests instance of Rj:
Pi
Rj
Pi is holding an instance of Rj
:
Pi
Rj
7.6
7.7
7.8
7.9
7.10
Circular Wait
Order resources (each resource type is
assigned a unique integer) and allow process to
request for them only in increasing order
If a process needs several instances of the
same resource, it should issue a single request
for all of them.
Alternatively, we can require that whenever a
process requests an instance of a resource
type it has released all the resources which are
assigned a smaller inter value.
Problem:
Adding a new resource that upsets
ordering requires all code ever written to
be modified
Resource numbering affects efficiency
A process may have to request a
resource well before it needs it, just
because of the requirement that it
must request resources in ascending
order
An example:
7.11
Deadlock Avoidance
OS never allocates resources in a way that could
lead to a deadlock
Processes must tell OS in advance how many
resources they will request
Some Definitions
State of a system
An enumeration of which processes hold, are
waiting for or might request which resource
Safe state
1. No process is deadlocked, and there exits no
possible sequence of future request in which
deadlock could occur
2. No process is deadlocked and the current state
will not lead to a dead lock state
3. Safe state is where there is at least one
sequence that does not result in deadlock
Prepared by Dr. Amjad Mahmood
7.12
Unsafe state
Is a state that is not safe
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
7.13
7.14
7.15
Bankers Algorithm
Applicable to system with multiple instances of
resource types.
Each process must 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.
Bankers algorithm runs each time:
A process requests resource Is it sage?
A process terminates Can I allocate released
resources to a suspended process waiting for
them?
A new state is safe if and only if every process
can complete after allocation is made
Make allocation and then check system
state and deallocate if unsafe
7.16
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively.
Initialize: Work = Available
Finish [i]=false for i=1,3, , n.
2. Find and 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.
7.17
P0
P1
P2
P3
P4
Allocation
ABC
010
200
302
211
002
Max
ABC
753
322
902
222
433
Available
ABC
332
Need
ABC
743
122
600
011
431
7.18
Allocatio
n
ABC
010
302
301
211
002
Need
Available
ABC
743
020
600
011
431
ABC
230
7.19
7.20
Examples:
7.21
7.22