0% found this document useful (0 votes)
39 views48 pages

CH4 and CH5-CPU Scheduling V1

The document discusses CPU scheduling in operating systems, detailing types of schedulers (long-term, short-term, medium-term) and their characteristics. It outlines scheduling criteria aimed at maximizing CPU utilization and throughput while minimizing waiting and response times, along with various scheduling algorithms such as FCFS, SJF, and priority scheduling. Additionally, it explains the states of a process and the role of the dispatcher in managing process execution.

Uploaded by

Nikita Tyagi
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)
39 views48 pages

CH4 and CH5-CPU Scheduling V1

The document discusses CPU scheduling in operating systems, detailing types of schedulers (long-term, short-term, medium-term) and their characteristics. It outlines scheduling criteria aimed at maximizing CPU utilization and throughput while minimizing waiting and response times, along with various scheduling algorithms such as FCFS, SJF, and priority scheduling. Additionally, it explains the states of a process and the role of the dispatcher in managing process execution.

Uploaded by

Nikita Tyagi
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/ 48

OPERATING SYSTEM

CPU SCHEDULING
CONTEN
TS :

 CPU Scheduling

 Types of Schedulers

 Scheduling criteria

 Scheduling algorithms
CHARACTERISTICS OF GOOD PROCESS
SCHEDULER-
The characteristics of a good process scheduler are as follows −

 It increases the utilization of the CPU as it keeps it busy all


the time by assigning the ready state processes.
 It has flexibility you can change your schedules easily
 It allows revisiting or reviewing your projects.
 By using this you can strictly follow the deadlines. For
example − If you have a deadline for a project for SEP 10th then
you can set the deadline for 10th SEP till 6pm or anything.
 Also the tasks are not isolated; you can clearly see the relation
among them or how they are interconnected to each other.
3
TYPES OF SCHEDULING

There are three types


of
schedulers available
which are as follows −

 Long Term
 Short Term
 Medium Term

4
TYPES OF SCHEDULING
 Long Term job scheduler :
 It brings the new process to the ‘Ready State’. It controls Degree of Multi-
programming, i.e., number of process present in ready state at any point of
time.
 It is important that the long-term scheduler make a careful selection of both I/O
and CPU-bound processes.
 I/O bound tasks are which use much of their time in input and output
operations while CPU bound processes are which spend their time on CPU. The
job scheduler increases efficiency by maintaining a balance between the two.
TYPES OF SCHEDULING
 Short term CPU scheduler :

 Short-term scheduling is the


actual decision of which ready
process to execute next.
 The interval of time between
the submission of a process and
its completion.
 Includes actual execution time
plus time spent waiting for
resources, including the
processor.
TYPES OF SCHEDULING
 Medium-term scheduler (Swap Scheduler):

It is responsible for suspending and resuming the process. It


mainly does swapping (moving processes from main memory
to disk and vice versa). Swapping may be necessary to
improve the process mix or because a change in memory
requirements has overcommitted available memory, requiring
memory to be freed up. It is helpful in maintaining a perfect
balance between the I/O bound and the CPU bound. It
reduces the degree of multiprogramming.
SCHEDULING CRITERIA
SCHEDULING CRITERIA
The aim of the scheduling algorithm is to maximize and minimize the following:

 MAXIMIZE:

 CPU utilization - It makes sure that the CPU is operating at its peak and is busy
or CPU busy if possible and to maximize its usage. In theory, the range of CPU
utilization is in the range of 0 to 100 but in real-time, it is actually 50 to 90% which relies on the
system’s load.
 Throughput - It is the number of processes that complete their execution per
unit of time.
SCHEDULING CRITERIA
 MINIMIZE:

 Waiting time- It is the amount of waiting time in the queue.

 Response time- Time retired for generating the first request after submission.

 Turnaround time- It is the amount of time required to execute a specific


Process from the submission to completion. It is the summation of time spent
waiting to get into the memory, waiting for a queue to be ready, for
the I/O process, and for the execution of the CPU. The formula for calculating
TYPES OF SCHEDULING
ALGORITHM
 Pre-emptive
 Non pre-
Scheduling
emptive

Preemptive Scheduling means once a


process started its execution, the Non-Preemptive Scheduling
means once a process starts its
currently running process can be execution or the CPU is processing
paused for a short period of time a specific process it cannot be
to handle some other process of halted or in other words we
higher priority, it means we can cannot preempt (take control) the
preempt the control of CPU from one CPU to some other process.
process to another if required.
STATES OF A PROCESS
 Process scheduler is a part of the Operating system which
