You are on page 1of 92

Operating System

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

Response time – amount of time it takes from


when a request was submitted until the first
response is produced, not output (for time-
sharing environment)

Optimization Rule:
Max CPU utilization; Max throughput; Min
turnaround time; Min waiting time; Min
response time

11
Scheduling Algorithms

FIRST-COME, FIRST SERVED:


( FCFS) same as FIFO
Simple, fair, but poor performance. Average
queueing time may be long.
What are the average queueing and residence
times for this scenario?
How do average queueing and residence times
depend on ordering of these processes in the
queue?
12
EXAMPLE DATA:
Process Arrival
Service
Time
Time
1 0 8
2 1 4
3 2 9
FCFS
4 3 5
P1 P2 P3 P4

0 8 12 21 26

Average wait = ( (8-0) + (12-1) + (21-2) + (26-3) )/4 = 61/4 = 15.25

Residence Time
at the CPU 13
 Shortest-Job-First (SJF) Scheduling

Associate with each process the length of its next


CPU burst. Use these lengths to schedule the
process with the shortest time.
Two schemes:
nonpreemptive – once CPU given to the process
it cannot be preempted until it completes its CPU
burst.

14
 Shortest-Job-First (SJF) Scheduling

preemptive – if a new process arrives with


CPU burst length less than remaining time of
current executing process, preempt. This
scheme is known as the Shortest-
Remaining-Time-First (SRTF).

SJF is optimal – gives minimum average


waiting time for a given set of processes.

15
Scheduling Algorithms
SHORTEST JOB FIRST:

 Optimal for minimizing queueing time, but


impossible to implement. Tries to predict
the process to schedule based on
previous history.
 Predicting the time the process will use
on its next schedule:

16
t( n+1 ) = w * t( n ) + ( 1 - w ) * T( n )

Here: t(n+1) is time of next burst.


t(n) is time of current burst.
T(n) is average of all previous bursts .
W is a weighting factor emphasizing current
or previous bursts.

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)

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

Average waiting time = (9 + 1 + 0 + 2)/4 = 3


18
PRIORITY BASED SCHEDULING:

 Assign each process a priority. Schedule


highest priority first. All processes within same
priority are FCFS.

 Priority may be determined by user or by


some default mechanism. The system may
determine the priority based on memory
requirements, time limits, or other resource
usage.

19
PRIORITY BASED SCHEDULING:

 Starvation occurs if a low priority process


never runs. Solution: build aging into a
variable priority.

 Delicate balance between giving


favorable response for interactive jobs,
but not starving batch jobs.

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:

 Choosing a time quantum


– Too short - inordinate fraction of the
time is spent in context switches.
– Too long - reschedule latency is too
great. If many processes want the CPU,
then it's a long time before a particular
process can get the CPU. This then
acts like FCFS.
– Adjust so most processes won't use
their slice. As processors have become
faster, this is less of an issue.
23
CPU SCHEDULING Scheduling
Algorithms
EXAMPLE DATA:
Process Arrival Service Note:
Example violates rules for
Time quantum size since most
Time processes don’t finish in
1 0 one quantum.
8
2 1
4 Robin, quantum = 4, no priority-based preemption
Round
3 2
9
P1 P2 P3 P4 P1 P3 P4 P3
4 3
5
0 4 8 12 16 20 24 25 26

Average wait = ( (20-0) + (8-1) + (26-2) + (25-3) )/4 = 74/4 = 18.5

24
CPU SCHEDULING Scheduling
Algorithms
MULTI-LEVEL QUEUES:

 Each queue has its scheduling algorithm.


 Then some other algorithm (perhaps priority based) arbitrates between queues.
 Can use feedback to move between queues
 Method is complex but flexible.
 For example, could separate system processes, interactive, batch, favored,
unfavored processes

25
MULTI-LEVEL QUEUES:

26
CPU SCHEDULING Scheduling
Algorithms
MULTIPLE PROCESSOR SCHEDULING:

 Different rules for homogeneous or


heterogeneous processors.

 Load sharing in the distribution of work, such


that all processors have an equal amount to do.

 Each processor can schedule from a common


ready queue ( equal machines ) OR can use a
master slave arrangement.
27
MULTIPLE PROCESSOR
SCHEDULING:

28
Real Time Scheduling:
• Hard real-time systems – required to
complete a critical task within a guaranteed
amount of time.

• Soft real-time computing – requires that


critical processes receive priority over less
fortunate ones.

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?

• Deterministic modeling – takes a particular predetermined workload and


