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

Os Chapter 3

Chapter 3 discusses CPU scheduling, a critical function in multiprogrammed operating systems, focusing on various scheduling algorithms and their evaluation criteria. It explains the roles of long-term, medium-term, and short-term schedulers, along with preemptive and non-preemptive scheduling methods. Additionally, it covers key concepts such as waiting time, turnaround time, and throughput, highlighting the importance of optimizing CPU utilization and performance.

Uploaded by

kalkidanasdro11
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)
5 views

Os Chapter 3

Chapter 3 discusses CPU scheduling, a critical function in multiprogrammed operating systems, focusing on various scheduling algorithms and their evaluation criteria. It explains the roles of long-term, medium-term, and short-term schedulers, along with preemptive and non-preemptive scheduling methods. Additionally, it covers key concepts such as waiting time, turnaround time, and throughput, highlighting the importance of optimizing CPU utilization and performance.

Uploaded by

kalkidanasdro11
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/ 81

Chapter-3

CPU scheduling
Chapter Objectives:
To introduce CPU scheduling, which is the basis
for multiprogrammed operating systems.
 To discuss evaluation criteria for selecting a
CPU-scheduling algorithm for a particular
system.
To describe various CPU-scheduling algorithms.
Basic Concepts

 In a single-processor system, only one process can run at a


time; any others must wait until the CPU is free and can be
rescheduled.
 The objective of multiprogramming is to have some process
running at all times, to maximize CPU utilization.
 Scheduling is a fundamental operating-system function. Almost
all computer resources are scheduled before use.
 The CPU is, of course, one of the primary computer resources.
Thus, its scheduling is central to operating-system design.
Process scheduling

 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.
 The process scheduling is the activity of the process manager that
handles the removal of the running process from the CPU and the
selection of another process on the basis of a particular strategy.
 A scheduler is an OS program that selects the next job to be
admitted for execution.
Cont…
 NOTE: 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


CPU Scheduler
 Selects from among the processes in memory that are ready to execute and

allocates the CPU to one of them.


 The aim of CPU scheduling is to make the system efficient, fast, and fair.

 CPU scheduling decisions may take place when a process:

1. Switches from running to waiting state (ex., I/O request).

2. Switches from running to ready state (ex., Interrupts occur).

3. Switches from waiting to ready (ex., Completion of I/O).


4. Terminates.
 Note:- Scheduling under 1 and 4 is non-preemptive(till it terminates or switches
to waiting state); otherwise is called preemptive (limited time).
Preemptive Scheduling:
Preemptive scheduling is used when a process switches from
running state to ready state or from waiting state to ready state.
The resources (mainly CPU cycles) are allocated to the process
for the limited amount of time and then is taken away, and the
process is again placed back in the ready queue if that process
still has CPU burst time remaining.
That process stays in ready queue till it gets next chance to
execute.
Algorithms based on preemptive scheduling are:
Round Robin (RR),Shortest Remaining Time First (SRTF) ,
Priority (preemptive version), etc.
Non-Preemptive Scheduling:
 Non-preemptive Scheduling is used when a process terminates, or a
process switches from running to waiting state.
 In this scheduling, once the resources (CPU cycles) is allocated to a
process, the process holds the CPU till it gets terminated or it reaches a
waiting state.
 Incase of non-preemptive scheduling process does not interrupt in the
middle of execution.
 Instead, it waits till the process complete its CPU burst time and then it can
allocate the CPU to another process.
 Algorithms based on non-preemptive scheduling are:
Shortest Job First (SJF basically non preemptive) and
Priority (non preemptive version), etc.
Process Scheduling Queues revision
 Job queue − This queue keeps all the processes in the
system.
 Ready queue − This queue keeps a set of all processes
residing in main memory, ready and waiting to execute. A
new process is always put in this queue.
 Device queues − The processes which are blocked due to
unavailability of an I/O device constitute this queue.
Cont…
Types of scheduler
 Schedulers are special system software which handle process

scheduling in various ways.


 Their main task is to select the jobs to be submitted into the system and

to decide which process to run. Schedulers are of three types −


 Long term scheduler

 Medium term scheduler

 Short term scheduler


Long-Term scheduler
 Due to the smaller size of main memory initially all program are stored

in secondary memory.
 When they are stored or loaded in the main memory they are called

process.
 This is the decision of long term scheduler that how many processes will

stay in the ready queue.


 Hence, in simple words, long term scheduler decides the degree of

multi-programming of system.
Cont…
It also known as a job scheduler

 “ Long-term ” scheduler b/c the time for which the

scheduling is valid is long.


 When the processor utilization is low then the scheduler