schedules the process. If the process is in ready,
waiting and running completed, it schedules it
correctly and is also responsible for the allocation of the CPU
processor to a specific task within a time interval.

 Also, when the task or process is completed, it doesn't allow


the CPU to sit idle. It will allocate the other task
depending on its current state.

 If the process was in ready state it will allocate it to the CPU in a


way that it always keeps the CPU busy all of the time.
12
STATES OF A PROCESS
There are
three types
of STATE

 READY
 RUN
 WAIT

13
COMBINATION OF SCHEDULING & PROCESSS STATES

14
Differences between long term, medium term and short
term scheduler are as follows −
Long term scheduler Medium term scheduler Short term scheduler

Long term scheduler is a job Medium term is a Short term scheduler is


scheduler. process of swapping called a CPU scheduler.
schedulers.
The speed of long term is The speed of medium The speed of short term is
lesser than the short term. term is in between fastest among the other
short and long term two.
scheduler.
Long term controls the Medium term reduces The short term provides
degree of the degree of lesser control over the
multiprogramming. multiprogramming. degree of
multiprogramming.
The long term is almost nil The medium term is a Short term is also a
or minimal in the time part of the time sharing minimal time sharing
sharing system. system. system.
The long term selects the Medium term can Short term selects those 15
processes from the pool and reintroduce the process processes that are ready
STATES OF A PROCESS
New: – This is the initial state where process is about to create.
Ready: – After creation of process, the process enters in the ready state and it waits for the CPU to be
assigned.
Running: – The process is running under the CPU and program is executing by the CPU
Waiting: – When the process request to I/O then it enters in the wait state. It executes in the main memory
and doesn’t require CPU.

Terminated or exit: – When the process finishes its execution or it its terminated by operating system, it
is moved to terminated state.
Dispatcher: – Dispatcher is one of the important entity in CPU scheduling. A dispatcher is placed in
between the ready queue and Short-term scheduler. When the short-term scheduler makes decision that
which process from among the several processes waiting in the ready queue will be allocated CPU next for its
execution, it is the dispatcher that assigns CPU to that selected process.

Context switching:- A dispatcher switch execution from one process to another is called context
switching.
Dispatcher Latency: – dispatcher latency is the amount of time taken by the system to stop one process

16
Shortest-Job-First (SJF)
Scheduling
Completion Time: Time at which process completes its execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

Burst Time
Burst time, also referred to as “execution time”. It is the amount of CPU time
the process requires to complete its execution. It is the amount of processing
time required by a process to execute a specific task or unit of a job. Factors
such as the task’s complexity, code efficiency, and the system’s resources
determine the process’s burst time.

https://www.geeksforgeeks.org/program-for-shortest-job-first-or-sjf-cpu-scheduling-set-1-non-preemptive/
TYPES OF SCHEDULING ALGORITHM
 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) Scheduling


 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or
preemptive.
18
 FIRST- COME, FIRST-SERVED (FCFS) SCHEDULING
 Jobs are executed on first come, first serve basis.
 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.

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


Process Burst Time
Average waiting time: (0 + 24 + 27)/3 = 17
P1 24 Turnaround time for P1 = 24; P2 = 27; P3 = 30
P2 3 Response time for P1 = 0; P2 = 24; P3 = 27
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
FCFS SCHEDULING (CONT.)

Suppose that the processes arrive in the order:

P2 , P3 , P1

Answer: ?
Prepare Gantt chart for the scheduling

Calculate waiting time for P1 P2 P3

Calculate average waiting time

Calculate turnaround time

Calculate response time


 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


 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

ProcessArriva l Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
• Answer: ?
• Prepare Gantt chart for the scheduling
• Calculate waiting time for P1 P2 P3
• Calculate average waiting time
• Calculate turnaround time
• Calculate response time
SHORTEST-JOB-FIRST (SJF) SCHEDULING
ProcessArriva l 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


SHORTEST-JOB-FIRST (SJF) SCHEDULING
Shortest-Job-First (SJF)
Scheduling
Completion Time: Time at which process completes its execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

Burst Time
Burst time, also referred to as “execution time”. It is the amount of CPU time
the process requires to complete its execution. It is the amount of processing
time required by a process to execute a specific task or unit of a job. Factors
such as the task’s complexity, code efficiency, and the system’s resources
determine the process’s burst time.

