SlideShare a Scribd company logo
1 of 31
BITS, PILANI – K. K. BIRLA GOA CAMPUS
Operating Systems
(CS C372 & IS C362)
by
Mrs. Shubhangi Gawali
Dept. of CS and IS
1July 16, 2014
OPERATING SYSTEMS
(CS C372 & IS C362)
LECTURE 16: CPU
SCHEDULING
Linux Scheduling
Linux 2.4 scheduler - goals
 Linux 2.4 scheduler provided real-time scheduling
capability coupled with a scheduler for non-real-time
processes
 Good interactive performance even during high load
 If the user types or clicks then the system must react
instantly and must execute the user tasks smoothly,
even during considerable background load.
 Fairness
 No process should stay without any timeslice for any
unreasonable amount of time.
 No process should get an unjustly high amount of
CPU time.
July 16, 2014 4
Linux 2.4 scheduler - goals
 Supports SMP: single runqueue, individual
scheduler
 SMP efficiency
 No CPU should stay idle if there is work to do.
 SMP affinity
 Processes which run on one CPU should stay
affine to that CPU.
 Processes should not bounce between CPUs too
frequently.
July 16, 2014 5
The three Linux 2.4 scheduling classes
 RT process is scheduled according to one of
the following classes
 SCHED_FIFO: First-in-first-out real-time threads
 SCHED_RR: Round-robin real-time threads
 Non RT process (Conventional time shared process)
 SCHED_OTHER: Other, non-real-time threads
 Within each class, multiple priorities may be used, with
priorities in the real-time classes higher than the
priorities for the SCHED_OTHER class.
July 16, 2014 6
SCHED_FIFO scheduling class
For FIFO threads, the following rules apply:
1. The system will not interrupt an executing FIFO thread
except in the following cases:
a. Another FIFO thread of higher priority becomes ready.
b. The executing FIFO thread is blocked for an I/O.
c. The executing FIFO thread voluntarily gives up the
processor.
2. When an executing FIFO thread is interrupted, it is placed
in the queue associated with its priority.
3. If more than one thread has that highest priority, the
thread that has been waiting the longest is chosen.
July 16, 2014 7
SCHED_RR scheduling class
 The SCHED_RR policy is similar to the
SCHED_FIFO policy, except for the addition of a
timeslice associated with each thread.
 When a SCHED_RR thread has executed for its
timeslice, it is suspended and a real-time thread
of equal or higher priority is selected for running.
July 16, 2014 8
Real time scheduling example in Linux
2.4
July 16, 2014 9
Drawbacks of Linux 2.4 scheduler :
 Scheduler for the SCHED_OTHER class did
not scale well with increasing number of
processors and increasing number of
processes.
 The scheduler uses a single runqueue for all
processors in a symmetric multiprocessing
system (SMP) (good for load balancing but
bad for memory caches)
 Uses a single runqueue lock.
 Preemption is not possible.
July 16, 2014 10
Linux Scheduling
O(1) scheduler
Evolution of Linux schedulers
Linux 2.4 scheduler Linux 2.6 O(1) scheduler
SMP with Single run queue, lacks
scalability
SMP with individual run queue
Single run queue Two queues (active and expiry)
Selection takes O(N) time as it iterates
over every process
Selection take constant time (O(1)) as
from priority queue, dequeue the next
from active runqueue
Inefficient and weak for real time
system
Much more efficient and much more
scalable.
Priority of the process is fixed (Static
priority)
Priority changes as per cpu usage.
Static and dynamic priority.
Incorporated interactivity metrics with
numerous heuristics to determine
whether the process was I/O bound or
processor bound
July 16, 2014 12
O(1) scheduler in Linux 2.6
 To correct these problems, Linux 2.6 uses
the O(1) scheduler.
 The scheduler is designed so that the time to
select the appropriate process and assign it
to a processor is constant,
 regardless of the load on the system or the
number of processors.
July 16, 2014 13
O(1) scheduler in Linux 2.6
 Preemptive priority based round robin
scheduler
 Two separate priority ranges:
 Real time range : 0 to 99
 Nice value range: 100 to 139
 These two ranges map into global priority