admits more jobs to increase the number of processes in


the ready queue.
Con’t…
 Similarly when the utilization factor is very high then the

long-term scheduler may reduce the rate of batch jobs.


 Long-term scheduler is invoked very infrequently

(seconds, minutes)  (may be slow).


 Selects which processes should be brought into the ready

queue.
Medium Term Scheduler
 Most often, a running process needs I/O operation which doesn’t requires CPU.
 Hence during the execution of a process when an I/O operation is required then
the operating system sends that process from running queue to blocked queue.
 When a process completes its I/O operation then it should again be shifted to
ready queue.
 Or in other word, a running process may become suspended if it makes an I/O
request.
 In this condition, to remove the process from memory and make space for other
processes, the suspended process is moved to the secondary storage.
 All these decisions are taken by the medium-term scheduler. Medium-term
scheduling is a part of swapping. Or This process is called swapping
Short-Term scheduler
 Short-Term scheduler(CPU scheduler) selects a process among the processes that are ready to

execute and allocates CPU to one of them.

 It is the change of ready state to running state of the process.

 Its main objective is to maximize CPU utilization and to increase system performance in

accordance with the chosen set of criteria.

 Short-term scheduler is invoked very frequently (milliseconds)  (must be fast).

 Selects which process should be executed next and allocates CPU.


Cont…
NOTE: Short-term scheduler executes at least once every
10 milliseconds.
 Short-term schedulers, also known as dispatchers, make
the decision of which process to execute next.
Dispatcher
A dispatcher is a special program which comes into play
after the scheduler.
When the scheduler completes its job of selecting a
process, it is the dispatcher which takes that process to
the desired state/queue.
The dispatcher is the module that gives a process control
over the CPU after it has been selected by the short-term
scheduler.
Difference between dispatcher and
scheduler
Con’t…
Schedulers with diagram
Comparison among Scheduler
S.N
Long-Term Scheduler Short-Term Scheduler Medium-Term
.
Scheduler
1
It is a job scheduler It is a CPU scheduler It is a process swapping
scheduler.
2
Speed is lesser than short Speed is fastest among Speed is in between
term scheduler other two both short and long
term scheduler.
3
It controls the degree of It provides lesser control It reduces the degree of
multiprogramming over degree of multiprogramming.
multiprogramming
4
It is almost absent or It is also minimal in time It is a part of Time
minimal in time sharing sharing system sharing systems.
system
5
It selects processes from It selects those It can re-introduce the
pool and loads them into processes which are process into memory
Scheduling criteria's
Burst time
Every process in a computer system requires some
amount of time for its execution. This time is both the
CPU time and the I/O time.
In general, we ignore the I/O time and we consider only
the CPU time for a process. So, Burst time is the total
time taken by the process for its execution on the CPU.
Arrival time
Arrival time is the time when a process enters into the
ready state and is ready for its execution.

Exit time
Exit time is the time when a process completes its execution and
exit from the system.
Response time
Response time is the time spent when the process is in the ready state
and gets the CPU for the first time.
Response time = Time at which the process gets the CPU for the first time - Arrival time

For example, here we are using the First Come First Served CPU scheduling
algorithm for the below 3 processes:
process Arrival time Burst time

P1 0 ms 8 ms

P2 1 ms 7ms

P3 2 ms 10ms
Cont…
Here, the response time of all the 3 processes are:

 P1: 0 ms

 P2: 7 ms because the process P2 have to wait for 8 ms during the execution
of P1 and then after it will get the CPU for the first time. Also, the arrival
time of P2 is 1 ms. So, the response time will be 8-1 = 7 ms.

 P3: 13 ms because the process P3 have to wait for the execution of P1 and
P2 i.e. after 8+7 = 15 ms, the CPU will be allocated to the process P3 for
the first time. Also, the arrival of P3 is 2 ms. So, the response time for P3
will be 15-2 = 13 ms.
Waiting time
Waiting time is the total time spent by the process in the ready state waiting for
CPU.

 Waiting time = Turnaround time - Burst time

For example, consider the arrival time of all the below 3 processes to be 0 ms, 0
ms, and 2 ms and we are using the First Come First Serve scheduling algorithm.

Process Arrival time Burst time


P1 0 ms 8ms
P2 0ms 7ms
P3 2ms 10ms
Gantt chart
P1 P2 P3
0 ms 8 8 ms 15 15 ms 25
ms ms ms
Cont…
Then the waiting time for all the 3 processes will be:

 P1: 0 ms
 P2: 8 ms because P2 have to wait for the complete execution of