defines the performance of each algorithm for that workload.
• Queueing models.

32
The Deadlock Problem

• A set of blocked processes each holding a


resource and waiting to acquire a resource held
by another process in the set.
– Example 1
– System has 2 tape drives. P1 and P2
each hold one tape drive and each
needs the other one.

33
The Deadlock Problem

Example 2

Semaphores A and B each initialized to 1


P0 P1
wait(A) wait(B)
wait(B) wait(A)

34
Definitions Of Deadlock

• A process is deadlocked if it is waiting for an event


that will never occur.
Typically, more than one process will be
involved in a deadlock (the deadly embrace).
• A process is indefinitely postponed if it is delayed
repeatedly over a long period of time while the
attention of the system is given to other processes,
• i.e. the process is ready to proceed but never
gets the CPU.

35
Deadlock

Example - Bridge Crossing

– Assume traffic in one direction.


• Each section of the bridge is viewed as a resource.
– If a deadlock occurs, it can be resolved only if one
car backs up (preempt resources and rollback).
• Several cars may have to be backed up if a deadlock
occurs.
• Starvation is possible

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

• The following 4 conditions are necessary and


sufficient for deadlock (must hold
simultaneously)
– Mutual Exclusion:
• Only once process at a time can use the
resource.
- Hold and Wait:
Processes hold resources already
allocated to them while waiting for other
resources.
40
Conditions for Deadlock
No preemption:
Resources are released by processes
holding them only after that process has
completed its task.
Circular wait:
A circular chain of processes exists in
which each process waits for one or
more resources held by the next
process in the chain.

41
Resource Allocation Graph

• A set of vertices V and a set of edges E


• V is partitioned into 2 types
• P = {P1, P2,…,Pn} - the set of processes in
the system
• R = {R1, R2,…,Rn} - the set of resource
types in the system
• Two kinds of edges
• Request edge - Directed edge Pi ---> Rj
• Assignment edge - Directed edge Rj ----> Pi

42
Resource Allocation Graph

• Process

• Resource type with 4 instances

• 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

Graph with cycles and deadlock


R1 R2

P1 P2 P3

R3 R4

46
Methods for handling deadlocks

• Ensure that the system will never enter a


deadlock state.
• Allow the system to potentially enter a deadlock
state, detect it and then recover
• Ignore the problem and pretend that deadlocks
never occur in the system;
– Used by many operating systems, e.g. UNIX

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

After detection, clear the problem,


allow processes to complete and
resources to be reused. May
involve destroying and restarting
processes

49
Deadlock Prevention

– If any one of the conditions for deadlock (with


reusable resources) is denied, deadlock is
impossible.
– Restrain ways in which requests can be made
• Mutual Exclusion
– non-issue for sharable resources
– cannot deny this for non-sharable
resources (important)

50
Deadlock Prevention

Hold and Wait - guarantee that when a


process requests a resource, it does not
hold other resources.
Force each process to acquire all the
required resources at once. Process
cannot proceed until all resources have
been acquired.
Low resource utilization, starvation
possible

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.)

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.
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

 Set of resources, set of customers, banker


 Rules
 Each customer tells banker maximum
number of resources it needs.
 Customer borrows resources from
banker.
 Customer returns resources to banker.

 Customer eventually pays back loan.

 Banker only lends resources if the system will


be in a safe state after the loan. 54
Deadlock Avoidance

• Requires that the system has some additional a


priori 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.
55
Deadlock Avoidance

Resource allocation state is defined by the


number of available and allocated resources,
and the maximum demands of the processes.

56
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 Pj
with j<i.
57
Safe state

If Pi resource needs are not available, Pi


can wait until all Pj have finished.
When Pj is finished, Pi can obtain
needed resources, execute, return
allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its
needed resources...

58
Basic Facts

• If a system is in a safe state  no deadlocks.


• If a system is in unsafe state  possibility of
deadlock.
• Avoidance  ensure that a system will never
reach an unsafe state.

59
Resource Allocation Graph Algorithm

• Used for deadlock avoidance when there is only


one instance of each resource type.
– Claim edge: Pi  Rj indicates that
process Pi may request resource Rj;
represented by a dashed line.
– Claim edge converts to request edge
when a process requests a resource.
– When a resource is released by a
process, assignment edge reconverts to
claim edge.
60
Resource Allocation Graph Algorithm

Resources must be claimed a priori in


the system.
If request assignment does not result in the
formation of a cycle in the resource
allocation graph - safe state, else unsafe
state.

