SlideShare a Scribd company logo
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

PDF
eBPF - Rethinking the Linux Kernel
PPT
Basic Linux Internals
PDF
Linux File System
PDF
PPTX
Fuse- Filesystem in User space
PPTX
eBPF Basics
PDF
Linux scheduler
PPTX
Introduction to Unix
eBPF - Rethinking the Linux Kernel
Basic Linux Internals
Linux File System
Fuse- Filesystem in User space
eBPF Basics
Linux scheduler
Introduction to Unix

What's hot (20)

PDF
Linux Kernel - Virtual File System
PDF
IntelON 2021 Processor Benchmarking
DOC
Lesson 2 History Of Operating System
PDF
The Linux Block Layer - Built for Fast Storage
PPTX
eMMC 5.0 Total IP Solution
PDF
LCU13: An Introduction to ARM Trusted Firmware
PPT
Linux presentation
PPT
Interprocess communication (IPC) IN O.S
PPT
A Quick Introduction to Linux
PPT
PPTX
Process management in linux
PPT
Linux architecture
PDF
RTOS - Real Time Operating Systems
PDF
NFS(Network File System)
PDF
LCU14 500 ARM Trusted Firmware
PPTX
Cpu scheduling in operating System.
PPT
cpu scheduling OS
PPT
Ipc in linux
PPT
Linux - Introductions to Linux Operating System
PDF
Part 01 Linux Kernel Compilation (Ubuntu)
Linux Kernel - Virtual File System
IntelON 2021 Processor Benchmarking
Lesson 2 History Of Operating System
The Linux Block Layer - Built for Fast Storage
eMMC 5.0 Total IP Solution
LCU13: An Introduction to ARM Trusted Firmware
Linux presentation
Interprocess communication (IPC) IN O.S
A Quick Introduction to Linux
Process management in linux
Linux architecture
RTOS - Real Time Operating Systems
NFS(Network File System)
LCU14 500 ARM Trusted Firmware
Cpu scheduling in operating System.
cpu scheduling OS
Ipc in linux
Linux - Introductions to Linux Operating System
Part 01 Linux Kernel Compilation (Ubuntu)
Ad

Similar to cpu scheduling in os (20)

PPTX
Scheduling Algorithms unit IV(II).pptx -
PPT
Window scheduling algorithm
PPTX
Aman 16 os sheduling algorithm methods.pptx
PPT
Scheduling.ppt with operating system slides
PPT
Operating System Scheduling
PPTX
Windows process scheduling presentation
PPTX
Process scheduling
PPTX
Scheduling in Linux and Web Servers
PPTX
Windows process-scheduling
DOCX
Rate.docx
PPT
chapter 5 CPU scheduling.ppt
PPT
CPU Scheduling
PPT
CPU scheduling in Operating System Explanation
PDF
Unit iios process scheduling and synchronization
PPT
Scheduling algorithm (chammu)
PPTX
Scheduling algo(by HJ)
DOCX
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
DOCX
Cpu scheduling final
PPTX
In computing, scheduling is the action .
PPTX
Process scheduling in Light weight weight and Heavy weight processes.
Scheduling Algorithms unit IV(II).pptx -
Window scheduling algorithm
Aman 16 os sheduling algorithm methods.pptx
Scheduling.ppt with operating system slides
Operating System Scheduling
Windows process scheduling presentation
Process scheduling
Scheduling in Linux and Web Servers
Windows process-scheduling
Rate.docx
chapter 5 CPU scheduling.ppt
CPU Scheduling
CPU scheduling in Operating System Explanation
Unit iios process scheduling and synchronization
Scheduling algorithm (chammu)
Scheduling algo(by HJ)
Uniprocessor SchedulingCsci 430, Spring 2018Texas A&
Cpu scheduling final
In computing, scheduling is the action .
Process scheduling in Light weight weight and Heavy weight processes.
Ad

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Introduction to Artificial Intelligence
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
L1 - Introduction to python Backend.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
AI in Product Development-omnex systems
PPTX
Transform Your Business with a Software ERP System
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
history of c programming in notes for students .pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Essential Infomation Tech presentation.pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Navsoft: AI-Powered Business Solutions & Custom Software Development
wealthsignaloriginal-com-DS-text-... (1).pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Design an Analysis of Algorithms I-SECS-1021-03
Introduction to Artificial Intelligence
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
L1 - Introduction to python Backend.pptx
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
AI in Product Development-omnex systems
Transform Your Business with a Software ERP System
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
history of c programming in notes for students .pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo POS Development Services by CandidRoot Solutions
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Essential Infomation Tech presentation.pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...

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

  • #5: 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.
  • #11: 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.
  • #17: 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.
  • #21: 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.
  • #29: Real-time tasks are handled in a different manner from non-real-time tasks in the priority queues.