P1 and arrival time of P2 is 0 ms.
 P3: 13 ms becuase P3 will be executed after P1 and P2 i.e. after
8+7 = 15 ms and the arrival time of P3 is 2 ms. So, the waiting
time of P3 will be: 15-2 = 13 ms.
What is the difference between waiting time and
response time?

Response time is the time spent between the ready state and
getting the CPU for the first time.
But the waiting time is the total time taken by the process in the
ready state.
Let's take an example of a round-robin scheduling algorithm. The
time quantum is 2 ms.
Cont…

Process Arrival time Burst time


P1 0 ms 4 ms
P2 0 ms 6 ms

 Time quantum= 2 ms
Gantt chart

P1 P2 P1 P2 p2
0 2 4 6 8
2 4 6 8 10

The response time of the process P2 is 2 ms because after 2 ms, the CPU is
allocated to P2 and the waiting time of the process P2 is 4 ms i.e turnaround time -
burst time (10 - 6 = 4 ms).
Turnaround time
Turnaround time is the total amount of time spent by the process from coming in
the ready state for the first time to its completion.
Turnaround time = Burst time + Waiting time
or
Turnaround time = Exit time - Arrival time

 For example, if we take the First Come First Serve scheduling algorithm, and the
order of arrival of processes is P1, P2, P3 and each process is taking 2, 5, 10
seconds.
 Then the turnaround time of P1 is 2 seconds , P2 is 7 seconds ,and P3 will be 17
seconds
 Note: Different CPU scheduling algorithms produce different turnaround time
for the same set of processes. This is because the waiting time of processes differ
when we change the CPU scheduling algorithm.
Throughput

Throughput is a way to find the efficiency of a CPU.


 It can be defined as the number of processes executed by the CPU in a given
amount of time.
For example, let's say, the process P1 takes 3 seconds for execution, P2 takes
5 seconds, and P3 takes 10 seconds. So, in this case, the throughput will be
(3+5+10)/3 = 18/3 = 6 seconds.

CPU utilization
Keep the CPU as busy as possible
Optimization Criteria
 Max CPU utilization

 Max throughput

 Min turnaround time

 Min waiting time

 Min response time


Categories of Scheduling Algorithms

 Not surprisingly, in different environments different scheduling algorithms are


needed. This situation arises because different application areas (and different kinds
of operating systems) have different goals.
 In other words, what the scheduler should optimize for is not the same in all
systems. Three environments worth distinguishing are:
 Batch
 Interactive and
 Real time
Reading assignment
◦ Which of the following scheduling algorithms are belonging to each
environment? Why?
Scheduling Algorithms
 A Process Scheduler schedules different processes to be assigned to the CPU

based on particular scheduling algorithms.


 There are six popular process scheduling algorithms:

• First-Come First-Served (FCFS)

• Shortest-Job-First (SJF)

• Priority

• Round-Robin (RR)
• Multilevel Queue and

• Multilevel Feedback Queue


Cont…
 These algorithms are either non-preemptive or preemptive.
 Non-preemptive algorithms are designed so that once a process
enters the running state, it cannot be preempted until it completes
its allotted time,
 whereas the preemptive scheduling is based on priority where a
scheduler may preempt a low priority running process anytime
when a high priority process enters into a ready state.
1. First-Come First-Served (FCFS)
Scheduling
It is the simplest CPU scheduling algorithm.
The process that requests the CPU first is
allocated the CPU first.
The average waiting time under FCFS is long.
Figure below shows First Come First Serve
Scheduling
Example:1
Process Burst Time (In milliseconds)
P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P 1 , P2 and P3
 The Gantt Chart for the schedule is:
P1 P2 P3

0 24 27 30

 Waiting time for P1 = 0, P2 = 24 and P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17 ms
 Average Turnaround Time (ATAT) = 24+27+30/3= 27 ms
Example 2
 Suppose that the processes arrive in the order: P2 , P3 and

P1 P2 P3 P1

0 3 6 30

 Waiting time for: P1, P2, and , P3 ?

 Average waiting time?


 Average Turnaround Time (ATAT)?
 Waiting time for: P1 = 6, P2 = 0, P3 = 3

 Average waiting time: (0 + 3 + 6)/3 = 3 ms


 Average Turnaround Time (ATAT)= 3+6+30/3=13
ms
 Much better than previous case.
 The order of processes in FCFS queue is important.
Exercise 1
Consider the following processes: P1,P2 and
P3 with their CPU burst time as shown below
Process CPU burst time
(ms)
P1 7
P2 8
P3 5
Base on the above table
A. Draw the Gantt chart
B. Waiting time for each process (WT)
C. Average Waiting time (AWT)
D. Average Turnaround Time (ATAT)
Advantages and Disadvantages of FCFS