61
Claim Graph

Process claims resource

Process requests resource

Process is assigned resource

Process releases resource

62
Claim Graph

1 2

Possible Deadlock!!
3
5
4

63
Banker’s Algorithm

• Used for multiple instances of each resource


type.
• Each process must a priori claim maximum use
of each resource type.
• 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.

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

Need: n  m matrix. If Need[i,j] = k, then


process Pi may need k more instances of
resource type Rj to complete its task.
Need[i,j] = Max[i,j] - Allocation[i,j]

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

Work := Work + Allocation_i


Finish[i] := true
go to step 2
If Finish[i] = true for all i, then the system is
in a safe state

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

STEP 3: Pretend to allocate requested resources


to Pi by modifying the state as follows:
Available := Available - Request (i);
Allocation (i) := Allocation (i) + Request (i);
Need (i) := Need (i) - Request (i);
If safe  resources are allocated to Pi.
If unsafe  Pi must wait and the old resource-
allocation state is restored.

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

Allocation Max Available


A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
72
Example: P1 requests (1,0,2)

• Check to see that Request  Available


– ((1,0,2)  (3,3,2))  true.

Allocation Need Available


A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
73
Example (cont.)

• Executing the 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?

74
Deadlock Detection

• Allow system to enter deadlock state


• Detection Algorithm
• Recovery Scheme

75
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.
• An algorithm to detect a cycle in a graph
requires an order of n^2 operations, where n is
the number of vertices in the graph.

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

Request : An n  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 .

78
Deadlock Detection Algorithm

• Step 1: Let Work and Finish be vectors of


length m and n, respectively. Initialize
– Work := Available
– For i = 1,2,…,n, if Allocation(i)  0, then
Finish[i] := false, otherwise Finish[i] := true.
• Step 2: Find an index i such that both:
– Finish[i] = false
– Request (i)  Work
– If no such i exists, go to step 4.

79
Deadlock Detection Algorithm

– Step 3: Work := Work + Allocation(i)


• Finish[i] := true
• go to step 2
– Step 4: If Finish[i] = false for some i, 1  i  n,
then the system is in a deadlock state.
Moreover, if Finish[i] = false, then Pi is
deadlocked.
Algorithm requires an order of m  (n^2) operations
to detect whether the system is in a deadlocked
state.
80
Example of Detection Algorithm

• 5 processes - P0 - P4; 3 resource types -


A(7 instances), B(2 instances), C(6
instances)
• Snapshot at time T 0: <P0,P2,P3,P1,P4>
will result in Finish[i] = true for all i.

81
Example of Detection Algorithm

Allocation Max Available


A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
82
Example of Detection Algorithm

• P2 requests an additional instance of type


C.
• State of system
– Can reclaim resources held by process
P0, but insufficient resources to fulfill
other processes’ requests.
– Deadlock exists, consisting of P 1,P 2,P
3 and P 4.

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

– When, and 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
– How often --
• Every time a request for allocation cannot
be granted immediately

85
Detection-Algorithm Use

Allows us to detect set of deadlocked processes


and process that “caused” deadlock. Extra
overhead.
Every hour or whenever CPU utilization drops.

With arbitrary invocation there may be many cycles


in the resource graph and we would not be able to
tell which of the many deadlocked processes
“caused” the deadlock.

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

• Combine the three basic approaches


• Prevention
• Avoidance
• Detection
allowing the use of the optimal approach for each
class of resources in the system.
• Partition resources into hierarchically ordered
classes.
– Use most appropriate technique for handling
deadlocks within each class.
90
Scheduling –
SUMMARY
Objectives, concept, criteria, CPU and I/O burst cycle.
Types of Scheduling-Pre-emptive, Non pre- emptive.
Scheduling Algorithms.
First come first served (FCFS), Shortest job first
(SJF),Round Robin (RR), Priority.
Other Scheduling.
Multilevel, Multiprocessor, real-time.
Deadlock.
System model, principle necessary conditions, mutual
exclusion,critical region.
Deadlock handling.
Prevention, avoidance algorithm-Bankar’s algorithm,
Safty algorithm 91
References:
Websites
1. www.denett.com
2. www.tatamcgrawhill.com
3. www.phindia.com
4.www.wiley.com/college/silberschatz6e/
0471417432/slides/ppt
5. www.en.wikipedia.org
6. www.computerworld.com
7. www.computer.howstuffworks.com
8. www.willamstallings.com/os4e.html
9. www.deitel.com/books/os3e/slides.html

92

You might also like