SlideShare a Scribd company logo
CPU Scheduling
BY
Dr. V. S. PRAKASH
Assistant Professor
Department of Computer Science (UG)
Kristu Jayanti College
Operating System Concepts
CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling
• Real-Time Scheduling
• Algorithm Evaluation
Operating System Concepts
Basic Concepts
• Maximum CPU utilization obtained with multiprogramming
• I/O Burst phase: – when the process waits for some IO
operation
– Process execution consists of a cycle of CPU execution and I/O
wait.
• CPU burst phase :distribution or when the process is allocated
CPU and other resource is executing
Operating System Concepts
Alternating Sequence of CPU And I/O Bursts
Operating System Concepts
Histogram of CPU-burst Times
CPU Scheduler
• Selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them.
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.( I/O wait)
2. Switches from running to ready state.( Interrupt)
3. Switches from waiting to ready.(IO complete)
4. Process terminates.
Scheduling under 1 and 4 is nonpreemptive.
• Nonpreemptive: there is no choice for CPU scheduling ( Stop)
– Terminate or waiting state
– new process present in queue.
• All other scheduling is preemptive(2 and 3):
– Process has choice CPU scheduling and CPU is execute
based on the priority.
Dispatcher
• Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this functionality involves:
– switching context: switching from one process to another
process
– switching to user mode: switching to kernel mode to user
mode
– jumping to the proper location in the user program to
restart that program
• Dispatch latency – time it takes to stop one process and start
another running process.
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution per
time unit
• Turnaround time – amount of time to execute a particular process
• Waiting time – amount of time a process has been waiting in the
ready queue
• 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)
Optimization Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
FCFS: First Come First Serve
• First Come First Serve, is just like FIFO(First in First out) Queue
data structure, where the data element which is added to the
queue first, is the one who leaves the queue first.
• This is used in Batch Systems.
• It's easy to understand and implement programmatically, using
a Queue data structure, where a new process enters through
the tail of the queue, and the scheduler selects process from
the head of the queue.
• A perfect real life example of FCFS scheduling is buying tickets
at ticket counter.
• Burst time: the time CPU taking control of process
First-Come, First-Served (FCFS) Scheduling
• Example: Process Burst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 30
0
FCFS Scheduling (Cont.)
Suppose that the processes will arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:
• 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
P1
P3
P2
6
3 30
0
FCFS
• Problems:
1. It is non-preemptive
2. Improper process Scheduling
3. Resource unitization in parallel is not possible, which
leads to convey effect and hence poor resource
utilization
– convey effect: in which whole os slows down due to few slow
process.
Shortest-Job-First (SJF) Scheduling
• It is the best approach to minimize waiting time.
• It select the waiting process with the smallest
execution time to execute next.
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.
Cpu scheduling
Disadvantage:
The total execution time of job must be known before
execution while it is not possible to perfectly predict
execution
Cpu scheduling
Shortest-Remaining-Time-First (SRTF).
• As you can see in the GANTT chart above, as P1 arrives first,
hence it's execution starts immediately, but just after 1 ms,
process P2 arrives with a burst time of 3 ms which is less than
the burst time of P1, hence the process P1(1 ms done, 20 ms
left) is preemptied and process P2 is executed.
• As P2 is getting executed, after 1 ms, P3 arrives, but it has a
burst time greater than that of P2, hence execution of P2
continues. But after another millisecond, P4 arrives with a burst
time of 2 ms, as a result P2(2 ms done, 1 ms left) is preemptied
and P4 is executed.
• After the completion of P4, process P2 is picked up and finishes,
then P2 will get executed and at last P1.
• The Pre-emptive SJF is also known as Shortest Remaining
Time First, because at any given point of time, the job with the
shortest remaining time is executed first.
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
• Average waiting time = (0 + 6 + 3 + 7)/4 - 4
Example of Non-Preemptive SJF
P1 P3 P2
7
3 16
0
P4
8 12
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)
• Average waiting time = (9 + 1 + 0 +2)/4 - 3
P1 P3
P2
4
2 11
0
P4
5 7
P2 P1
16
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
– nonpreemptive
• SJF is a priority scheduling where priority is the predicted next
CPU burst time.
• Problem  Starvation – low priority processes may never
execute.
• Solution  Aging – as time progresses increase the priority of the
process.
Priority Scheduling
• Priority scheduling can be of two types:
– Preemptive Priority Scheduling: If the new process
arrived at the ready queue has a higher priority than the
currently running process, the CPU is preempted, which
means the processing of the current process is stoped
and the incoming new process with higher priority gets
the CPU for its execution.
– Non-Preemptive Priority Scheduling: In case of non-
preemptive priority scheduling algorithm if a new
process arrives with a higher priority than the current
running process, the incoming process is put at the
head of the ready queue, which means after the
execution of the current process it will be processed.
Characteristics of Priority Scheduling
• A CPU algorithm that schedules processes based on priority.
• It used in Operating systems for performing batch processes.
• If two jobs having the same priority are READY, it works on a
FIRST COME, FIRST SERVED basis.
• In priority scheduling, a number is assigned to each process
that indicates its priority level.
• Lower the number, higher is the priority.
• In this type of scheduling algorithm, if a newer process arrives,
that is having a higher priority than the currently running
process, then the currently running process is preempted.
Example of Priority Scheduling Algorithm
• Consider the above table of processes with their
respective CPU burst times and the priorities.
Problem with Priority Scheduling
Algorithm
• In priority scheduling algorithm, the chances of indefinite
blocking or starvation.
• A process is considered blocked when it is ready to run but
has to wait for the CPU as some other process is running
currently.
• But in case of priority scheduling if new higher priority
processes keeps coming in the ready queue then the
processes waiting in the ready queue with lower priority
may have to wait for long durations before getting the CPU
for execution.
Using Aging Technique with Priority
Scheduling
• To prevent starvation of any process, we can use the
concept of aging where we keep on increasing the
priority of low-priority process based on the its waiting
time.
• For example, if we decide the aging factor to be 0.5 for
each day of waiting, then if a process with
priority 20(which is comparitively low priority) comes in
the ready queue. After one day of waiting, its priority is
increased to 19.5 and so on.
• Doing so, we can ensure that no process will have to
wait for indefinite time for getting CPU time for
processing.
Round Robin Scheduling
• CPU is assigned to the process on the basis of
FCFS for a fixed amount of time.
• This fixed amount of time is called as time
quantum or time slice.
• After the time quantum expires, the running
process is preempted and sent to the ready
queue.
• Then, the processor is assigned to the next
arrived process.
• It is always preemptive in nature.
• Round Robin Scheduling is FCFS Scheduling
with preemptive mode.
Cpu scheduling
Advantages-
• It gives the best performance in terms of average response time.
• It is best suited for time sharing system, client server architecture
and interactive system.
Disadvantages-
• It leads to starvation for processes with larger burst time as they
have to repeat the cycle many times.
• Its performance heavily depends on time quantum.
• Priorities can not be set for the processes.
Note(RRS)
With decreasing value
of time quantum,
•Number of context
switch increases
•Response time
decreases
•Chances of starvation
decreases
With increasing value
of time quantum,
•Number of context
switch decreases
•Response time
increases
•Chances of starvation
increases
•
Problem-01:
Process Id Arrival time Burst time
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process
Id
Exit time
Turn Around
time
Waiting time
P1 13
13 – 0 =
13
13 – 5 = 8
P2 12
12 – 1 =
11
11 – 3 = 8
P3 5 5 – 2 = 3 3 – 1 = 2
P4 9 9 – 3 = 6 6 – 2 = 4
P5 14
14 – 4 =
10
10 – 3 = 7
Average Turn Around time =
(13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6
unit
Average waiting time =
(8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
Consider the set of 6 processes whose arrival time and burst time are given below-
P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
Consider the set of 5 proceses whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
Problem-01:
Process Id Arrival time Burst time
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
Example: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:
• Typically, higher average turnaround than SJF, but better
response.
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
P1=6, p2=5, p3=2, p4=3,p5=7
Multilevel Queue
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• Each queue has 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
Multilevel Queue Scheduling
Multilevel Feedback Queue
• A process can move between the various queues; aging can be
implemented this way.
• 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
Multilevel Feedback Queues
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.

More Related Content

PPTX
Shortest Job First
PPTX
Context switching
PDF
Implementation of FIFO in Linux
PPTX
Shortest job first Scheduling (SJF)
PPT
Memory allocation in c
PDF
CPU Scheduling
PPTX
Cpu scheduling in operating System.
PDF
Monitors
Shortest Job First
Context switching
Implementation of FIFO in Linux
Shortest job first Scheduling (SJF)
Memory allocation in c
CPU Scheduling
Cpu scheduling in operating System.
Monitors

What's hot (20)

PPT
Ipc in linux
PDF
3 additional dpdk_theory(1)
PPTX
CPU scheduling
PDF
Python unit 2 as per Anna university syllabus
PPTX
Operators and expressions in C++
PPT
Process scheduling linux
PDF
Linux scheduler
PPT
Python Control structures
PDF
Virtual machine and javascript engine
PDF
Implementation of Pipe in Linux
PPTX
Network Routing | Computer Science
PDF
Data comm and networking
PPTX
Operating system 25 classical problems of synchronization
PPT
Linux command ppt
PPTX
cpu scheduling
PPTX
Network devices
PDF
IP Datagram Structure
PDF
Protocols and standards
PPT
Ch05 cpu-scheduling
PPTX
PYTHON FEATURES.pptx
Ipc in linux
3 additional dpdk_theory(1)
CPU scheduling
Python unit 2 as per Anna university syllabus
Operators and expressions in C++
Process scheduling linux
Linux scheduler
Python Control structures
Virtual machine and javascript engine
Implementation of Pipe in Linux
Network Routing | Computer Science
Data comm and networking
Operating system 25 classical problems of synchronization
Linux command ppt
cpu scheduling
Network devices
IP Datagram Structure
Protocols and standards
Ch05 cpu-scheduling
PYTHON FEATURES.pptx
Ad

Similar to Cpu scheduling (20)

PPT
Chapter No 4 CPU Scheduling and Algorithms.ppt
PPTX
3Chapter Three- CPU Scheduling this is the best.pptx
PPTX
CPU SCHEDULING ALGORITHMS-FCFS,SJF,RR.pptx
PDF
ch5_CPU Scheduling_part1.pdf
PPT
MODULE 2 for the cpu shcheduling and.ppt
PPT
Introduction of cpu scheduling in operating system
PPT
CPU Scheduling Algorithms(FCFS,SJF,RR).ppt
PPTX
CPU Scheduling.pptx this is operating system
PPTX
UNIPROCESS SCHEDULING.pptx
PPT
ch_scheduling (1).ppt
PPTX
programming .pptx
PPTX
Cpu scheduling
PPTX
Scheduling algorithms
PDF
operating system (1).pdf
PDF
OS Process Chapter 3.pdf
PPTX
operating system scheduling concept lec2
PPT
scheduling Uni processor Long-term .ppt
PPT
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
PPTX
CPU Scheduling
PDF
Ch6 cpu scheduling
Chapter No 4 CPU Scheduling and Algorithms.ppt
3Chapter Three- CPU Scheduling this is the best.pptx
CPU SCHEDULING ALGORITHMS-FCFS,SJF,RR.pptx
ch5_CPU Scheduling_part1.pdf
MODULE 2 for the cpu shcheduling and.ppt
Introduction of cpu scheduling in operating system
CPU Scheduling Algorithms(FCFS,SJF,RR).ppt
CPU Scheduling.pptx this is operating system
UNIPROCESS SCHEDULING.pptx
ch_scheduling (1).ppt
programming .pptx
Cpu scheduling
Scheduling algorithms
operating system (1).pdf
OS Process Chapter 3.pdf
operating system scheduling concept lec2
scheduling Uni processor Long-term .ppt
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
CPU Scheduling
Ch6 cpu scheduling
Ad

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Pre independence Education in Inndia.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
01-Introduction-to-Information-Management.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PDF
Basic Mud Logging Guide for educational purpose
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
Anesthesia in Laparoscopic Surgery in India
Pre independence Education in Inndia.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
VCE English Exam - Section C Student Revision Booklet
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
O7-L3 Supply Chain Operations - ICLT Program
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
Basic Mud Logging Guide for educational purpose
Abdominal Access Techniques with Prof. Dr. R K Mishra

Cpu scheduling

  • 1. CPU Scheduling BY Dr. V. S. PRAKASH Assistant Professor Department of Computer Science (UG) Kristu Jayanti College
  • 2. Operating System Concepts CPU Scheduling • Basic Concepts • Scheduling Criteria • Scheduling Algorithms • Multiple-Processor Scheduling • Real-Time Scheduling • Algorithm Evaluation
  • 3. Operating System Concepts Basic Concepts • Maximum CPU utilization obtained with multiprogramming • I/O Burst phase: – when the process waits for some IO operation – Process execution consists of a cycle of CPU execution and I/O wait. • CPU burst phase :distribution or when the process is allocated CPU and other resource is executing
  • 4. Operating System Concepts Alternating Sequence of CPU And I/O Bursts
  • 6. CPU Scheduler • Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them. • CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state.( I/O wait) 2. Switches from running to ready state.( Interrupt) 3. Switches from waiting to ready.(IO complete) 4. Process terminates. Scheduling under 1 and 4 is nonpreemptive. • Nonpreemptive: there is no choice for CPU scheduling ( Stop) – Terminate or waiting state – new process present in queue. • All other scheduling is preemptive(2 and 3): – Process has choice CPU scheduling and CPU is execute based on the priority.
  • 7. Dispatcher • Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this functionality involves: – switching context: switching from one process to another process – switching to user mode: switching to kernel mode to user mode – jumping to the proper location in the user program to restart that program • Dispatch latency – time it takes to stop one process and start another running process.
  • 8. Scheduling Criteria • CPU utilization – keep the CPU as busy as possible • Throughput – # of processes that complete their execution per time unit • Turnaround time – amount of time to execute a particular process • Waiting time – amount of time a process has been waiting in the ready queue • 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)
  • 9. Optimization Criteria • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time
  • 10. FCFS: First Come First Serve • First Come First Serve, is just like FIFO(First in First out) Queue data structure, where the data element which is added to the queue first, is the one who leaves the queue first. • This is used in Batch Systems. • It's easy to understand and implement programmatically, using a Queue data structure, where a new process enters through the tail of the queue, and the scheduler selects process from the head of the queue. • A perfect real life example of FCFS scheduling is buying tickets at ticket counter. • Burst time: the time CPU taking control of process
  • 11. First-Come, First-Served (FCFS) Scheduling • Example: Process Burst Time P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 P1 P2 P3 24 27 30 0
  • 12. FCFS Scheduling (Cont.) Suppose that the processes will arrive in the order P2 , P3 , P1 . • The Gantt chart for the schedule is: • 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 P1 P3 P2 6 3 30 0
  • 13. FCFS • Problems: 1. It is non-preemptive 2. Improper process Scheduling 3. Resource unitization in parallel is not possible, which leads to convey effect and hence poor resource utilization – convey effect: in which whole os slows down due to few slow process.
  • 14. Shortest-Job-First (SJF) Scheduling • It is the best approach to minimize waiting time. • It select the waiting process with the smallest execution time to execute next.
  • 15. 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.
  • 17. Disadvantage: The total execution time of job must be known before execution while it is not possible to perfectly predict execution
  • 19. Shortest-Remaining-Time-First (SRTF). • As you can see in the GANTT chart above, as P1 arrives first, hence it's execution starts immediately, but just after 1 ms, process P2 arrives with a burst time of 3 ms which is less than the burst time of P1, hence the process P1(1 ms done, 20 ms left) is preemptied and process P2 is executed. • As P2 is getting executed, after 1 ms, P3 arrives, but it has a burst time greater than that of P2, hence execution of P2 continues. But after another millisecond, P4 arrives with a burst time of 2 ms, as a result P2(2 ms done, 1 ms left) is preemptied and P4 is executed. • After the completion of P4, process P2 is picked up and finishes, then P2 will get executed and at last P1. • The Pre-emptive SJF is also known as Shortest Remaining Time First, because at any given point of time, the job with the shortest remaining time is executed first.
  • 20. Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (non-preemptive) • Average waiting time = (0 + 6 + 3 + 7)/4 - 4 Example of Non-Preemptive SJF P1 P3 P2 7 3 16 0 P4 8 12
  • 21. 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) • Average waiting time = (9 + 1 + 0 +2)/4 - 3 P1 P3 P2 4 2 11 0 P4 5 7 P2 P1 16
  • 22. 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 – nonpreemptive • SJF is a priority scheduling where priority is the predicted next CPU burst time. • Problem  Starvation – low priority processes may never execute. • Solution  Aging – as time progresses increase the priority of the process.
  • 23. Priority Scheduling • Priority scheduling can be of two types: – Preemptive Priority Scheduling: If the new process arrived at the ready queue has a higher priority than the currently running process, the CPU is preempted, which means the processing of the current process is stoped and the incoming new process with higher priority gets the CPU for its execution. – Non-Preemptive Priority Scheduling: In case of non- preemptive priority scheduling algorithm if a new process arrives with a higher priority than the current running process, the incoming process is put at the head of the ready queue, which means after the execution of the current process it will be processed.
  • 24. Characteristics of Priority Scheduling • A CPU algorithm that schedules processes based on priority. • It used in Operating systems for performing batch processes. • If two jobs having the same priority are READY, it works on a FIRST COME, FIRST SERVED basis. • In priority scheduling, a number is assigned to each process that indicates its priority level. • Lower the number, higher is the priority. • In this type of scheduling algorithm, if a newer process arrives, that is having a higher priority than the currently running process, then the currently running process is preempted.
  • 25. Example of Priority Scheduling Algorithm • Consider the above table of processes with their respective CPU burst times and the priorities.
  • 26. Problem with Priority Scheduling Algorithm • In priority scheduling algorithm, the chances of indefinite blocking or starvation. • A process is considered blocked when it is ready to run but has to wait for the CPU as some other process is running currently. • But in case of priority scheduling if new higher priority processes keeps coming in the ready queue then the processes waiting in the ready queue with lower priority may have to wait for long durations before getting the CPU for execution.
  • 27. Using Aging Technique with Priority Scheduling • To prevent starvation of any process, we can use the concept of aging where we keep on increasing the priority of low-priority process based on the its waiting time. • For example, if we decide the aging factor to be 0.5 for each day of waiting, then if a process with priority 20(which is comparitively low priority) comes in the ready queue. After one day of waiting, its priority is increased to 19.5 and so on. • Doing so, we can ensure that no process will have to wait for indefinite time for getting CPU time for processing.
  • 28. Round Robin Scheduling • CPU is assigned to the process on the basis of FCFS for a fixed amount of time. • This fixed amount of time is called as time quantum or time slice. • After the time quantum expires, the running process is preempted and sent to the ready queue. • Then, the processor is assigned to the next arrived process. • It is always preemptive in nature. • Round Robin Scheduling is FCFS Scheduling with preemptive mode.
  • 30. Advantages- • It gives the best performance in terms of average response time. • It is best suited for time sharing system, client server architecture and interactive system. Disadvantages- • It leads to starvation for processes with larger burst time as they have to repeat the cycle many times. • Its performance heavily depends on time quantum. • Priorities can not be set for the processes.
  • 31. Note(RRS) With decreasing value of time quantum, •Number of context switch increases •Response time decreases •Chances of starvation decreases With increasing value of time quantum, •Number of context switch decreases •Response time increases •Chances of starvation increases •
  • 32. Problem-01: Process Id Arrival time Burst time P1 0 5 P2 1 3 P3 2 1 P4 3 2 P5 4 3 Consider the set of 5 processes whose arrival time and burst time are given below- If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
  • 33. Now, we know- Turn Around time = Exit time – Arrival time Waiting time = Turn Around time – Burst time Process Id Exit time Turn Around time Waiting time P1 13 13 – 0 = 13 13 – 5 = 8 P2 12 12 – 1 = 11 11 – 3 = 8 P3 5 5 – 2 = 3 3 – 1 = 2 P4 9 9 – 3 = 6 6 – 2 = 4 P5 14 14 – 4 = 10 10 – 3 = 7 Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6 unit Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit
  • 34. Process Id Arrival time Burst time P1 0 4 P2 1 5 P3 2 2 P4 3 1 P5 4 6 P6 6 3 Consider the set of 6 processes whose arrival time and burst time are given below-
  • 35. P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
  • 36. Process Id Arrival time Burst time P1 0 4 P2 1 5 P3 2 2 P4 3 1 P5 4 6 P6 6 3 Consider the set of 5 proceses whose arrival time and burst time are given below- If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
  • 37. Problem-01: Process Id Arrival time Burst time P1 0 5 P2 1 3 P3 2 1 P4 3 2 P5 4 3 Consider the set of 5 processes whose arrival time and burst time are given below- If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
  • 38. Example: RR with Time Quantum = 20 Process Burst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is: • Typically, higher average turnaround than SJF, but better response. P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162
  • 39. P1=6, p2=5, p3=2, p4=3,p5=7
  • 40. Multilevel Queue • Ready queue is partitioned into separate queues: foreground (interactive) background (batch) • Each queue has 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
  • 42. Multilevel Feedback Queue • A process can move between the various queues; aging can be implemented this way. • 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
  • 44. 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.