0% found this document useful (0 votes)
19 views55 pages

2.4 CPU Scheduling

The document discusses CPU scheduling, detailing concepts such as CPU-I/O burst cycles, scheduling algorithms (including FCFS, SJF, and priority scheduling), and their respective advantages and disadvantages. It outlines scheduling criteria like CPU utilization, throughput, turnaround time, waiting time, and response time, emphasizing the importance of optimizing these metrics. Additionally, it provides examples and calculations for various scheduling scenarios to illustrate the impact of different algorithms on performance.

Uploaded by

ragineedas1
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)
19 views55 pages

2.4 CPU Scheduling

The document discusses CPU scheduling, detailing concepts such as CPU-I/O burst cycles, scheduling algorithms (including FCFS, SJF, and priority scheduling), and their respective advantages and disadvantages. It outlines scheduling criteria like CPU utilization, throughput, turnaround time, waiting time, and response time, emphasizing the importance of optimizing these metrics. Additionally, it provides examples and calculations for various scheduling scenarios to illustrate the impact of different algorithms on performance.

Uploaded by

ragineedas1
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/ 55

CPU Scheduling

Deepika R Sahu

Deepika R Sahu 1
CPU-I/O Burst Cycle
• 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

Deepika R Sahu 2
Alternate Sequence of CPU and I/O Burst

Deepika R Sahu 3
CPU Scheduler
• Whenever the CPU becomes idle, the short term
scheduler selects a process from the processes in
memory that are ready to execute and allocates the
CPU to that process.

• A ready queue can be implemented as FIFO queue, a


priority queue, tree or simply an unordered linked
list.

Deepika R Sahu 4
Preemptive Scheduling
• CPU scheduling decisions may take place under the
following four cases:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
• When scheduling takes place only under
circumstances 1 and 4, we say that the scheduling is
non-preemptive or cooperative. Otherwise, it is
preemptive.

Deepika R Sahu 5
Cont..
Non-Preemptive Scheduling
• Once the CPU has been allocated to a process, the
process keeps CPU until it releases the CPU either by
terminating or by switching to the waiting state.

Preemptive Scheduling
• Once the CPU has been allocated to a process, then
CPU can jump to next process, if the first process has
not completed its execution is called preemptive
scheduling
Deepika R Sahu 6
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

Deepika R Sahu 7
Scheduling Criteria
• CPU utilization: To keep the CPU as busy as
possible.
– CPU utilization range from 0 to 100%.
• Throughput – The number of processes that
complete their execution per unit time is called
throughput.
• Turnaround time – amount of time to execute a
particular process. The interval from the time of
submission of a process to the time of completion is
called turnaround time.
Deepika R Sahu 8
Scheduling Criteria
• Waiting time – The amount of time that a process
has been waiting in the ready queue is called waiting
time.
– It is the sum of the periods spent waiting in the
ready queue.
• Response time – The amount of time it takes from
when a request was submitted until the first response
is produced, not output (for time-sharing
environment)

Deepika R Sahu 9
Scheduling Algorithm Optimization Criteria

• Maximize CPU utilization

• Maximize throughput

• Minimize turnaround time

• Minimize waiting time

• Minimize response time


Deepika R Sahu 10
SCHEDULING ALGORITHMS

Deepika R Sahu 11
First- Come, First-Served (FCFS)
Scheduling
• In FCFS, the process that request CPU first is
allocated the CPU first .
• Implementation is managed with a FIFO queue.
• When a process enters the ready queue, its PCB is
linked onto the tail of the queue.
• When the CPU is free, it is allocated to the process at
the head of the queue
• The running process is then removed from the queue.

Deepika R Sahu 12
FCFS
Process Burst Time
P1 24 ms
P2 3 ms
P3 3 ms
• Suppose that the processes arrive at t = 0 ms 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
Deepika R Sahu 13
FCFS
Suppose that the processes arrive at t = 0 ms 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

Deepika R Sahu 14
Tasks-1
• Consider the following processes P1, P2, P3 with their CPU
burst time:
Process Arrival Time Burst Time(ms)
P1 0 10
P2 0 5
P3 0 5

• Using FCFS, calculate the waiting time for each process


and turnaround time of each process if processes are
arriving in the order P1->P2-> P3.
• Also calculate average waiting time, average turnaround
time.
Deepika R Sahu 15
Tasks-2
• Consider the following set of processes with arrival
time and CPU burst time:
Process Arrival Time Burst Time(ms)
P1 0 10
P2 1 6
P3 3 2
P4 5 4

• Using FCFS, calculate average waiting time, average


turnaround time and response time of each process

Deepika R Sahu 16
Tasks-3
• Consider the following set of processes with arrival
time and CPU burst time:
Process Arrival Time(ms) Run Time(ms)
P1 10.00 2.00
P2 10.10 1.00
P3 10.25 0.25
P4 12.00 3.25

• Using FCFS, calculate average waiting time and


average turnaround time.

Deepika R Sahu 17
Note
• Lower the average turnaround time, better it is.

• Lower the average waiting time, better it is.

• Higher CPU utilization, better it is.

• Higher throughput, better it is.

Deepika R Sahu 18
FCFS Advantages:
• FCFS is non-preemptive
• Suitable for batch systems
• Simple, Easy to implement.

FCFS Disadvantages:
• Not suitable for time-sharing systems
• Proper mix of CPU bound and I/O bound jobs is
required.
• Results in poor performance and low throughput.

Deepika R Sahu 19
Criteria
• Turnaround time = Finish Time – Arrival Time

• Waiting time = Start time – Arrival time

• Response Time = Finish Time – Burst time

• Relative delay = Turnaround Time / Burst Time

Deepika R Sahu 20
Shortest-Job-First (SJF) Scheduling
• Use the lengths of CPU bursts to schedule the process
with shortest time.

• Two schemes:
– Non-preemptive : once CPU is given to a process
it cannot be preempted until it completes.
– Preemptive : if a new process arrives with CPU
burst length less than remaining time of current
executing process, preempt.
• This scheme is know as Shortest-Remaining-
Time-First (SRTF).
• SJF is optimal.
Deepika R Sahu 21
Example of Non-Preemptive SJF
Process Burst Time
P1 6 All processes arrive at 0 ms.
P2 8
P3 7
P4 3
• SJF (non-preemptive)

P4 P1 P3 P2
0 3 9 16 24

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


Deepika R Sahu 22
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
P1 P3 P2 P4

0 3 7 8 12 16

• Average waiting time= ([0 – 0] +[8-2] +[7-4] +[12-5])/4


•Average turnaround
= 4 time
= [7 - 0] + [12 – 2 ] +[ 8 - 4 ] + [16 – 5 ] /4 = 32/4 = 8.0ms
Deepika R Sahu 23
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 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


Deepika R Sahu 24
Tasks
• Consider four processes P1, P2,P3,P4 with their CPU
burst time as: Take Preemptive SJF
Process Arrival time Burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

• Calculate AWT, ATAT.


• Sol : AWT = 6.5 ms ATAT = 13ms

Deepika R Sahu 25
SJF
Advantages:
• This algorithm gives minimum average waiting time
so it is an optimal algorithm.

Disadvantages:
1. Difficult to know the length of next CPU burst time.
2. Cannot be implemented at the level of short term
CPU scheduler
3. Big jobs are waiting for CPU. This results in aging
problem.
Deepika R Sahu 26
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
• Equal priority processes are scheduled in FCFS order.

Deepika R Sahu 27
Priority Scheduling
• The priority is the inverse of the next CPU burst.

• The larger the CPU burst , the lower the priority and
vice-versa.

• Low numbers represent high priority

Deepika R Sahu 28
Priority Scheduling
• Priority Scheduling can be either preemptive or non-
preemptive.
• When a process arrives at the ready queue, its priority
is compared with the priority of the currently running
process.
• A Non-Preemptive priority scheduling algorithm
will put the new process at the head of the ready
queue.
• A Preemptive priority scheduling algorithm will
preempt the CPU if the priority of the newly arrived
process is higher than the priority of the currently
running process
Deepika R Sahu 29
Example-I

Deepika R Sahu 30
Example-II

Deepika R Sahu 31
Example-II

Non –Preemptive Priority Scheduling

AWT = 24.4ms ATAT = 37.8 ms

Preemptive Priority Scheduling

AWT = 38+0+37+28+42/5 = 29 ms ATAT = 42.4 ms


Deepika R Sahu 32
Example- III

Deepika R Sahu 33
Example-IV
Process no Arrival Time Burst Time Priority
1 0 3 3
2 1 6 4
3 2 1 9
4 3 2 7
5 4 4 8

Deepika R Sahu 34
Example - IV

Average waiting time


= [0 – 0 ] + [3 – 1] + [9 – 3 ] + [ 11 – 4 ] + [15 – 2 ]
= 28 / 5 = 5.6 ms
Average turnaround time
= [3 – 0 ] + [9 - 1 ] + [ 11 – 3 ] + [15 – 4 ] + [16 – 2 ]
= 44 / 5 = 8.8 ms

Deepika R Sahu 35
Priority Scheduling
• In heavily loaded system, a steady stream of higher
priority processes can prevent a low priority process
from ever getting the CPU. [starvation]

• A solution to the problem of indefinite blockage of


low priority process is aging.

• Aging is a technique of gradually increasing the


priority of processes that wait in the system for a long
time.

Deepika R Sahu 36
Round Robin Scheduling
• Each process gets a small unit of CPU time (time
quantum).

• After this time has elapsed, the process is preempted


and added to the end of the ready queue.

• It is similar to FCFS with preemption added to it.

• 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 Deepika
of at most
R Sahu q time units at once. 37
Round Robin Scheduling
• The ready queue is considered as a FIFO queue of
processes.

• New processes are added to the tail of the queue.

• The CPU scheduler picks the first process from the


ready queue, sets a timer to interrupt after unit time
quantum and dispatch the process.

Deepika R Sahu 38
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• 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

Deepika R Sahu 39
Example-II

Process Arrival Time Service Time


1 0 8
2 1 4
3 2 9
4 3 5

Time Quantum = 4

Average waiting Time = 47 / 4 = 11.75


Deepika R Sahu 40
Example-III

Consider the set of 6 processes whose arrival time and


burst time are given below-

If the CPU scheduling policy is Round Robin with time quantum =


3, calculate the average waiting time and average turn around
time. Deepika R Sahu 41
Solution Example-III
•Average Turn Around time
= (27 + 23 + 30 + 29 + 4 + 15) / 6
= 128 / 6 = 21.33 unit

•Average waiting time


= (22 + 17 + 23 + 20 + 2 + 12) / 6
= 96 / 6 = 16 unit

Deepika R Sahu 42
Example-IV
Process Arrival Time Service Time
1 0 8
2 10 4
3 5 9
4 7 5

Time Quantum = 4

Calculate AWT, ATAT

Deepika R Sahu 43
Example-V
Process Arrival Time Service Time
1 13 8
2 10 4
3 5 9
4 14 5

Time Quantum = 4

Calculate AWT, ATAT

Deepika R Sahu 44
Round Robin Scheduling
• RR scheduling is preemptive.
• The performance of RR depends on the size of time
quantum.
1. If the time quantum is extremely large, the RR
policy is same as FCFS policy.
2. If the time quantum is extremely small, the RR
approach is called processor sharing. Each of the n
processes has its own processor running at 1/n th
speed of real processor.

Deepika R Sahu 45
Multilevel Queue Scheduling
• Another class of scheduling algorithms has been
created in which processes are classified into different
group.
• Common division is made between :
 Foreground(interactive) processes
 Background (batch) processes

• Foreground have priority over background processes.

Deepika R Sahu 46
Multilevel Queue Scheduling
• A multilevel queue scheduling algorithm partitions
the ready queue into several separate queues.

• The processes are permanently assigned to one


queue:
 Based on memory size, process priority or process
type.

• Each queue has its own scheduling algorithm


 foreground queue - RR
 background queue – FCFS
Deepika R Sahu 47
Multilevel Queue Scheduling

Deepika R Sahu 48
Multilevel Queue Scheduling
• Scheduling must be done between the queues:
1. Fixed priority scheduling:
• Each queue has absolute priority over lower priority
queues.
• No process in the batch queue could run unless
the queues for system processes, interactive
processes & interactive editing processes were all
empty.
• If an interactive editing process entered the ready
queue while a batch process are running, the batch
process would be preempted.
Deepika R Sahu 49
Multilevel Queue Scheduling
2. 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.
Advantages:
1. Processes permanently assigned to queue
2. Low scheduling overhead.

Disadvantages
3. Starvation of process occurs.
4. Processes are never allowed to change their queues.

Deepika R Sahu 50
Multilevel Feedback Queue Scheduling
• A process can move between the various queues.
• Idea : Separate process according to the
characteristics of their CPU burst.
• 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.
• A process that waits long in a lower priority queue
may be moved to a higher priority queue. This forms
aging prevents starvation.

Deepika R Sahu 51
Multilevel Feedback Queue Scheduling
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS

Deepika R Sahu 52
Multilevel Feedback Queue Scheduling

• 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

Deepika R Sahu 53
Multilevel Feedback Queue Scheduling
• 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
Deepika R Sahu 54
GO THROUGH ALL
CPU SCHEDULING
ALGORITHMS
CAREFULLY.☻☻☻

Deepika R Sahu 55

You might also like