scheme.
 Higher priority task : larger time quanta
 Lower priority task : smaller time quanta
July 16, 2014 14
The Relationship Between
Priorities and Time-slice length
July 16, 2014 15
139
Data structure
 The kernel maintains two scheduling data structure for
each processor in the system, of the following form
struct prio_array {
int nr_active; /* number of tasks in this array*/
unsigned long bitmap[BITMAP_SIZE]; /* priority bitmap */
struct list_head queue[MAX_PRIO]; /* priority queues */
 Two such structures are maintained:
 an active queues structure and
 an expired queues structure.
 A separate queue is maintained for each priority level.
July 16, 2014 16
List of Tasks Indexed According to
Prorities
July 16, 2014 17
[139 ] [139 ]
July 16, 2014 18
O(1) scheduling algorithm in Linux
2.6 Initially, both bitmaps are set to all zeroes and all queues
are empty.
 As a process becomes ready, it is assigned to the
appropriate priority queue in the active queues structure and
is assigned the appropriate timeslice.
 Possibilities:
 If the running process is preempted before its timeslice, it is returned
to an active queue.
 When a task completes its timeslice, it goes into the appropriate
queue in the expired queues structure and is assigned a new
timeslice.
 If the running process goes for I/O , priority (dynamic) is recomputed
and the process is added to the appropriate active queue.
July 16, 2014 19
O(1) scheduling algorithm in Linux
2.6 contd..
 All scheduling is done from among tasks in the active
queues structure.
 When the active queues structure is empty, a simple
pointer assignment results in a switch of the active and
expired queues, and scheduling continues.
 On a given processor, the scheduler picks the highest-
priority nonempty queue. If multiple tasks are in that
queue, the tasks are scheduled in round-robin fashion.
July 16, 2014 20
Calculating Priority of a process
 Each non-real-time task is assigned an initial priority in the range of
100 to 139, with a default of 120.This is the task’s static priority
and is specified by the user.
 As the task executes, a dynamic priority is calculated as a function
of the task’s static priority and its execution behavior.
 The Linux scheduler is designed to favor I/O-bound tasks over
processor-bound tasks. This preference tends to provide good
interactive response.
 The technique used by Linux to determine the dynamic priority is to
keep a running tab on how much time a process sleeps (waiting for
an event) versus how much time the process runs.
 In essence, a task that spends most of its time sleeping is given a
higher priority.
July 16, 2014 21
Calculating Static Priority of a process
from Nice value
 nice() or setpriority() system calls
 Changes the static priority of the process
 /*Convert user-nice values [ -20 ... 0 ... 19 ] to
static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
and back */
 #define NICE_TO_PRIO(nice)
(MAX_RT_PRIO + (nice) + 20)
 #define PRIO_TO_NICE(prio)
((prio) - MAX_RT_PRIO - 20)
July 16, 2014 22
Calculating Time slice of a process
 Time slices are assigned in the range of 10 ms to
200 ms.
 In general, higher priority tasks are assigned
larger time slices.
 A new process will always inherit the static priority
of its parent.
 User can change the static priority of the process
by nice() or setpriority() system calls
July 16, 2014 23
 Static priority determines the base time quantum of
a process
 Base time quantum (in milliseconds)
 (140 – static priority) X 20 if static priority <120
 (140 – static priority) X 5 if static priority >=120
 The higher the static priority (lower value), the longer the
base time quantum
 Example
Description static priority base time quantum
Highest static priority 100 800ms
Default static priority 120 100ms
Lowest static priority 139 5ms
July 16, 2014 24
Calculating Time slice from static priority
Dynamic priority
 In Linux, process priority is dynamic.
 The scheduler keeps track of what processes
are doing and adjusts their priorities periodically.
 DP is recalculated when q expires and process
is moved to expired array, also when process
sleeps
 In this way, processes that have been denied
the use of the CPU for a long time interval are
boosted by dynamically increasing their priority.
 Correspondingly, processes running for a long
time are penalized by decreasing their priority.
July 16, 2014 25
July 16, 2014 26
Average Sleep Time Bonus Granularity
Greater than or equal to 0 but smaller than 100ms 0 5120
Greater than or equal to 100ms but smaller than 200ms 1 2560
Greater than or equal to 200ms but smaller than 300ms 2 1280
Greater than or equal to 300ms but smaller than 400ms 3 640
Greater than or equal to 400ms but smaller than 500ms 4 320
Greater than or equal to 500ms but smaller than 600ms 5 160
Greater than or equal to 600ms but smaller than 700ms 6 80
Greater than or equal to 700ms but smaller than 800ms 7 40
Greater than or equal to 800ms but smaller than 900ms 8 20
Greater than or equal to 900ms but smaller than 1000ms 9 10
1 second 10 10
Calculating Dynamic Priority based on avg sleep time
 Dynamic priority value ranges from 100 (highest
priority) to 139 (lowest priority)
 Dynamic priority is the one scheduler looks at when
selecting a new process to run
 Dynamic priority =
max (100, min(static priority – bonus + 5, 139))
 Bonus is ranging from 0 to 10
 Less than 5 (is a penalty that) lowers dynamic priority
 Greater than 5 raises dynamic priority
 Value of bonus depends on the past history of the
process (related to average sleep time of the
process)
 Average sleep time reduces when process is running
 Average sleep can never become larger than 1 second
 Average sleep time increases when process is in I/O
July 16, 2014 27
Relationship to Real-Time Tasks
The following considerations apply:
1. All real-time tasks have only a static priority;
no dynamic priority changes are made.
2. SCHED_FIFO tasks do not have assigned timeslices.
Such tasks are scheduled in FIFO discipline.
If a SCHED_FIFO task is blocked, it returns to the same priority
queue in the active queue list when it becomes unblocked.
3. Although SCHED_RR tasks do have assigned timeslices, they
also are never moved to the expired queue list.
 When a SCHED_RR task exhaust its timeslice, it is returned to its
priority queue with the same timeslice value.
 Timeslice values are never changed.
 The effect of these rules is that the switch between the active queue
list and the expired queue list only happens when there are no
ready real-time tasks waiting to execute.
July 16, 2014 28
 A real time process is replaced by another only
when one of the following events occur
 Process is preempted by a higher real time priority
process
 Process performs blocking operation (TASK_
INTERRUPTABLE or TASK_UNINTERRUPTABLE)
 Process is stopped (TASK_STOPPED or
TASK_TRACED)
 Process is killed (EXIT_ZOMBIE or EXIT_DEAD)
 Process voluntarily relinquishes the CPU by invoking
sched_yield() system call
 The process is Round Robin real time (SCHED_RR)
and it has exhausted its time quantum
July 16, 2014 29
System Calls Related to Scheduling
 nice( ) Change the priority of a conventional process.
 getpriority( ) Get the maximum priority of a group of conventional processes.
 setpriority( ) Set the priority of a group of conventional processes.
 sched_getscheduler( ) Get the scheduling policy of a process.
 sched_setscheduler( ) Set the scheduling policy and priority of a process.
 sched_getparam( ) Get the scheduling priority of a process.
 sched_setparam( ) Set the priority of a process.
 sched_yield( ) Relinquish the processor voluntarily without blocking.
 sched_get_ priority_min( ) Get the minimum priority value for a policy.
 sched_get_ priority_max( ) Get the maximum priority value for a policy.
 sched_rr_get_interval( ) Get the time quantum value for the Round Robin
policy.
July 16, 2014 30
Exercise
 A Linux OS supporting O(1) Scheduler has this snapshot at time t. Assume that the
order of arrival of processes to the system is Process A to Process D. Process
A and B are Conventional processes and Process C and D are Real Time
Processes.
 Find Static Priority for all the processes.
 Find Quantum time for all the processes.
 Find Dynamic priority for all Conventional processes.
 Find the resultant schedule and represent it as Gantt chart
July 16, 2014 31
Process Nice Execution
Time(ms)
Sleep
time(ms)
Real Time
Priority
Sched policy
A 8 50 0 - SCHED_OTHER
B -15 800 0 - SCHED_OTHER
C -5 600 - 30 SCHED_RR
D 0 100 - 30 SCHED_FCFS

More Related Content

What's hot

Introduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed ComputingIntroduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed ComputingSayed Chhattan Shah
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)Prakhar Maurya
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.Ravi Kumar Patel
 
