Professional Documents
Culture Documents
Chapter 4 Scheduling
(12178)
Computer Department
1
Scheduling
4.1 Scheduling –
4.2 Types of
4.3 Scheduling Algorithms.
4.4 Other Scheduling.
4.5 Deadlock.
4.6 Deadlock handling.
2
Scheduling Objectives
Enforcement of fairness in allocating resources to
processes
Enforcement of priorities
Make best use of available system resources
Give preference to processes holding key resources.
Give preference to processes exhibiting good
behavior.
Degrade gracefully under heavy loads.
3
Scheduling Concepts
Multiprogramming A number of
programs can be in memory at the
same time. Allows overlap of CPU and
I/O.
Jobs (batch) are programs that run without
user interaction.
User (time shared) are programs that may
have user interaction.
Process is the common name for both.
4
CPU - I/O burst cycle Characterizes
process execution, which
alternates, between CPU and I/O
activity. CPU times are generally
much shorter than I/O times.
Preemptive Scheduling An interrupt causes
currently running process to give up
the CPU and be replaced by another
process
5
CPU - I/O burst cycle Characterizes
6
The Scheduler
Selects from among the processes in memory
that are ready to execute, and allocates the CPU
to one of them.
CPU scheduling decisions may take place when
a process:
1. switches from running to waiting state.
2. switches from running to ready state.
3. switches from waiting to ready.
4. terminates.
7
The Scheduler
When scheduling takes place only under
conditions 1 and 4 we say the scheduling scheme
is nonpreemptive.
All other scheduling is preemptive
8
The Scheduler
Dispatcher
Dispatcher module gives control of the CPU to
the process selected by the short-term scheduler;
this involves:
switching context
switching to user mode
jumping to the proper location in the user
program to restart that program
Dispatch latency – time it takes for the dispatcher
to stop one process and start another running.
9
The Scheduler
Scheduling Criteria
CPU utilization – keep the CPU as busy as
possible
Throughput – # of processes that complete
their execution per time unit
Turnaround time – amount of time to execute
a particular process
Waiting time – amount of time a process has
been waiting in the ready queue
10
The Scheduler
Optimization Rule:
Max CPU utilization; Max throughput; Min
turnaround time; Min waiting time; Min
response time
11
Scheduling Algorithms
0 8 12 21 26
Residence Time
at the CPU 13
Shortest-Job-First (SJF) Scheduling
14
Shortest-Job-First (SJF) Scheduling
15
Scheduling Algorithms
SHORTEST JOB FIRST:
16
t( n+1 ) = w * t( n ) + ( 1 - w ) * T( n )
17
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
1.Example:
oSJF (non-preemptive)
19
PRIORITY BASED SCHEDULING:
20
ROUND ROBIN:
Use a timer to cause an interrupt after a
predetermined time. Preempts if task exceeds
it’s quantum.
Train of events
Dispatch
Time slice occurs OR process suspends
on event
Put process on some queue and dispatch
next
Use numbers in last example to find queueing
and residence times. (Use quantum = 4 sec.)
21
ROUND ROBIN
Definitions:
– Context Switch Changing the processor
from running one task (or process) to
another. Implies changing memory.
– Processor Sharing Use of a small
quantum such that each process runs
frequently at speed 1/n.
– Reschedule latency How long it takes
from when a process requests to run,
until it finally gets control of the CPU.
22
ROUND ROBIN:
24
CPU SCHEDULING Scheduling
Algorithms
MULTI-LEVEL QUEUES:
25
MULTI-LEVEL QUEUES:
26
CPU SCHEDULING Scheduling
Algorithms
MULTIPLE PROCESSOR SCHEDULING:
28
Real Time Scheduling:
• Hard real-time systems – required to
complete a critical task within a guaranteed
amount of time.
29
CPU SCHEDULING
Two algorithms: time-sharing and real-time
• Time-sharing
– Prioritized credit-based – process with most
credits is scheduled next
– Credit subtracted when timer interrupt occurs
– When credit = 0, another process chosen
– When all processes have credit = 0, recrediting
occurs
• Based on factors including priority and history
30
CPU SCHEDULING
Real-time
Soft real-time
Posix.1b compliant – two classes
FCFS and RR
Highest priority process runs first
31
CPU SCHEDULING
How do we decide which algorithm is best for a particular environment?
32
The Deadlock Problem
33
The Deadlock Problem
Example 2
34
Definitions Of Deadlock
35
Deadlock
36
Resources
• Resource
• commodity required by a process to
execute
• Resources can be of several types
• Serially Reusable Resources
– CPU cycles, memory space, I/O devices,
files
– acquire -> use -> release
37
Resources
Consumable Resources
Produced by a process, needed by a
process - e.g. Messages, buffers of
information, interrupts
create ->acquire ->use
Resource ceases to exist after it has
been used
38
System Model
• Resource types
– R1, R2,….Rm
• Each resource type Ri has Wi instances
• Assume serially reusable resources
• request -> use -> release
39
Conditions for Deadlock
41
Resource Allocation Graph
42
Resource Allocation Graph
• Process
• Pi requests instance of Rj
• Pi is holding an instance of Rj
43
Resource Allocation Graph
Graph with no cycles
R1 R2
P1 P2 P3
R3 R4
44
Resource Allocation Graph
Graph with cycles
R1 P2
P1 P3
R2 P4
45
Resource Allocation Graph
P1 P2 P3
R3 R4
46
Methods for handling deadlocks
47
Deadlock Management
– Prevention
– Design the system in such a way that deadlocks
can never occur
– Avoidance
– Impose less stringent conditions than for
prevention, allowing the possibility of deadlock
but sidestepping it as it occurs.
– Detection
– Allow possibility of deadlock, determine if
deadlock has occurred and which processes and
resources are involved.
48
Deadlock Management
Recovery
49
Deadlock Prevention
50
Deadlock Prevention
51
Deadlock Prevention (cont.)
• No Preemption
– If a process that is holding some
resources requests another resource
that cannot be immediately allocated to
it, the process releases the resources
currently being held.
– Preempted resources are added to the
list of resources for which the process is
waiting.
52
Deadlock Prevention (cont.)
Circular Wait
Impose a total ordering of all resource
types.
Require that processes request
resources in increasing order of
enumeration; if a resource of type N is
held, process can only request
resources of types > N.
53
Deadlock Avoidance
56
Safe state
58
Basic Facts
59
Resource Allocation Graph Algorithm
61
Claim Graph
62
Claim Graph
1 2
Possible Deadlock!!
3
5
4
63
Banker’s Algorithm
64
Data Structures for the Banker’s
Algorithm
• Let n = number of processes and m = number of
resource types.
– Available: Vector of length m. If
Available[j] = k, there are k instances of
resource type Rj available.
– Max: n m matrix. If Max[i,j] = k, then
process Pi may request at most k
instances of resource type Rj.
– Allocation: n m matrix. If Allocation[i,j] =
k, then process Pi is currently allocated k
instances of resource type Rj.
65
Data Structures for the Banker’s
Algorithm
66
Safety Algorithm
• Let Work and Finish be vectors of length m
and n, respectively. Initialize
– Work := Available
– Finish[i] := false for i = 1,2,…,n.
• Find an i (i.e. process Pi) such that both:
– Finish[i] = false
– Need_i <= Work
– If no such i exists, go to step 4.
67
Safety Algorithm
68
Resource-Request Algorithm for Process
Pi
• Request_i = request vector for process Pi.
If Request_i[j] = k, then process Pi wants k
instances of resource type Rj.
– STEP 1: If Request(i) Need(i), go to
step 2. Otherwise, raise error condition,
since process has exceeded its
maximum claim.
– STEP 2: If Request(i) Available, go to
step 3. Otherwise, Pi must wait since
resources are not available.
69
Resource-Request Algorithm for Process Pi
70
Example of Banker’s Algorithm
• 5 processes
– P0 - P4;
• 3 resource types
– A(10 instances), B (5 instances), C (7
instances)
• Snapshot at time T0
71
Example of Banker’s Algorithm
74
Deadlock Detection
75
Single Instance of each resource type
76
Several instances of a resource type
• Data Structures
– Available: Vector of length m. If
Available[j] = k, there are k instances of
resource type Rj available.
– Allocation: n m matrix. If Allocation[i,j]
= k, then process Pi is currently
allocated k instances of resource type
Rj.
77
Several instances of a resource type
78
Deadlock Detection Algorithm
79
Deadlock Detection Algorithm
81
Example of Detection Algorithm
83
Example of Detection Algorithm
Request
A B C
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
84
Detection-Algorithm Use
85
Detection-Algorithm Use
86
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?
– Priority of the process
– How long the process has computed,
and how much longer to completion.
– Resources the process has used.
– Resources process needs to complete.
87
Recovery from Deadlock: Process
Termination
– How many processes will need to be
terminated.
– Is process interactive or batch?
– Resources the process has used.
– Resources process needs to complete.
– How many processes will need to be
terminated.
– Is process interactive or batch?
88
Recovery from Deadlock: Resource
Preemption
• Selecting a victim - minimize cost.
• Rollback
• return to some safe state, restart process
from that state.
• Starvation
• same process may always be picked as
victim; include number of rollback in cost
factor.
89
Combined approach to deadlock handling
92