0% found this document useful (0 votes)
16 views30 pages

7.OS - CPU - Scheduling - Dr. Monika Sethi

The document provides an overview of CPU scheduling, detailing various scheduling algorithms such as First Come, First Served (FCFS), Shortest Job First (SJF), Priority-based Scheduling, and Round Robin (RR). It discusses the concepts of preemptive and non-preemptive scheduling, scheduling criteria, and optimization goals, along with advantages and disadvantages of each algorithm. Additionally, it includes examples and calculations related to average waiting time, turnaround time, and response time for different scheduling scenarios.

Uploaded by

sankalpsharma554
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)
16 views30 pages

7.OS - CPU - Scheduling - Dr. Monika Sethi

The document provides an overview of CPU scheduling, detailing various scheduling algorithms such as First Come, First Served (FCFS), Shortest Job First (SJF), Priority-based Scheduling, and Round Robin (RR). It discusses the concepts of preemptive and non-preemptive scheduling, scheduling criteria, and optimization goals, along with advantages and disadvantages of each algorithm. Additionally, it includes examples and calculations related to average waiting time, turnaround time, and response time for different scheduling scenarios.

Uploaded by

sankalpsharma554
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

CPU Scheduling

Prepared By

Dr. Monika Sethi

Chitkara University Institute of Engineering and Technology


Chitkara University, Punjab

1
CPU Scheduling: Process Behavior

• During its lifetime, a process goes through a sequence of CPU


and I/O bursts.
• In a multi-programmed computer system, multiple process
compete for the CPU at a given time, to complete their
current CPU bursts.

2
Basic Concepts
• The CPU scheduler (a.k.a. short-term scheduler) will select one of the
processes in the ready queue for execution.
• The CPU scheduler algorithm may have tremendous effects on the
system performance
– Interactive systems
– Real-time systems
• 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 the
program

3
When to Schedule?
Under a simple process state transition
model, CPU scheduler could be invoked
at four different points:
1. When a process switches from
running state to the waiting state.
2. When a process switches from the
running state to the ready state.
3. When a process switches from the
waiting state to the ready state.
4. When a process terminates.
Scheduling under 1 and 4 is non-
preemptive.
All other scheduling is preemptive.

4
Non-preemptive vs. Preemptive
Scheduling

• Under non-preemptive scheduling, each running process


keeps the CPU until it completes or it switches to the waiting
(blocked) state.
• Under preemptive scheduling, a running process may be also
forced to release the CPU even though it is neither
completed nor blocked.
– In time-sharing systems, when the running process reaches the end of its
time quantum (slice)
– To allocate CPU to another higher-priority process

5
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 (time
from submission to termination).
Or
Time Difference between completion time and arrival time. Turn
Around Time = Completion Time - Arrival Time
• Waiting time – amount of time a process has been waiting in the ready
queue.
Or
Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time - Burst Time
• Response time – amount of time it takes from when a request was
submitted until the first response is produced, not the complete output.
**Completion Time: Time at which process completes its execution.
6
Optimization Criteria

• Maximize the CPU utilization


• Maximize the throughput
• Minimize the (average) turnaround time
• Minimize the (average) waiting time
• Minimize the (average) response time

7
Scheduling Algorithms

• First Come, First Served (FCFS)


• Shortest Job First (SJF)
• Priority
• Round Robin (RR)

8
First-Come, First-Served (FCFS)
Scheduling

• Single FIFO ready queue.


• No-preemptive.
– Not suitable for timesharing systems
• Simple to implement and understand.
• Average waiting time dependent on the order processes
enter the system.

9
Example of FCFS Scheduling

• Suppose that the processes arrive at time = 0 in the order P2 , P3 , P1


The Gantt chart for the schedule:
3 3 24

P2 P3 P1

0 3 6 30
• Average response time: (6+0+3)/3 = 3
• Average waiting time: (6+0+3)/3 = 3
• Average turnaround time: (30+3+6)/3 = 13
10
Example of FCFS Scheduling (cont.)
Burst Completion Response Turnaround Waiting
Process Time Time Time Time Time
P1 24 30 6 30 6
P2 3 3 0 3 0
P3 3 6 3 6 3