Multiple processor (ppt 2010)
Multiple processor (ppt 2010)Multiple processor (ppt 2010)
Multiple processor (ppt 2010)Arth Ramada
 
Priority Scheduling
Priority Scheduling  Priority Scheduling
Priority Scheduling JawadHaider36
 
Parallel computing
Parallel computingParallel computing
Parallel computingVinay Gupta
 
Cache coherence
Cache coherenceCache coherence
Cache coherenceEmployee
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlockstech2click
 
services and system calls of operating system
services and system calls of operating system services and system calls of operating system
services and system calls of operating system Saurabh Soni
 
Parallel computing chapter 3
Parallel computing chapter 3Parallel computing chapter 3
Parallel computing chapter 3Md. Mahedi Mahfuj
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process ConceptsMukesh Chinta
 
Parallel Processors (SIMD)
Parallel Processors (SIMD) Parallel Processors (SIMD)
Parallel Processors (SIMD) Ali Raza
 

What's hot (20)

Cs8493 unit 1
Cs8493 unit 1Cs8493 unit 1
Cs8493 unit 1
 
Introduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed ComputingIntroduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed Computing
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Multiple processor (ppt 2010)
Multiple processor (ppt 2010)Multiple processor (ppt 2010)
Multiple processor (ppt 2010)
 
Priority Scheduling
Priority Scheduling  Priority Scheduling
Priority Scheduling
 
Parallel computing
Parallel computingParallel computing
Parallel computing
 
An Introduction to Operating Systems
An Introduction to Operating SystemsAn Introduction to Operating Systems
An Introduction to Operating Systems
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Superscalar Processor
Superscalar ProcessorSuperscalar Processor
Superscalar Processor
 
Memory management
Memory managementMemory management
Memory management
 
Cache coherence
Cache coherenceCache coherence
Cache coherence
 
kernels
 kernels kernels
kernels
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
services and system calls of operating system
services and system calls of operating system services and system calls of operating system
services and system calls of operating system
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Parallel computing chapter 3
Parallel computing chapter 3Parallel computing chapter 3
Parallel computing chapter 3
 
Parallel computing persentation
Parallel computing persentationParallel computing persentation
Parallel computing persentation
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
Parallel Processors (SIMD)
Parallel Processors (SIMD) Parallel Processors (SIMD)
Parallel Processors (SIMD)
 

Similar to cpu scheduling in os

Window scheduling algorithm
Window scheduling algorithmWindow scheduling algorithm
Window scheduling algorithmBinal Parekh
 
Aman 16 os sheduling algorithm methods.pptx
Aman 16 os sheduling algorithm methods.pptxAman 16 os sheduling algorithm methods.pptx
Aman 16 os sheduling algorithm methods.pptxvikramkagitapu
 
Operating System Scheduling
Operating System SchedulingOperating System Scheduling
Operating System SchedulingVishnu Prasad
 
Windows process scheduling presentation
Windows process scheduling presentationWindows process scheduling presentation
Windows process scheduling presentationTalha Shaikh
 
Process scheduling
Process schedulingProcess scheduling
Process schedulingHao-Ran Liu
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web ServersDavid Evans
 
Windows process-scheduling
Windows process-schedulingWindows process-scheduling
Windows process-schedulingTalha Shaikh
 
Rate.docx
Rate.docxRate.docx
Rate.docxkradha5
 
chapter 5 CPU scheduling.ppt
chapter  5 CPU scheduling.pptchapter  5 CPU scheduling.ppt
chapter 5 CPU scheduling.pptKeyreSebre
 
CPU scheduling in Operating System Explanation
CPU scheduling in Operating System ExplanationCPU scheduling in Operating System Explanation
CPU scheduling in Operating System ExplanationAnitaSofiaKeyser
 
Unit iios process scheduling and synchronization
Unit iios process scheduling and synchronizationUnit iios process scheduling and synchronization
Unit iios process scheduling and synchronizationdonny101
 
Scheduling algorithm (chammu)
Scheduling algorithm (chammu)Scheduling algorithm (chammu)
Scheduling algorithm (chammu)Nagarajan
 
Scheduling algo(by HJ)
Scheduling algo(by HJ)Scheduling algo(by HJ)
Scheduling algo(by HJ)Harshit Jain
 
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&corbing9ttj
 
Cpu scheduling final
Cpu scheduling finalCpu scheduling final
Cpu scheduling finalmarangburu42
 
In computing, scheduling is the action .
In computing, scheduling is the action .In computing, scheduling is the action .
In computing, scheduling is the action .nathansel1
 
Process scheduling in Light weight weight and Heavy weight processes.
Process scheduling in Light weight weight and Heavy weight processes.Process scheduling in Light weight weight and Heavy weight processes.
Process scheduling in Light weight weight and Heavy weight processes.Shreya Kumar
 
Ch6
Ch6Ch6
Ch6C.U
 

Similar to cpu scheduling in os (20)

Window scheduling algorithm
Window scheduling algorithmWindow scheduling algorithm
Window scheduling algorithm
 
Aman 16 os sheduling algorithm methods.pptx
Aman 16 os sheduling algorithm methods.pptxAman 16 os sheduling algorithm methods.pptx
Aman 16 os sheduling algorithm methods.pptx
 
Operating System Scheduling
Operating System SchedulingOperating System Scheduling
Operating System Scheduling
 
Windows process scheduling presentation
Windows process scheduling presentationWindows process scheduling presentation
Windows process scheduling presentation
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
 
Windows process-scheduling
Windows process-schedulingWindows process-scheduling
Windows process-scheduling
 
Rate.docx
Rate.docxRate.docx
Rate.docx
 
chapter 5 CPU scheduling.ppt
chapter  5 CPU scheduling.pptchapter  5 CPU scheduling.ppt
chapter 5 CPU scheduling.ppt
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
CPU scheduling in Operating System Explanation
CPU scheduling in Operating System ExplanationCPU scheduling in Operating System Explanation
CPU scheduling in Operating System Explanation
 
Unit iios process scheduling and synchronization
Unit iios process scheduling and synchronizationUnit iios process scheduling and synchronization
Unit iios process scheduling and synchronization
 
Scheduling algorithm (chammu)
Scheduling algorithm (chammu)Scheduling algorithm (chammu)
Scheduling algorithm (chammu)
 
Scheduling algo(by HJ)
Scheduling algo(by HJ)Scheduling algo(by HJ)
Scheduling algo(by HJ)
 
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
 
Cpu scheduling final
Cpu scheduling finalCpu scheduling final
Cpu scheduling final
 
In computing, scheduling is the action .
In computing, scheduling is the action .In computing, scheduling is the action .
In computing, scheduling is the action .
 
Process scheduling in Light weight weight and Heavy weight processes.
Process scheduling in Light weight weight and Heavy weight processes.Process scheduling in Light weight weight and Heavy weight processes.
Process scheduling in Light weight weight and Heavy weight processes.
 
Ch6
Ch6Ch6
Ch6
 
Cp usched 2
Cp usched  2Cp usched  2
Cp usched 2
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 

cpu scheduling in os

  • 1. BITS, PILANI – K. K. BIRLA GOA CAMPUS Operating Systems (CS C372 & IS C362) by Mrs. Shubhangi Gawali Dept. of CS and IS 1July 16, 2014
  • 2. OPERATING SYSTEMS (CS C372 & IS C362) LECTURE 16: CPU SCHEDULING
  • 4. Linux 2.4 scheduler - goals  Linux 2.4 scheduler provided real-time scheduling capability coupled with a scheduler for non-real-time processes  Good interactive performance even during high load  If the user types or clicks then the system must react instantly and must execute the user tasks smoothly, even during considerable background load.  Fairness  No process should stay without any timeslice for any unreasonable amount of time.  No process should get an unjustly high amount of CPU time. July 16, 2014 4
  • 5. Linux 2.4 scheduler - goals  Supports SMP: single runqueue, individual scheduler  SMP efficiency  No CPU should stay idle if there is work to do.  SMP affinity  Processes which run on one CPU should stay affine to that CPU.  Processes should not bounce between CPUs too frequently. July 16, 2014 5
  • 6. The three Linux 2.4 scheduling classes  RT process is scheduled according to one of the following classes  SCHED_FIFO: First-in-first-out real-time threads  SCHED_RR: Round-robin real-time threads  Non RT process (Conventional time shared process)  SCHED_OTHER: Other, non-real-time threads  Within each class, multiple priorities may be used, with priorities in the real-time classes higher than the priorities for the SCHED_OTHER class. July 16, 2014 6
  • 7. SCHED_FIFO scheduling class For FIFO threads, the following rules apply: 1. The system will not interrupt an executing FIFO thread except in the following cases: a. Another FIFO thread of higher priority becomes ready. b. The executing FIFO thread is blocked for an I/O. c. The executing FIFO thread voluntarily gives up the processor. 2. When an executing FIFO thread is interrupted, it is placed in the queue associated with its priority. 3. If more than one thread has that highest priority, the thread that has been waiting the longest is chosen. July 16, 2014 7
  • 8. SCHED_RR scheduling class  The SCHED_RR policy is similar to the SCHED_FIFO policy, except for the addition of a timeslice associated with each thread.  When a SCHED_RR thread has executed for its timeslice, it is suspended and a real-time thread of equal or higher priority is selected for running. July 16, 2014 8
  • 9. Real time scheduling example in Linux 2.4 July 16, 2014 9
  • 10. Drawbacks of Linux 2.4 scheduler :  Scheduler for the SCHED_OTHER class did not scale well with increasing number of processors and increasing number of processes.  The scheduler uses a single runqueue for all processors in a symmetric multiprocessing system (SMP) (good for load balancing but bad for memory caches)  Uses a single runqueue lock.  Preemption is not possible. July 16, 2014 10
  • 12. Evolution of Linux schedulers Linux 2.4 scheduler Linux 2.6 O(1) scheduler SMP with Single run queue, lacks scalability SMP with individual run queue Single run queue Two queues (active and expiry) Selection takes O(N) time as it iterates over every process Selection take constant time (O(1)) as from priority queue, dequeue the next from active runqueue Inefficient and weak for real time system Much more efficient and much more scalable. Priority of the process is fixed (Static priority) Priority changes as per cpu usage. Static and dynamic priority. Incorporated interactivity metrics with numerous heuristics to determine whether the process was I/O bound or processor bound July 16, 2014 12
  • 13. O(1) scheduler in Linux 2.6  To correct these problems, Linux 2.6 uses the O(1) scheduler.  The scheduler is designed so that the time to select the appropriate process and assign it to a processor is constant,  regardless of the load on the system or the number of processors. July 16, 2014 13
  • 14. O(1) scheduler in Linux 2.6  Preemptive priority based round robin scheduler  Two separate priority ranges:  Real time range : 0 to 99  Nice value range: 100 to 139  These two ranges map into global priority scheme.  Higher priority task : larger time quanta  Lower priority task : smaller time quanta July 16, 2014 14
  • 15. The Relationship Between Priorities and Time-slice length July 16, 2014 15 139
  • 16. Data structure  The kernel maintains two scheduling data structure for each processor in the system, of the following form struct prio_array { int nr_active; /* number of tasks in this array*/ unsigned long bitmap[BITMAP_SIZE]; /* priority bitmap */ struct list_head queue[MAX_PRIO]; /* priority queues */  Two such structures are maintained:  an active queues structure and  an expired queues structure.  A separate queue is maintained for each priority level. July 16, 2014 16
  • 17. List of Tasks Indexed According to Prorities July 16, 2014 17 [139 ] [139 ]
  • 19. O(1) scheduling algorithm in Linux 2.6 Initially, both bitmaps are set to all zeroes and all queues are empty.  As a process becomes ready, it is assigned to the appropriate priority queue in the active queues structure and is assigned the appropriate timeslice.  Possibilities:  If the running process is preempted before its timeslice, it is returned to an active queue.  When a task completes its timeslice, it goes into the appropriate queue in the expired queues structure and is assigned a new timeslice.  If the running process goes for I/O , priority (dynamic) is recomputed and the process is added to the appropriate active queue. July 16, 2014 19
  • 20. O(1) scheduling algorithm in Linux 2.6 contd..  All scheduling is done from among tasks in the active queues structure.  When the active queues structure is empty, a simple pointer assignment results in a switch of the active and expired queues, and scheduling continues.  On a given processor, the scheduler picks the highest- priority nonempty queue. If multiple tasks are in that queue, the tasks are scheduled in round-robin fashion. July 16, 2014 20
  • 21. Calculating Priority of a process  Each non-real-time task is assigned an initial priority in the range of 100 to 139, with a default of 120.This is the task’s static priority and is specified by the user.  As the task executes, a dynamic priority is calculated as a function of the task’s static priority and its execution behavior.  The Linux scheduler is designed to favor I/O-bound tasks over processor-bound tasks. This preference tends to provide good interactive response.  The technique used by Linux to determine the dynamic priority is to keep a running tab on how much time a process sleeps (waiting for an event) versus how much time the process runs.  In essence, a task that spends most of its time sleeping is given a higher priority. July 16, 2014 21
  • 22. Calculating Static Priority of a process from Nice value  nice() or setpriority() system calls  Changes the static priority of the process  /*Convert user-nice values [ -20 ... 0 ... 19 ] to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], and back */  #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)  #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) July 16, 2014 22
  • 23. Calculating Time slice of a process  Time slices are assigned in the range of 10 ms to 200 ms.  In general, higher priority tasks are assigned larger time slices.  A new process will always inherit the static priority of its parent.  User can change the static priority of the process by nice() or setpriority() system calls July 16, 2014 23
  • 24.  Static priority determines the base time quantum of a process  Base time quantum (in milliseconds)  (140 – static priority) X 20 if static priority <120  (140 – static priority) X 5 if static priority >=120  The higher the static priority (lower value), the longer the base time quantum  Example Description static priority base time quantum Highest static priority 100 800ms Default static priority 120 100ms Lowest static priority 139 5ms July 16, 2014 24 Calculating Time slice from static priority
  • 25. Dynamic priority  In Linux, process priority is dynamic.  The scheduler keeps track of what processes are doing and adjusts their priorities periodically.  DP is recalculated when q expires and process is moved to expired array, also when process sleeps  In this way, processes that have been denied the use of the CPU for a long time interval are boosted by dynamically increasing their priority.  Correspondingly, processes running for a long time are penalized by decreasing their priority. July 16, 2014 25
  • 26. July 16, 2014 26 Average Sleep Time Bonus Granularity Greater than or equal to 0 but smaller than 100ms 0 5120 Greater than or equal to 100ms but smaller than 200ms 1 2560 Greater than or equal to 200ms but smaller than 300ms 2 1280 Greater than or equal to 300ms but smaller than 400ms 3 640 Greater than or equal to 400ms but smaller than 500ms 4 320 Greater than or equal to 500ms but smaller than 600ms 5 160 Greater than or equal to 600ms but smaller than 700ms 6 80 Greater than or equal to 700ms but smaller than 800ms 7 40 Greater than or equal to 800ms but smaller than 900ms 8 20 Greater than or equal to 900ms but smaller than 1000ms 9 10 1 second 10 10
  • 27. Calculating Dynamic Priority based on avg sleep time  Dynamic priority value ranges from 100 (highest priority) to 139 (lowest priority)  Dynamic priority is the one scheduler looks at when selecting a new process to run  Dynamic priority = max (100, min(static priority – bonus + 5, 139))  Bonus is ranging from 0 to 10  Less than 5 (is a penalty that) lowers dynamic priority  Greater than 5 raises dynamic priority  Value of bonus depends on the past history of the process (related to average sleep time of the process)  Average sleep time reduces when process is running  Average sleep can never become larger than 1 second  Average sleep time increases when process is in I/O July 16, 2014 27
  • 28. Relationship to Real-Time Tasks The following considerations apply: 1. All real-time tasks have only a static priority; no dynamic priority changes are made. 2. SCHED_FIFO tasks do not have assigned timeslices. Such tasks are scheduled in FIFO discipline. If a SCHED_FIFO task is blocked, it returns to the same priority queue in the active queue list when it becomes unblocked. 3. Although SCHED_RR tasks do have assigned timeslices, they also are never moved to the expired queue list.  When a SCHED_RR task exhaust its timeslice, it is returned to its priority queue with the same timeslice value.  Timeslice values are never changed.  The effect of these rules is that the switch between the active queue list and the expired queue list only happens when there are no ready real-time tasks waiting to execute. July 16, 2014 28
  • 29.  A real time process is replaced by another only when one of the following events occur  Process is preempted by a higher real time priority process  Process performs blocking operation (TASK_ INTERRUPTABLE or TASK_UNINTERRUPTABLE)  Process is stopped (TASK_STOPPED or TASK_TRACED)  Process is killed (EXIT_ZOMBIE or EXIT_DEAD)  Process voluntarily relinquishes the CPU by invoking sched_yield() system call  The process is Round Robin real time (SCHED_RR) and it has exhausted its time quantum July 16, 2014 29
  • 30. System Calls Related to Scheduling  nice( ) Change the priority of a conventional process.  getpriority( ) Get the maximum priority of a group of conventional processes.  setpriority( ) Set the priority of a group of conventional processes.  sched_getscheduler( ) Get the scheduling policy of a process.  sched_setscheduler( ) Set the scheduling policy and priority of a process.  sched_getparam( ) Get the scheduling priority of a process.  sched_setparam( ) Set the priority of a process.  sched_yield( ) Relinquish the processor voluntarily without blocking.  sched_get_ priority_min( ) Get the minimum priority value for a policy.  sched_get_ priority_max( ) Get the maximum priority value for a policy.  sched_rr_get_interval( ) Get the time quantum value for the Round Robin policy. July 16, 2014 30
  • 31. Exercise  A Linux OS supporting O(1) Scheduler has this snapshot at time t. Assume that the order of arrival of processes to the system is Process A to Process D. Process A and B are Conventional processes and Process C and D are Real Time Processes.  Find Static Priority for all the processes.  Find Quantum time for all the processes.  Find Dynamic priority for all Conventional processes.  Find the resultant schedule and represent it as Gantt chart July 16, 2014 31 Process Nice Execution Time(ms) Sleep time(ms) Real Time Priority Sched policy A 8 50 0 - SCHED_OTHER B -15 800 0 - SCHED_OTHER C -5 600 - 30 SCHED_RR D 0 100 - 30 SCHED_FCFS

Editor's Notes

  1. Linux provided a real-time scheduling capability coupled with a scheduler for non-real-time processes that made use of the traditional UNIX scheduling algorithm.
  2. The Linux 2.4 scheduler uses a single runqueue lock.Thus, in an SMP system, the act of choosing a task to execute locks out any other processor from manipulating the runqueues. The result is idle processors awaiting release of the runqueue lock and decreased efficiency. • Preemption is not possible in the Linux 2.4 scheduler; this means that a lower-priority task can execute while a higher-priority task waited for it to complete.
  3. The kernel maintains two scheduling data structure for each processor in the system, of the following form The total number of queues in the structure is MAX_PRIO, which has a default value of 140. The structure also includes a bitmap array of sufficient size to provide one bit per priority level. The bitmap indicates which queues are not empty. nr_active indicates the total number of tasks present on all queues.
  4. Linux also includes a mechanism for moving tasks from the queue lists of one processor to that of another. Periodically, the scheduler checks to see if there is a substantial imbalance among the number of tasks assigned to each processor. To balance the load, the schedule can transfer some tasks.The highest priority active tasks are selected for transfer, because it is more important to distribute highpriority tasks fairly.
  5. Real-time tasks are handled in a different manner from non-real-time tasks in the priority queues.