Advantage
 Simple and easy algorithm to implement
 It is suitable for batch system

Disadvantage
 The average waiting time is not minimal
 It is not suitable for time sharing system
2. Shortest-Job-First (SJF) Scheduling
 Also known as shortest job next.
 The basic principle of this algorithm is to allocate the CPU to the process with least CPU
burst time.
 Associate each process with the length of its next CPU burst.
 Use these lengths to schedule the process with the shortest time.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not
known.
 The processer should know in advance how much time process will take.
Con’t…
Two schemes:
 Non-preemptive: Once CPU given to the process it can not 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.
 Note: If the two processes have same CPU burst time the FCFS
is used to break the tie.
Example of Non-Preemptive SJF

Process Id Arrival Time Burst


Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

1. Draw Gantt chart for the above process?


2. Waiting time for each process?
3. Average waiting time?
Solution
1. Gantt chart
P1 P3 P2 P4

0 7 8 12 16

2. Waiting time for each process


Waiting time= total waiting time-arrival
time
waiting time: P1=0, P2=6, P3=3 and P4=7
3. Average waiting time = (0+ 6 + 3 + 7) /
4=4
Con’t…
 If we used FCFS, then the average waiting time would be:

(0 + 5 + 7 + 7) / 4 = 4.75
 The SJF gives less average waiting time than FCFS.

 The SJF is a nonpreemptive in which the waiting process

with the smallest estimated run-time-to completion is run


next.
 Both FCFS and SJF are not useful for timesharing

environments, because they are nonpreemptive.


Example1 of Preemptive SJF
 SJF (preemptive) [Shortest-Remaining-Time-First (SRTF)]
Process ID Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

 waiting time for each process?


 Average waiting time?
Solution
Waiting time for each process= total waiting
time-number of millisecond process executed-
arrival time
P1= 11-2-0=9ms
P2= 5- 2-2=1ms
P3= 4-0-4= 0ms
P4= 7-0-5= 2ms
Average waiting time = (9 + 1 + 0 +2) / 4 =
3 ms
Example2 of Preemptive SJF
Process Id Arrival time Burst time

P1 0 12

P2 2 4

P3 3 6

P4 8 5

Gantt chart?
Waiting time?
Average waiting time?
Solution

1. Gantt chart of the above process


P1 P2 P3 P4 P1
0 2 6 12 17
27

2. Waiting time for each process


P1=17-2-0=15
p2=2-0-2=0
P3=6-0-3=3
P4=12-0-8=4
3. Average waiting time =(15+0+3+4)/4= 5.5 ms
Exercise 2

 Consider the following set of processes having their CPU-burst

time and arrival time. Based on these find the average waiting
time using
A. Non- preemptive SJF?
B. Preemptive SJF?
Process Arrival Time Burst Time
P1 0 3
P2 2 6
P3 4 4
P4 6 5
P5 8 2
3. Priority Scheduling
 A priority number (integer) is associated with each process,

and the CPU is allocated to the process with the highest priority.
 Priority may be determined by users or by some default

mechanism.
 The system may determine the priority based on memory

requirements, time limits, or other resource usage .

 The CPU is allocated to the process with the highest priority

(smallest integer = highest priority).


 Equal priority processes are scheduled in FCFS order.
Con’t…

Figure above shows: Priority Scheduling


Con’t…
 Priorities can be defined either internally or externally.
Internally priority:- use of some measurable quantity or
quantities to compute the priority of a process.
 For example, memory requirements, number of open
files, ratio of average I/O burst to average CPU burst
have been used in computing priorities.
Externally priority:- is set by criteria that is external to
the O.S.; such as importance of the process.
Con’t…
 SJF is a special case of general priority scheduling,

where as priority is the predicted next CPU burst time.


 Priority can be either preemptive or nonpreemptive.

A preemptive: priority will preempt the CPU if the newly


arrived process is higher than the priority of the
currently running process.
A nonpreemptive: priority will simply put the new
highest priority process at the head of the ready queue.
Problem with priority scheduling
 A major problem with priority scheduling is indefinite blocking or Starvation

(waiting for the CPU but never gets the CPU)

 A process that is ready to run but waiting for the CPU can be considered blocked.

 A priority scheduling algorithm can leave some low priority processes waiting

indefinitely.

 In a heavily loaded computer system, a steady stream of higher-priority processes

can prevent a low- priority process from ever getting the CPU.
Solution to the problem
 A solution to the problem of indefinite blockage of low-priority