https://www.geeksforgeeks.org/program-for-shortest-job-first-or-sjf-cpu-scheduling-set-1-non-preemptive/
SHORTEST-JOB-FIRST (SJF) SCHEDULING
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process
with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be
preemptive or non-preemptive.

Characteristics of SJF Scheduling:

Shortest Job first has the advantage of having a minimum average waiting time among all scheduling
algorithms.

 It is a Greedy Algorithm.
 It may cause starvation if shorter processes keep coming. This problem can be solved using the
concept of ageing.
 It is practically infeasible as Operating System may not know burst times and therefore may not
sort them. While it is not possible to predict execution time, several methods can be used to
estimate the execution time for a job, such as a weighted average of previous execution times.
 SJF can be used in specialized environments where accurate estimates of running time are
available.
SHORTEST-JOB-FIRST (SJF) SCHEDULING

 Advantages of SJF:

 SJF is better than the First come first serve(FCFS) algorithm as it reduces the average waiting
time.
 SJF is generally used for long term scheduling
 It is suitable for the jobs running in batches, where run times are already known.
 SJF is probably optimal in terms of average turnaround time.

 Disadvantages of SJF:

 SJF may cause very long turn-around times or starvation.


 In SJF job completion time must be known earlier, but sometimes it is hard to predict.
 Sometimes, it is complicated to predict the length of the upcoming CPU request.
 It leads to the starvation that does not reduce average turnaround time.
 Shortest-remaining-time-first(SRTF)-Improvement in SJF
 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

 Answer: ?
 Prepare Gantt chart for the scheduling
 Calculate waiting time for P1 P2 P3
 Calculate average waiting time
 Calculate turnaround time TAT (Turnaround Time) = Completion Time (CT) – Arrival Time (AT)
 Calculate response time WT (Waiting Time) = Turnaround Time (TAT) – Burst Time (BT)
 Shortest-Remaining-Time-First(SRTF)

https://mycareerwise.com/content/srtf-process-and-
examples/content/exam/gate/computer-science
29
 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)
 SJF is priority scheduling where priority is the inverse of
predicted next CPU burst time
Example of Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Answer: ?
 Prepare Gantt chart for the scheduling

 Calculate waiting time for P1 P2 P3

 Calculate average waiting time


 Calculate turnaround time
 Calculate response time
 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 = (0+1+6+16+18)=8.2 msec


Priority Scheduling

• Problem  Starvation – low priority processes may never execute

• Solution  Aging – as time progresses increase the priority of the


process

• Preemptive or Non-preemptive?
ROUND ROBIN (RR)
The Round-robin scheduling algorithm is a kind
of preemptive First come, First Serve CPU
Scheduling algorithm where each process in the
ready state gets the CPU for a fixed time in a
cyclic way (turn by turn). It is the oldest
scheduling algorithm, which is mainly used
for multitasking.

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

 Timer interrupts every quantum to schedule next


process
ROUND ROBIN (RR)
Advantages
The Advantages of Round Robin CPU Scheduling are:

 A fair amount of CPU is allocated to each job.


 Because it doesn't depend on the burst time, it can truly be implemented in the system.
 It is not affected by the convoy effect or the starvation problem as occurred in First Come First Serve CPU
Scheduling Algorithm.

Disadvantages
The Disadvantages of Round Robin CPU Scheduling are:

 Low Operating System slicing times will result in decreased CPU output.
 Round Robin CPU Scheduling approach takes longer to swap contexts.
 Time quantum has a significant impact on its performance.
 The procedures cannot have priorities established.

35
EXAMPLE OF RR WITH TIME
QUANTUM = 4
Process Burst Time
P1 24
P2 3
P3 3

• Answer: ?
• Prepare Gantt chart for the scheduling
• Calculate waiting time for P1 P2 P3
• Calculate average waiting time
• Calculate turnaround time
• Calculate response time
EXAMPLE OF RR WITH TIME
QUANTUM =
ProcessBurst Time
4
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

 Waiting Time:
P1 =10-(1*4)=6 ms
P2 =4 ms
P3 =7 ms
• Average Waiting: =(6+4+7)/3 =5.66 ms
• Typically, higher average turnaround than SJF, but better response
ROUND ROBIN (RR)

• Problem is high context switching

• Solution:

• q should be large compared to context switch time

• Example: q usually 10ms to 100ms, context switch < 10 ms

