0% found this document useful (0 votes)
11 views40 pages

Unit 4 OS

......

Uploaded by

Omkar Rakhonde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

Unit 4 OS

......

Uploaded by

Omkar Rakhonde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Government Polytechnic Jintur

Department of Computer Engg

Course Name: Operating System (OSY)


Course Code : 22516

M.A. Zahed
Lecturer
Unit IV: Scheduling
CPU Scheduling

⚫Basic Concepts
⚫Scheduling Criteria
⚫Scheduling Algorithms
Objectives

⚫ To introduce CPU scheduling, which


is the basis for multiprogrammed
operating systems
⚫ To describe various CPU-scheduling
algorithms
⚫ To discuss evaluation criteria for
selecting a CPU-scheduling
algorithm for a particular system
Basic Concepts

⚫ Maximum CPU
utilization obtained
with
multiprogramming
⚫ CPU–I/O Burst Cycle –
Process execution
consists of a cycle of
CPU execution and
I/O wait
⚫ CPU burst followed
by I/O burst
⚫ CPU burst distribution
is of main concern
CPU Scheduler
 Short-term scheduler selects from
among the processes in ready queue, and
allocates the CPU to one of them
 Queue may be ordered in various ways
 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
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
Scheduling Criteria

⚫ CPU utilization – keep the CPU as busy


as possible
⚫ Throughput – number 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
⚫ 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)
Scheduling Algorithm Optimization
Criteria

⚫Max CPU utilization


⚫Max throughput
⚫Min turnaround time
⚫Min waiting time
⚫Min response time
First- Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
⚫ Suppose that the processes arrive in the order:
P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30

⚫ Waiting time for P1 = 0; P2 = 24; P3 = 27


⚫ Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
⚫ The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

⚫ Waiting time for P1 = 6; P2 = 0; P3 = 3


⚫ Average waiting time: (6 + 0 + 3)/3 = 3
⚫ Much better than previous case
⚫ Convoy effect - short process behind long process
◦ Consider one CPU-bound and many I/O-bound processes
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
⚫SJF is optimal – gives minimum
average waiting time for a given
set of processes
◦ The difficulty is knowing the length
of the next CPU request
◦ Could ask the user
Example of SJF

ProcessArriva l Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

⚫ SJF scheduling chart


P4 P1 P3 P2
0 3 9 16 24

⚫ Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


Example of Shortest-remaining-time-first

⚫ Now we add the concepts of varying arrival times and


preemption to the analysis
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
⚫ Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

⚫ Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4


= 26/4 = 6.5 msec
Priority Scheduling

⚫ A priority number (integer) is associated with each


process
⚫ The CPU is allocated to the process with the highest
priority (smallest integer  highest priority)
◦ Preemptive
◦ Nonpreemptive

⚫ SJF is priority scheduling where priority is the


inverse of predicted next CPU burst time
⚫ Problem  Starvation – low priority processes may
never execute
⚫ Solution  Aging – as time progresses increase the
priority of the process
Example of Priority Scheduling

ProcessA arri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

⚫ Priority scheduling Gantt Chart

⚫ Average waiting time = 8.2 msec


Round Robin (RR)

⚫ Each process gets a small unit of CPU time (time


quantum q), usually 10-100 milliseconds. After
this time has elapsed, the process is preempted and
added to the end of the ready queue.
⚫ If there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of
the CPU time in chunks of at most q time units at
once. No process waits more than (n-1)q time units.
⚫ Timer interrupts every quantum to schedule next
process
⚫ Performance
◦ q large  FIFO
◦ q small  q must be large with respect to context
switch, otherwise overhead is too high
Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 3
P3 3
⚫ The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

⚫ Typically, higher average turnaround than SJF, but


better response
⚫ q should be large compared to context switch time
⚫ q usually 10ms to 100ms, context switch < 10 usec
Deadlock
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
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 {P , P , …, P } of waiting0 1 n
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.

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.30
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

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.31
Resource-Allocation Graph
(Cont.)
● Process

● Resource Type with 4 instances

● Pi requests instance of Rj
P
i
Rj
● Pi is holding an instance of Rj

P
i
Rj

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.32
Example of a Resource Allocation
Graph

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.33
Resource Allocation Graph With A
Deadlock

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.34
Graph With A Cycle But No
Deadlock

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.35
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

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.36
Methods for Handling
Deadlocks
● Deadlock ignorance (Ostrich’s approach)(used by most
operating systems, including UNIX)
● Deadlock prevention
● Deadlock avoidance
● Deadlock detection and recovery

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.37
Deadlock
Prevention

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

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.38
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
● Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing
order of enumeration

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.39
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
● Resource-allocation state is defined by the number of available
and allocated resources, and the maximum demands of the
processes

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.40
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 sequence <P1, P2, …, Pn> of ALL
the processes in the systems such that 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
● That is:
● 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
● When Pi terminates, Pi +1 can obtain its needed resources, and so on

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.41
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.

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.42
Safe, Unsafe, Deadlock
State

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.43
Avoidance
Algorithms
● Single instance of a resource type
● Use a resource-allocation graph

● Multiple instances of a resource type


● Use the banker’s algorithm

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.44
Deadlock Detection &
Recovery
● Allow system to enter deadlock state

● Detection algorithm

● Recovery scheme

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.52
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?

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.53
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

Silberschatz, Galvin and Operating System Concepts – 9th


Gagne ©2013 Edition 7.54
Thank You..!!!

You might also like