processes is aging. Aging is a technique of gradually increasing
the priority of processes that wait in the system for a long time.
 For example:

 Ifpriorities range from 1279low) to 0 (high), we could increase


the priority of a waiting process by 1 every 15 minutes.
 Eventually, even a process with an initial priority of 127 would
have the highest priority in the system and would be executed.
Example (preemptive base)
 Consider a set of three processes P1,P2 and P3 with their priorities
and arrival times
Process Burst time Priority Arrival time
P1 10 3 0
P2 5 2 1
P3 2 1 2

1= highest priority, 3= least priority

P1 P2 P3 P2 P1

0 1 2 4 8 17
AWT = (8-1)+(4-2)+0/3
= 7+2+0/3=9/3=3 ms
Exercise (Non-preemptive)
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart

Average waiting time?


Solution

 Waiting time= total waiting time-number of

processes execution in milliseconds-arrival time

 Waiting time P1=6, P2=0,P3=16,P4=18 and P5=1

 Therefore:

◦ Average waiting time = 8.2 ms


Exercise 2:
 Consider the set of process with arrival time(in milliseconds), CPU
burst time (in milliseconds) and priority( 0 is the highest priority)
shown below. None of the processes have I/O burst time.
Process Id Arrival time Burst time Priority
P1 0 11 2
P2 5 28 0
P3 12 2 3
P4 2 10 1
P5 9 16 4

 Theaverage waiting time(in milliseconds)of all the


processes using preemptive priority scheduling
algorithm is _____?
Solution
 Gantt chart
P1 P4 P2 P4 P1 P3 p5
0 2 67
5 33 40 49 51
 Priority for each process:
Waiting time= total waiting time -number of processes execution in milliseconds -
arrival time
P1= 40-2-0=38
P2= 5-0-5= 0
P3= 49-0-12= 37
P4= 33-3-2= 28
P5= 51-0-9= 42
Average WT= (38+0+37+28+42)/5= 29
4. Round Robin (RR) Scheduling
 The Round Robin is designed for time sharing systems.

 The RR is similar to FCFS, but preemption is added to switch between

processes.
 Each process gets a small unit of CPU time (time quantum or time slice )

usually 10-100 milliseconds.


 After this time has elapsed, the process is preempted and added to the end

of the ready queue.


 That is, after the time slice is expired an interrupt will occur and a context

switch.
Con’t…

Figure above shows: Round Robin Scheduling


Con’t…

 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.
Con’t…
 The performance of RR depends on the size of

the time slice q:


 If q very large (infinite) ⇒FCFS
 If q very small ⇒RR is called processor sharing,
and context switch increases.
So, q must be large with respect to context
switch, otherwise overhead is too high.
Example 1: RR with Time Quantum = 20
Process Burst Time Waiting Time of each Process
P1 53 0+(77-20)+(121-97)=81
P2 17 20
P3 68 37+(97-57)+(134-117)=9
P4 24 57+(117-77)=97
 The Gantt chart is:

Average Waiting Time = (81+20+94+97) / 4 = 73 ms


Exercise 2:
 Consider the set of 5 processes whose arrival time and burst
time are given below:

 Ifthe CPU scheduling policy is round robin with the time


quantum= 2 units, calculate the average waiting time and
average turnaround time.
Solution
Gantt chart:

Ready queue Time


(clock)
p2 1
P1 P3 2
P4 P1 P3 3
P2 P5 P4 P1 4
P2 P5 P4 5
P1 P2 P5 7
… … ….
P5 P1 12
Solution Cont…
Solution Cont…
5. Multi level Queue Scheduling
 Ready queue is partitioned into separate

queues:
– Foreground (interactive)
– Background (batch)
 Each queue has its own scheduling algorithm:

– Foreground – RR
– Background – FCFS
Con’t…
 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:


 80% to foreground in RR

 20% to background in FCFS


Multilevel Queue Scheduling
6. Multilevel Feedback Queue
 Different from Multilevel Queue Scheduling by Allowing

Processes to Migrate among queues.


 A process can move between various queues, aging can

be implemented this way.


 If a process waits too long in a lower-priority queue may

be moved to a higher-priority queue (this form of aging to


prevent starvation).
Con’t…

 If a process uses too much CPU time, it will be

moved to lower-priority queues.


 This leaves I/O bound and interactive processes

in the higher-priority queues.


 In general, the multilevel feedback queue

scheduling algorithm is the most complex.


Example: 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.
 The Multilevel Feedback Queue Scheduling is preemptive.
Multilevel Feedback Queues
Thank you !!!

You might also like