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

LECTURE 4 Scheduling

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

LECTURE 4 Scheduling

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

CPU SCHEDULING

P.Ray@CSE
Introduction
Scheduling refers to the set of policies and
mechanisms that an OS supports for determining
the order of execution of the pending jobs and
processes.
A scheduler is an operating system program
(module) that selects the next job to be admitted
for execution.
Note that the main objective of scheduling is to
increase CPU utilization and to increase the
throughput.
Throughput means the amount of work
accomplished in a given time interval.
P.Ray@CSE
Also note that the behavior of a process plays
a key role in CPU Scheduling.
This is depicted by CPU - I/O burst cycle.
A process will normally run for a while
(known as a CPU burst), perform some I/O,
(known as I/O burst) then run for a while
more (the next CPU burst) and so on.

P.Ray@CSE
The duration of interval between the CPU
burst and I/O burst depends upon the
following:
(a) If the process is I/O bound:
A process is I/O bound process if it performs lot of
I/O operations as compared to other computation
operations. Each I/O operation is followed by a
short CPU burst to process the I/O.
(b) If the process is CPU bound:
A process is CPU bound if it performs lot of
computation and has very less I/O operations to
do. There will be long CPU bursts with I/O bursts
of very short duration.P.Ray@CSE
P.Ray@CSE
As shown in Fig. the process execution
consists of a cycle of CPU execution and l/O
wait.
A process alternates between these two
states.
Process execution begins with CPU burst
followed by an l/O burst, the last CPU burst
will end with a system request to terminate
the execution.
The duration of each burst depends upon
process to process.

P.Ray@CSE
SCHEDULERS
The objective of process scheduling is to
assign processes to the processor in such
a way that the processor utilization and
the throughput is maximized.
The job of scheduling is done by a
software routine called scheduler.
There are three different types of
schedulers to schedule processes at
various levels; long, middle and short
term scheduler. P.Ray@CSE
P.Ray@CSE
Long- Term Scheduler
The long-term scheduler also known as job
scheduler decides whether a new process can
be added to the ready queue now.
It does this by monitoring the degree of
multiprogramming, if it is high now, a new
process can be prevented to enter into the
ready queue.
If a good selection is made by the long-term
scheduler in choosing new jobs to join the ready
queue, then the average rate of process creation
must almost equal the average rate of processes
leaving the system.
P.Ray@CSE
The long-term scheduler decides
whether a new process can be
added to the ready queue now
by monitoring degree of
multiprogramming.

P.Ray@CSE
Medium- Term Scheduler
Medium-term scheduling is a part of the
swapping function.
When system throughput falls below a
threshold, some of the ready processes are
swapped out of memory to reduce the degree of
multiprogramming.
Sometime later, these swapped processes
are reintroduced into memory to join the ready
queue.
The medium-term scheduler handles such
situations .
P.Ray@CSE
Short term Scheduler
The short-term scheduler or CPU
scheduler selects a process from ready
queue among the ready processes to
execute on the CPU.
CPU switching from one process to
another requires saving the state of the
current process and loading the latest
state of the next process. This is known
as a context switch.
P.Ray@CSE
Time that is required for a context switch
is a clear overhead since the system at
that instant of time is not doing any
useful work.
Context switch time varies from machine
to machine depending on speed, the
amount of saving and loading to be done,
hardware support and so on.

P.Ray@CSE
The short term scheduler selects a
process from ready queue among
the ready processes to execute on
the CPU according to scheduling
algorithm employed on the
system.

P.Ray@CSE
P.Ray@CSE
P.Ray@CSE
P.Ray@CSE
Previous figure relates the scheduling functions to the
process state transition diagram (first shown in Figure
3.9b).
Long-term scheduling is performed when a new
process is created.
This is a decision whether to add a new process to the set of
processes that are currently active.
Medium-term scheduling is a part of the swapping
function.
This is a decision whether to add a process to those that are
at least partially in main memory and therefore available for
execution.
Short-term scheduling is the actual decision of which
ready process to execute next.
P.Ray@CSE
P.Ray@CSE
P.Ray@CSE
Dispatcher
The short term scheduler just select the process
which can be assigned the next process.
The actual job of context switching i.e. storing
the states of previous process and assigning the
next processor to CPU is done by the Dispatcher.
The time taken by the dispatcher to stop one
process and start another running is known
Dispatch latency.

P.Ray@CSE
Priorities
Scheduler will always choose a process of
higher priority over one of lower priority.
Have multiple ready queues to represent
each level of priority.
Lower-priority may suffer starvation.
Allow a process to change its priority
based on its age or execution history.

P.Ray@CSE
P.Ray@CSE
Scheduling Criteria
 CPU utilization – keep the CPU as busy as possible.
 Throughput – of processes that complete their
execution per time unit. It means how many jobs are
completed by the CPU within a time period.
 Turnaround time – amount of time to execute a
particular process (finished time – arrival time)
 Waiting time – amount of time a process has been
waiting in the ready queue sum of ( starting time –
arrival time).
 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)