• Preemptive or Non preemptive?


 MULTILEVEL QUEUE

 At a time, you do so much work on your computer. You listen to music,


practice coding questions on Coding Ninjas, chat on WhatsApp, etc.
Have you ever wondered how all these processes can work at a time?

 You can argue about a multi-core CPU, but what if you had a single CPU
available?
 Multilevel queue scheduling is one such CPU scheduling algorithm where the tasks
to be performed by the CPU are divided into different groups based on various
properties.
MULTILEVEL QUEUE SCHEDULING'S WORKING
example where the processes are divided into three different queues given below.

40
MULTILEVEL QUEUE SCHEDULING'S WORKING

 When a new process comes, that process is added to one of the above-given
three process queues based on the classification specified for those process
queues.
 Interactive processes: Let us assume that an interactive process like online
gaming wants to utilize the CPU. Then this process will be placed in the
Interactive processes queue.

 System Processes: If any process owned by the operating system itself comes,
it is placed in the System Processes queue, and similarly for other processes.

 Now we have the question of which process will go first in the CPU if all the
process queues have some processes. This dilemma can be solved in different
ways. We have discussed two different ways to solve it.
41
Multilevel Queue Scheduling
FIXED PRIORITY PREEMPTIVE SCHEDULING
METHOD
Each queue is assigned an absolute priority over other queues in this method. For
example, let us consider the following priority order:

System Processes queue > Interactive Processes queue > Batch


Processes queue

According to this priority order, all the processes in the System Processes
queue are initially run, and we don't move to the next process queue until the
current process queue is empty. When all the system queue processes are
executed, we move to the following priority process queue. And similarly, for the
next lower-priority queues.

Suppose we are running the processes from the Interactive process queue, and a
new process arrives in the System processes queue which is of higher priority?
Then we will run the system process first, and then we will move to the
interactive process, and that too only when the system process queue is empty.43
TIME SLICING METHOD
In this method, processes from each queue are run for a fixed amount of time, and then we move on to the next queue.
When we arrive at the last queue, we run it for a specific amount of time, and then we move back to the first queue.

For example, queue1 runs for 50% of CPU time, queue two runs for 30% of CPU time, and queue three runs
for 20% of CPU time. In this way, the total CPU time is divided among each queue to run their processes.

We have solved our problem of processes of which queue is to be run first. Now, we come to the fate of individual
queues. In each queue, we have multiple processes, so how to decide which process should be run first and for how
much time. And do we need to run the processes in all the queues in the same manner as we did in queue one, or can
we implement different ways to run the processes in different queues?

The answer to this question is that we can have different implementations for each queue. And there are many
algorithms using which we can decide the process to be run first and for how long.

For example:

The system process queue can use FCFS(First Come, First Serve) scheduling.
The interactive process queue can use SJF(Shortest Job First) scheduling.
The batch process queue can use RR(Round Robin) scheduling.
44
 PROPERTIES OF MULTILEVEL QUEUE SCHEDULING

 Multilevel Queue Scheduling distributes the processes into multiple queues based on the properties of
the processes.
 Each queue has its own priority level and scheduling algorithm to manage the processes inside that
queue.
 Queues can be arranged in a hierarchical structure.
 High-priority queues might use Round Robin scheduling.
 Low-priority queues might use First Come, First Serve scheduling.
 Processes can move between queues based on their behavior or performance.
 This algorithm prioritizes different types of processes and ensures fair resource allocation.

45
CPU AND I/O BURST CYCLE
• To Maximize CPU utilization by distinguishing
time required by process for CPU execution and
time required by process for I/O or other
operations are named as follows:
• CPU burst

• I/O burst

• CPU burst distribution is of main concern.


References
TEXT BOOKS

 T1: Galvin, Peter B.,Silberchatz,A.,“OperatingSystemConcepts”,AddisonWesley,9th Edition.

 T2: William Stallings, Operating Systems: Internals and DesignPrinciples,7th edition Pearson Education

Limited, 2014ISBN:1292061944, 9781292061948.

REFERENCE BOOKS

 R1: Andrew Tananbaum, “Operating System”, PHIL earning.

 R2: Godbole , Kahate, "Operating System: A Concept Based Approach", Tata Mc-Graw-Hill.

Online Video Link

• https://www.geeksforgeeks.org/operating-systems/

• https://www.geeksforgeeks.org/operating-systems/
47
THANK YOU

You might also like