• Suppose that the processes arrive at time = 0 in the order P2 , P3 , P1


The Gantt chart for the schedule:

3 3 24

P2 P3 P1

0 3 6 30

• Average response time: (6+0+3)/3 = 3


• Average waiting time: (6+0+3)/3 = 3
• Average turnaround time: (30+3+6)/3 = 13 11
Question
• Consider the set of 5 processes whose arrival time and burst time are
given below:
Process Arrival Completion Turnaround Waiting
Id time Burst time Time Time Time
P1 3 4 7 4 0
P2 5 3 13 8 5
P3 0 2 2 2 0
P4 5 1 14 9 8
P5 4 3 10 6 3

2 1 4 3 3 1
P3 P1 P5 P2 P4

0 2 3 7 10 13 14

12
Advantages and Disadvantages of FCFS
• Advantages:
1. It is simple and easy to understand.
• Disadvantages:
1. The process with less execution time suffer i.e. waiting time is often
quite long.
2. Favors CPU Bound process then I/O bound process.
3. Convoy effect: If the first process has large burst time, and other
processes have less burst time, then the processes will have
to wait more unnecessarily, this will result in more average waiting
time.
4. Not suitable for time-sharing systems, where it is important that each
user get a share of the CPU at regular intervals.

13
Shortest-Job-First (SJF) Scheduling

• Associate with each process the length of its next CPU burst.

• The CPU is assigned to the process with the smallest CPU burst (FCFS can
be used to break ties).

• Two schemes:
– non-preemptive
– preemptive – Also known as the Shortest-Remaining-Time-First (SRTF).

• Non-preemptive SJF is optimal if all the processes are ready


simultaneously– gives minimum average waiting time for a given set of
processes.

• SRTF is optimal if the processes may arrive at different times.

14
Example for Non-Preemptive SJF
Arrival Burst Completion Response Turnaround Waiting
Process Time Time Time Time Time Time
P1 0.0 7 7 0 7 0
P2 2.0 4 12 6 10 6
P3 4.0 1 8 3 4 3
P4 5.0 4 16 7 11 7

7 1 4 4
P1 P3 P2 P4

0 7 8 12 16

15
Example for Preemptive SJF (SRTF)
Arrival Burst Completion Response Turnaround Waiting
Process Time Time Time Time Time Time
P1 0.0 7 16 0 16 9
P2 2.0 4 7 0 5 1
P3 4.0 1 5 0 1 0
P4 5.0 4 11 2 6 2

2 2 1 2 4 5
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

16
Question

Calculate Average Turnaround


Time, Average Wafting Time.

https://www.geeksforgeeks.org/program-for-shortest-job-first-or-sjf-cpu-scheduling-set-1-non-
preemptive/

17
Advantages and Disadvantages of SJF

• Advantages:
1. Frequently used for long term scheduling.
2. Optimal for average waiting time.
3. Favors shorter jobs against long jobs.

• Disadvantages:
1. Job completion time must be known earlier, but it is hard to predict.
2. May cause starvation, if shorter processes keep coming.
3. Cannot be implemented at the level of short term CPU scheduling.

18
Priority-based (PB) 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 scheme with the priority the remaining time.

19
Example for Priority-based Scheduling
(Non-preemptive)

Burst
Process Priority Time C.T. R.T. T.T. W.T.

P1 3 10 16 6 16 6
P2 1 1 1 0 1 0
P3 4 2 18 16 18 16
P4 5 1 19 18 19 18
P5 2 5 6 1 6 1

1 5 10 2 1
P2 P5 P1 P3 P4

0 1 6 16 18 19

20
Example for Priority-based Scheduling
(Preemptive)

Arrival
Process Time Priority Burst Time C.T. R.T. T.T. W.T.

P1 0 2 4 15 0 15 9
P2 1 3 3 12 1 11 8
P3 2 4 1 3 2 1 0
P4 3 5 5 8 3 5 0
P5 4 5 2 10 8 6 8

(Higher number represents higher priority)

1 1 1 5 2 2 3
P1 P2 P3 P4 P5 P2 P3
0 1 2 3 8 10 12 15

21
Advantages and Disadvantages of PB

• Advantages:
1. Provides a good mechanism where the relative importance of each
process maybe precisely defined.

• Disadvantages:
1. If high priority processes use up a lot of CPU time, lower priority
processes may starve and be postponed indefinitely. The
situation where a process never gets scheduled to run is
called starvation.
• Solution: Aging – as time progresses,
increase the priority of the processes that wait in the
system for a long time.
2. Another problem is deciding which process gets which priority level
assigned to it.
22
Round Robin (RR) 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.

• Newly-arriving processes (and processes that complete


their I/O bursts) are added to the end of the ready queue.

• If there are n processes in the ready queue and the time quantum is
q, then no process waits more than (n-1)q time units.

• Performance
– q large => FCFS
– q small => Processor Sharing (The system appears to the users as
though each of the n processes has its own processor running at the
(1/n)th of the speed of the real processor) 23
Example for Round-Robin

Process Burst Time C.T. R.T. T.T. W.T.


P1 53 134 0 134 81
P2 17 37 20 37 20
P3 68 162 37 162 94
P4 24 121 57 121 97

Time Quantum (q) = 20

20 17 20 20 20 20 4 13 28
P1 P2 P3 P4 P1 P3 P4 P1 P3

0 20 37 57 77 97 117 121 134 162

24
Advantages and Disadvantages of RR

• Advantages:
1. Every process gets an equal share of the CPU.
2. RR is cyclic in nature, so there is no starvation.

• Disadvantages:
1. Setting the quantum too short, increases the overhead and lowers
the CPU efficiency, but setting it too long may cause poor
response to short processes.
2. Average waiting time under the RR policy is often long.

25
Multilevel Queue (MLQ) Scheduling

• Ready queue is divided into various queues that are called subqueues.

• Processes are permanently subqueues based on


property,assignedtosuch as memory priority,some
or process
(background
size, or foreground process).
process type

• Each queue has its own scheduling algorithm.


26
Example of MLQ

• Consider a system with 6 processes where multilevel queue scheduling


has been deployed using three queues labelled as Q1, Q2 and Q3,
with priorities of queues are like, Q1 > Q2 > Q3. The Q1, Q2, and Q3
queues use the shortest remaining time first (SRTF), first come first
serve (FCFS), and round robin (RR) scheduling with 2 ms time quantum,
respectively. The arrival time (ms), burst time (ms), and execution queues
of the processes are given in table below.

Process Id Arrival Time Burst Time Queue No.


P1 0 3 Q1
P2 0 4 Q1
P3 7 2 Q2
P4 6 4 Q2
P5 10 5 Q1
P6 5 3 Q3
27
Example of MLQ

Considering scheduling and context switching overhead as negligible, answer


the following:

1. Draw the Gantt chart showing the sequence of execution for the
processes involved in the system, and also specify the status of Q1, Q2,
and Q3 at time instance t=20, i.e. the processes that reside in
the said queues.

2. What is the turn-around time of processes P4 and P6?

3. What are the number context of switches after


execution of all
process?

28
Example of MLQ
Process Id Arrival Time Burst Time Queue No.
P1 0 3 Q1
P2 0 4 Q1
P3 7 2 Q2
P4 6 4 Q2
P5 10 5 Q1
P6 5 3 Q3

Gantt Chart:
3 4 3 5 2 1 2 1
P1 P2 P4 P5 P3 P4 P6 P6
0 3 7 10 15 17 18 20 21
1. At time instance t = 20 : Q1, Q2, Q3 will be empty.
2. Turnaround time of process P4 = 18-6 =12
Turnaround time of process P6 = 21- 5 = 16
3. Number of context switches = 7 29
Advantages and Disadvantages of MLQ

• Advantages:
1. apply various type of scheduling for
Weprocesses. different kind of
can

• Disadvantages:
1. Due to starvation lower-level processes either never execute or have
to wait for a long amount of time.

30

You might also like