( first response –arrival time)
P.Ray@CSE
Optimization Criteria
Max CPU utilization
Max throughput (no of jobs per unit time)
Min turnaround time (Finish time – Arrival
time)
Min waiting time (Staring Time – Arrival
Time)
Min response time (First Response – Arrival
Time)
P.Ray@CSE
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P1 24
Min turnaround time (Finish time – Arrival time)
P2 3 Min waiting time (Staring Time – Arrival Time)
Min response time (First Response – Arrival Time)
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-0); P3 = (27-0)
• Average waiting time: (0 + 24 + 27)/3 = 17
• Turn around time for P1 = (24-0); P2 = (27-0); P3 = (30-0)
• Average Turn around time = (24+27+30)/3 = 27
• Response time for P1 = 0; P2 = (24-0); P3 = (27-0)
• Average Response Time: (0+24+27)/3 = 17
P.Ray@CSE
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.

P.Ray@CSE
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 completes its CPU burst.
– preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).
SJF is optimal – gives minimum average waiting
time for a given set of processes.
P.Ray@CSE
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16
•Average waiting time = (0 + (8-2) + (7-3) + (12-5))/4 = 4

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

P.Ray@CSE
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

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

P.Ray@CSE
• Advantages of SJF
– Maximum throughput
– Minimum average waiting and turnaround time
• Disadvantages of SJF
– May suffer with the problem of starvation
– It is not implementable because the exact Burst
time for a process can't be known in advance.
• There are different techniques available by
which, the CPU burst time of the process can
be determined.
P.Ray@CSE
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
Non-preemptive
 SJF is a priority scheduling where priority is the predicted next
CPU burst -time.
 Problem  Starvation  low priority processes may never
execute.
 Solution  Ageing  as time progresses increase the priority of
the process.

P.Ray@CSE
Non-Preemptive
Process Arrival Burst Priority
Time Time
A 0 5 3
B 0 1 7
C 0 2 5
D 0 5 2
E 0 5 1

• GANTT Chart

E D A C B
P.Ray@CSE
Preemptive
Process Arrival Burst Priority
Time Time
A 1 5 3
B 2 1 7
C 3 2 5
D 4 5 2
E 5 5 1
• Prepare the GANTT chart.
A D E D A C B

1 4 5 10 14 16 18 19

P.Ray@CSE
Round Robin (RR)
 Each process gets a small unit of CPU time (time quantum),
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.
 Performance
q large  FIFO
q small  q must be large with respect to context switch,
otherwise overhead is too high.

P.Ray@CSE
In software, we need also to consider the effect of
context switching on the performance of RR scheduling.
Let us assume that we have only one process of 10 time
units.
If the quantum is 12 time units, the process finishes in
less than 1 time quantum, with no overhead.
If the quantum is 6 time units, however, the process
requires 2 quanta, resulting in one context switch.
If the time quantum is 1 time unit, then nine context
switches will occur, slowing the execution of the
process accordingly (next Figure).

P.Ray@CSE
Time Quantum and Context Switch Time

P.Ray@CSE
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
Min turnaround time (Finish time – Arrival time)
P4 24 Min waiting time (Staring Time – Arrival Time)
Min response time (First Response – Arrival Time)

 The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162


Typically, higher average turnaround than SJF, but
better response.

P.Ray@CSE
Multilevel Queue
 Ready queue is partitioned into separate queues:
 foreground (interactive)
 background (batch)
 Each queue has same type of jobs, and its own scheduling
algorithm,
 foreground – RR
 background – FCFS
 Scheduling must be done between the queues.
 Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
 Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
 20% to background in FCFS
P.Ray@CSE
Multilevel Queue Scheduling

P.Ray@CSE
Normally, when the multilevel queue
scheduling algorithm is used, processes are
permanently assigned to a queue when they
enter the system.
If there are separate queues for foreground and
background processes, for example, processes
do not move from one queue to the other, since
processes do not change their foreground or
background nature.
This setup has the advantage of low scheduling
overhead, but it is not flexible.
P.Ray@CSE
The multilevel feedback-queue scheduling algorithm, in
contrast, allows a process to move between queues.
The idea is to separate processes according to the
characteristics of their CPU bursts.
If a process uses too much CPU time, it will be moved
to a lower-priority queue.
This scheme leaves I/O-bound and interactive
processes in the higher-priority queues.
In addition, a process that waits too long in a lower-
priority queue may be moved to a higher-priority
queue. This form of aging which prevents starvation.

P.Ray@CSE
Multilevel Feedback Queue
 A process can move between the various queues; aging can
be implemented this way.
 Aging is a technique of gradually increase the priority of the
processes that wait in the system for a long time.
 Multilevel-feedback-queue scheduler defined by the
following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will
enter when that process needs service

P.Ray@CSE
Multilevel Feedback Queues

CPU
Ready
Queue

P.Ray@CSE
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – time quantum 8 milliseconds
– Q1 – time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served FCFS. When it
gains CPU, job receives 8 milliseconds. If it does not finish
in 8 milliseconds, job is moved to queue Q1.
– At Q1 job is again served FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted
and moved to queue Q2.

P.Ray@CSE
Process Arrival Burst Priority
Time Time
A 0 3 3
B 1 6 5
C 2 1 2
D 3 4 1
E 4 2 4

Prepare the GANTT chart for the following scheduling


algorithm
1. FCFS
2. SJF
3. Preemptive SJF
4. Round Robin (tq = 1)
5. Priority Preemptive

P.Ray@CSE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy