Sei sulla pagina 1di 147

Silberschatz, Galvin and Gagne 2009

Operating System Concepts 8


th
Edition,
CPU Scheduling
Modified by M.Rebaudengo - 2010
5.2
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Schedulers
! Process migrates among several queues
" Device queue, job queue, ready queue
! Scheduler selects a process to run from these queues
! Long-term scheduler:
" load a job in memory
" Runs infrequently
! Short-term scheduler:
" Select ready process to run on CPU
" Should be fast
! Middle-term scheduler (or swapper)
" Reduce multiprogramming or memory consumption
5.3
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Classification of Scheduling Activity
! Long-term: which process to admit?
! Medium-term: which process to swap in or out?
! Short-term: which ready process to execute next?
5.4
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Queuing Diagram for Scheduling
5.5
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Long-Term Scheduling
! Determines which programs are admitted to the system for
processing
! Controls the degree of multiprogramming
! Attempts to keep a balanced mix of processor-bound and I/O-
bound processes
" CPU usage
" System performance
5.6
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Medium-Term Scheduling
! Makes swapping decisions based on the current degree of
multiprogramming
" Controls which remains resident in memory and which jobs must be
swapped out to reduce degree of multiprogramming
5.7
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Short-Term Scheduling
! Selects from among ready processes in memory which one is
to execute next
" The selected process is allocated the CPU
! It is invoked on events that may lead to choose another process
for execution:
" Clock interrupts
" I/O interrupts
" Operating system calls and traps
" Signals

5.8
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
The Scheduler
! Programs execute concurrently
! Scheduler determines which thread executes next
! Scheduling policy determines
" when thread is removed from CPU
" which thread allocated the CPU next
! Scheduling mechanism determines
" how the process manager can determine that it is time to multiplex the
CPU
" how a thread can be allocated to/removed from the CPU
5.9
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
The Scheduler
! Scheduling mechanism
" Provides the tools and environment for controlling the ow of the thread
through the various queues
! Context Switching between Processes
! Queueing Capabilities
! Scheduling policy
" Denes which ready thread the scheduler chooses from the ready list
" Uses the scheduling mechanism
5.10
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Basic Concepts
! Processes require alternate use of processor and I/O in a repetitive
fashion
! Each cycle consist of a CPU burst followed by an I/O burst
! CPU-bound processes have longer CPU bursts than I/O-bound
processes
5.11
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
CPU and I/O bound processes
! Bursts of CPU usage alternate with periods of I/O wait
" a CPU-bound process
" an I/O bound process
5.12
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Histogram of CPU-burst Times
! The distribution of CPU cycle times shows a greater number of jobs
requesting short CPU cycles, and fewer jobs requesting long CPU cycles.
5.13
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Alternating Sequence of CPU And I/O Bursts
5.14
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example
{
printf("Enter the first integer: \n");
scanf("%d", &a);
printf("Enter the second integer: \n");
scanf("%d", &a);

c = a+b;
d = (a*b) c ;
e = a b;
f = d/e;

printf("\n a+b = %d", c);
printf("\n (a*b) c = %d", d);
printf("\n a - b = %d", e);
printf("\n d/e = %d", f);
}

I/O Cycle
CPU Cycle
I/O Cycle
5.15
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Remember
timeout
5.16
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Mechanisms
! Software components
" Enqueuer
! Places a pointer to the process descriptor in the ready list
! places the process into Ready state and decides its priority
" Context switcher
! Saves the contents of all CPU registers and other state
! Loads the state of the dispatcher
" Dispatcher
! Selects a thread from the ready list and allocates the CPU to it by
performing a context switch to it
5.17
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
The Scheduler
Ready Process
Enqueuer
Ready
List
Dispatcher
Context
Switcher
Process
Descriptor
CPU
From
Other
States
Running Process
5.18
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process/Thread Context
! Context Switch
" Saves all current registers (general purpose and status)
" Loads all registers for new process
" Context saved in a processs descriptor.
5.19
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process/Thread Context
R1
R2
Rn
. . .
Status
Registers
Functional Unit
Left Operand
Right Operand
Result
ALU
PC
IR
Ctl Unit
5.20
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Context Switching
CPU
New Thread Descriptor
Old Thread
Descriptor
5.21
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Mechanisms
! Context switching: time
" Save all registers
! 32 or more 32- or 64-bit registers + status registers
! Uses load/store operations
! Takes (n + m) b x K time units
n general registers, m status registers
b store operations required
Each store requires K time units

5.22
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Mechanisms
! Context switching: time
" Process requires up to 50 nanoseconds to store 1 unit of information (b
x K = 50ns)
" Assume 32-bit bus between processor and memory, each register is 32
bits wide
! Each register requires 50ns to store its contents
! If n = 32 and m = 8
! Total time to save is 40 x 50 = 2 microseconds
" Another 2 microseconds needed to restore another threads context
" If dispatcher runs as well, additional time is necessary
5.23
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Mechanisms
! Context switching: time
" A 1GHz machine can run a regular instruction (no memory access) in
about 2 ns
" During the 4 microseconds of context switch, CPU could have executed
2,000 instructions
5.24
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Performance
! Time to run a process depends on
" CPU time needed by process
" I/O time needed by process
" Context switch time (signicant)
" Scheduler choices
! The longer you wait in the ready list, the longer your process takes
to execute
! If you never get chosen, you starve
5.25
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Choosing a Process to Run
! Mechanism never changes
! Strategy = policy the dispatcher uses to select a process from the
ready list
! Different policies for different requirements
Ready Process
Enqueue
Ready
List
Dispatch
Context
Switch
Process
Descriptor
CPU
Running Process
5.26
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
CPU Scheduling (cont.)
! CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state (e.g., I/O request)
2. Switches from running to ready state (e.g., the CPU receives an
interrupt)
3. Switches from waiting to ready (e.g., completion of an I/O operation)
4. Terminates
! Scheduling under 1 and 4 is nonpreemptive
! All other scheduling is preemptive

5.27
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Preemptive vs. nonpreemptive scheduling
! Many CPU scheduling algorithms have both preemptive and nonpreemptive
versions:
" Preemptive:
! schedule a new process even when the current process does not
intend to give up the CPU
! Currently running process may be interrupted and moved to the
Ready state by the OS
! Prevents one process from monopolizing the processor
" Non-preemptive:
! Once a process is in the running state, it will continue until it
terminates or blocks for an I/O
! only schedule a new process when the current one does not want
CPU any more.
5.28
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Dispatcher
! The dispatcher is the module that gives control of the CPU to the
process selected by the short-term scheduler
! Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this involves:
" switching context
" switching to user mode
" jumping to the proper location in the user program to restart
that program
! Dispatch latency time it takes for the dispatcher to stop one
process and start another running
5.29
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Criteria
! CPU utilization percentage of the time that CPU is busy
! Throughput number of completed processes per time unit
! Turnaround time the interval from the time of submission of a
process to the time of completion
! Waiting time the sum of the periods spent waiting in the ready
queue
! Response time amount of time it takes from when a request was
submitted until the rst response is produced (for time-sharing
environment)
! Fairness give everyone an equal amount of CPU time
5.30
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling goals
! All Systems:
" Fairness: giving each process a fair share of the CPU
" Policy enforcement: seeing that stated policy is carried out
" Balance: keeping all parts of the system busy
! Batch Systems:
" Maximize jobs per hour
" Minimize time between submission and termination of a task
" CPU utilization: keep the CPU busy at all times
! Interactive systems:
" Respond to requests quickly
" Meet users expectations (a task that is supposed to take a short time should
nish quickly, and not surprise the user in terms of his/her expectations)
! Real-time systems:
" Meeting deadlines: avoid losing data
" Predictability: avoid quality degradation in multimedia systems
5.31
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Algorithm Optimization Criteria
! Maximize:
" CPU utilization
" throughput
" fairness
! Minimize:
" turnaround time
" waiting time
" response time.
5.32
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Methods for evaluating CPU scheduling algorithms
! Deterministic Modeling
" Take a predetermined workload
" Run the scheduling algorithm manually
" Find out the value of the performance metric that you care about.
5.33
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Deterministic modeling example:
! Suppose we have processes A, B, and C, submitted at time 0
! We want to know the response time, waiting time, and turnaround time of
process A

A B C A B C A C A C
Time
response time = 0
+ +
wait time
turnaround time
Gantt chart: visualize how processes execute.
5.34
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Deterministic modeling example
! Suppose we have processes A, B, and C, submitted at time 0
! We want to know the response time, waiting time, and turnaround time of
process B

A B C A B C A C A C
Time
response time
+
wait time
turnaround time
5.35
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Deterministic modeling example
! Suppose we have processes A, B, and C, submitted at time 0
! We want to know the response time, waiting time, and turnaround time of
process C

A B C A B C A C A C
Time
response time
+ + +
wait time
turnaround time
5.36
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling Policies
! FCFS (rst come, rst served)
! SJF (shortest job rst)
! Priority Scheduling
! Round robin
! Multilevel feedback queues
! Lottery scheduling
! This is obviously an incomplete list....
5.37
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
FCFS (First Come First Served)
! FCFS: assigns the CPU based on the order of requests
" Nonpreemptive: A process keeps running on a CPU until it is blocked
or terminated
" when a process arrives, all in ready list will be processed before this job
" and none of the following processes will be serviced before this process
" The process that currently has the CPU must nish.
+ Simple
- Short jobs can get stuck behind long jobs
- Turnaround time is not ideal
5.38
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P
1
24
P
2
3
P
3


3

! Suppose that the processes arrive in the order: P
1
, P
2
, P
3
The Gantt Chart for the schedule is:
#
#
#
#

! Waiting time for P


1
= 0; P
2
= 24; P
3
= 27
! Average waiting time: (0 + 24 + 27)/3 = 17
! Convoy effect short process behind long process
P
1
P
2
P
3

24 27 30 0
5.39
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
FCFS Scheduling (Cont)
Suppose that the processes arrive in the order
P
2
, P
3
, P
1

! The Gantt chart for the schedule is:

! Waiting time for P


1
= 6;

P
2
= 0
;
P
3
= 3
! Average waiting time: (6 + 0 + 3)/3 = 3
! Much better than previous case.
P
1
P
3
P
2

6 3 30 0
5.40
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Shortest-Job-First (SJF) Scheduling
! SJF runs whatever job puts the least demand on the CPU, also known as
STCF (shortest time to completion rst)
! Associate with each process the length of its next CPU burst. Use these
lengths to schedule the process with the shortest time
+ Probably optimal in terms of turn-around time
+ Gives minimum average waiting time for a given set of processes
+ Great for short jobs
+ Small degradation for long jobs
The difculty is knowing the length of the next CPU request
5.41
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example of SJF
Process Burst Time
P
1
6
P
2
8
P
3
7
P
4
3
! SJF scheduling chart
! Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

P
4

P
3

P
1

3
16 0
9
P
2

24
5.42
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
42
Shortest Job First Scheduling
ProcessArrival Time Burst Time
P
1
0 7
P
2
2 4
P
3
4 1

P
4
5 4

5.43
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
43
Shortest Job First Scheduling
ProcessArrival Time Burst Time
P
1
0 7
P
2
2 4
P
3
4 1

P
4
5 4

P
1
P
3
P
2

7
P
1
(7)
16 0
P
4

8 12
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
2
4 5
P
2
(4)
P
3
(1)
P
4
(4)
P
1
s wating time = 0
P
2
s wating time = 6
P
3
s wating time = 3
P
4
s wating time = 7
5.44
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Shortest Remaining Time First (SRTF)
! SRTF: a preemptive version of SJF
" If a job arrives with a shorter time to completion, SRTF preempts the
CPU for the new job
" Also known as SRTCF (shortest remaining time to completion rst)
5.45
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
45
Shortest Remaining Time First
Process Arrival Time Burst Time
P
1
0 7
P
2
2 4
P
3
4 1

P
4
5 4

P
1
P
3
P
2

4 2
11 0
P
4

5 7
P
2
P
1

16
Average waiting time = (9 + 1 + 0 +2)/4 = 3
P
1
(7)
P
2
(4)
P
3
(1)
P
4
(4)
P
1
s wating time = 9
P
2
s wating time = 1
P
3
s wating time = 0
P
4
s wating time = 2
P
1
(5)
P
2
(2)
5.46
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Turn-around time for SJF
! Let assume that all the processes are available at the same time
! Processes = {T
1
,T
2
, T
3
, ..., T
n
} in order of duration
! Total Turn-around time =
= T
1
+ (T
1
+

T
2
) + (T
1
+

T
2
+

T
3
) + ....+ (T
1
+

T
2
+

. . .

+

T
n
) =
= n * T
1
+ (n-1) * T
2
+ (n-2) * T
3
+ ... + 2* T
n-1
+ T
n
5.47
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Determining Length of Next CPU Burst
! Optimal scheduling
! Preemptive SJF is superior to non preemptive SJF.
! However, there are no accurate estimations to know the length of the next
CPU burst
! Can only estimate the length
! Can be done by using the length of previous CPU bursts, using exponential
averaging
: Define 4.
1 0 , 3.
burst CPU next the for value predicted 2.
burst CPU of length actual 1.
! !
=
=
+
" "
#
1 n
th
n
n t
( ) . 1
1 n n n
t ! " " ! # + =
+
5.48
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Examples of Exponential Averaging
! ! =0
" "
n+1
= "
n
" Recent history does not count
! ! =1
" "
n+1
= t
n
" Only the actual last CPU burst counts.
! Consider t
4
, we get:
"
4
= ! t
3
+(1 - !) "
3
+
= ! t
3
+(1 - !) (! t
2
+(1 - !) "
2
)
= ! t
3
+(1 - !) ! t
2
+ (1 - !)
2
"
2

= ! t
3
+(1 - !) ! t
2
+ (1 - !)
2
(! t
1
+ (1 -!) "
1
)


= ! t
3
+(1 - !) ! t
2
+ (1 - !)
2
! t
1
+ (1 -!)
3
"
1


= ! t
3
+(1 - !) ! t
2
+ (1 - !)
2
! t
1
+ (1 -!)
3
(! t
0
+ (1 -!) "
0
)


= ! t
3
+(1 - !) ! t
2
+ (1 - !)
2
! t
1
+ (1 -!)
3
! t
0
+ (1 -!)
4
"
0

! Since both ! and (1 - !) are less than or equal to 1, each successive term has less weight
than its predecessor.
5.49
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Examples of Exponential Averaging

0
5
10
15
Time
CPU burst 10 6 4 6 4 13 13 13
guess 10 8 6 6 5 9 11 12
0 1 2 3 4 5 6 7
5.50
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Drawbacks of Shortest Job First
- Starvation: constant arrivals of short jobs can keep long ones from running
- There is no way to know the completion time of jobs (most of the time)
" Some solutions
! Ask the user, who may not know any better
! If a user cheats, the job is killed

5.51
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Priority Scheduling
! 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)
" Priority 0:
" Priority 1:
" Priority 2:
A
B
C
A B
Time
C
Priority Scheduling
5.52
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Priority Scheduling
+ Generalization of SJF: SJF is a priority scheduling where priority is the
predicted next CPU burst time
" With SJF, priority = 1/requested_CPU_time
- Problem # Starvation low priority processes may never execute
+ Solution # Aging as time progresses increase the priority of the process
5.53
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
53
Real-time Systems
! On-line transaction systems
! Real-time monitoring systems
! Signal processing systems
" multimedia
! Embedded control systems:
" automotives
" Robots
" Aircrafts
" Medical devices
5.54
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Real-Time Workload
! Job (unit of work)
" a computation, a le read, a message transmission, etc
! Attributes
" Resources required to make progress
" Timing parameters
Released
Absolute
deadline
Relative deadline
Execution time
5.55
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Real-Time Task
! Task : a sequence of similar jobs
" Periodic task (p,e)
! Its jobs repeat regularly
! Period p = inter-release time (0 < p)
! Execution time e = maximum execution time (0 < e < p)
! Utilization U = e/p
5 10 15 0
5.56
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Deadlines: Hard vs. Soft
! Hard deadline
" Disastrous or very serious consequences may occur if the deadline is
missed
" Validation is essential : can all the deadlines be met, even under worst-
case scenario?
" Deterministic guarantees
! Soft deadline
" Ideally, the deadline should be met for maximum performance. The
performance degrades in case of deadline misses.
" Best effort approaches / statistical guarantees
5.57
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Schedulability
! Property indicating whether a real-time system (a set of real-time tasks) can
meet their deadlines
(4,1)
(5,2)
(7,2)
5.58
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Real-Time Scheduling
! Determines the order of real-time task executions
! Static-priority scheduling
! Dynamic-priority scheduling
5.59
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
RM (Rate Monotonic)
! Optimal static-priority scheduling
! It assigns priority according to period
! A task with a shorter period has a higher priority
! Executes a job with the shortest period
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T
1
T
2
T
3
5.60
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
RM (Rate Monotonic)
! Executes a job with the shortest period
(4,1)
(5,2)
(7,2)
Deadline Miss !
5
5
10
10 15
15
T
1
T
2
T
3
5.61
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
RM Utilization Bound
! Real-time system is schedulable under RM if
$U
i
% n (2
1/n
-1)
Liu & Layland,
Scheduling algorithms for multi-programming in a hard-real-time
environment, Journal of ACM, 1973.

5.62
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
RM Utilization Bound
! Real-time system is schedulable under RM if
$U
i
% n (2
1/n
-1)

! Example: T
1
(4,1), T
2
(5,1), T
3
(10,1),
$U
i
= 1/4 + 1/5 + 1/10

= 0.55
3 (2
1/3
-1) & 0.78

Thus, {T
1
, T
2
, T
3
} is schedulable under RM.

5.63
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
RM Utilization Bounds
0.5
0.6
0.7
0.8
0.9
1
1.1
1 4 16 64 256 1024 4096
The Number of Tasks
U
t
i
l
i
z
a
t
i
o
n
RM Utilization Bound
! Real-time system is schedulable under RM if
$U
i
% n (2
1/n
-1)

5.64
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
EDF (Earliest Deadline First)
! Optimal dynamic priority scheduling
! A task with a shorter deadline has a higher priority
! Executes a job with the earliest deadline
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T
1
T
2
T
3
5.65
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
EDF (Earliest Deadline First)
! Executes a job with the earliest deadline
! Optimal scheduling algorithm
" if there is a schedule for a set of real-time tasks,
EDF can schedule it.
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T
1
T
2
T
3
5.66
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
EDF Utilization Bound
! Real-time system is schedulable under EDF if and only if
$U
i
% 1
Liu & Layland,
Scheduling algorithms for multi-programming in a hard-real-time
environment, Journal of ACM, 1973.

5.67
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
! Domino effect during overload conditions
" Example: T
1
(4,3), T
2
(5,3), T
3
(6,3), T
4
(7,3)
EDF Overload Conditions
T
1
5 0 7
T
2
T
3
T
4
3 6
Deadline Miss !
T
1
5 0 7
T
3
3 6
Better schedules :
T
1
5 0 7
T
4
3 6
5.68
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Round Robin
! Goal is an equal distribution of processing time
! If there are n processes, each gets 1/n of the CPU time.
! Round Robin (RR) periodically releases the CPU from long-running jobs
" Based on timer interrupts
" Preemptive: a process can be forced to leave its running state and
replaced by another running process
" Time slice (or time quantum): interval between timer interrupts
! usually 10-100 milliseconds
" Most widely used scheduling algorithm

5.69
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Round Robin (RR)
! After the time slice has elapsed, the process is preempted and
added to the end of the ready queue.
" the ready queue is treated as a circular queue
! If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time in
chunks of at most q time units at once. No process waits more
than (n-1)q time units.
5.70
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
More on Round Robin
! If time slice is too long
" Scheduling degrades to FCFS
! If time slice is too short
" Context switching cost dominates
! Timeslice frequently set to ~100 milliseconds
! Context switches typically cost < 1 millisecond
" Context switch is usually negligible (< 1% per timeslice) otherwise you
context switch too frequently and lose all productivity.
5.71
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
71
! Time Quantum (Q) = 20
Process Burst Time Wait Time
P
1
53 57 +24 = 81
P
2
17 20
P
3
68 37 + 40 + 17= 94
P
4
24 57 + 40 = 97
Round Robin Scheduling
P
1
P
2
P
3
P
4
P
1
P
3
P
4
P
1
P
3
P
3
0 20 37 57 77 97 117 121134 154162
Average wait time = (81+20+94+97)/4 = 73
57
20
37
57
24
40
40
17
P
1
(53)
P
2
(17)
P
3
(68)
P
4
(24)
P
1
(33) P
1
(13)
P
3
(48)
P
3
(28)
P
3
(8)
P
4
(4)
5.72
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Time Quantum and Context Switch Time
5.73
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
FCFS vs. RR (varying Q)

Quantum
Completion
Time
Wait
Time
Average P
4
P
3
P
2
P
1
P
2

[8]
P
4

[24]

P
1

[53]

P
3

[68]

0 8 32 85 153
Best FCFS:
62 57 85 22 84 Q = 1
104! 112 153 28 125 Q = 20
100! 81 153 30 137 Q = 1
66" 88 85 20 72 Q = 20
31" 8 85 0 32 Best FCFS
121# 145 68 153 121 Worst FCFS
69! 32 153 8 85 Best FCFS
83! 121 0 145 68 Worst FCFS
95! 80 153 16 133 Q = 8
57" 56 85 8 80 Q = 8
99! 92 153 18 135 Q = 10
99! 82 153 28 135 Q = 5
61" 68 85 10 82 Q = 10
61" 58 85 20 82 Q = 5
5.74
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Turnaround Time Varies With The Time Quantum
Average turnaround time does not
necessarily decrease with larger time
quanta

5.75
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Round Robin (RR)
! Performance
" Average turnaround time decreases if most processes nish their
next CPU burst in a single time quantum.
" Rule of thumb: 80% of the CPU bursts should be shorter than
the time quantum.
5.76
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
FCFS vs. Round Robin
! Suppose we have three jobs of equal length (3 process of 10 time units
each)
" time quantum = 1 then turnaround time is 29
" time quantum = 10, average turnaround time is 20
A B C A B C
Time
A B C
turnaround time of A
turnaround time of B
turnaround time of C
Round Robin
A B
Time
C
turnaround time of A
turnaround time of B
turnaround time of C
FCFS
5.77
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
FCFS vs. Round Robin
! Round Robin
+ Shorter response time
+ Fair sharing of CPU
- Not all jobs are preemptable
- Not good for jobs of the same length
- Not good in terms of the turnaround time.
5.78
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multi-Level Queues
! All processes of the same priority are placed into the same queue
! Scheduler uses one strategy to allocate processor time to each pool
! Uses a different strategy for all the processes in the same pool
5.79
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multi-Level Queues
! Example:
" Use priority scheduling among pools
! Then all processes in pool i are run before any process in pool j if i <
j (and lower numbers mean higher priority)
" Use round robin within a pool
5.80
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multi-Level Queues
! Example:
" Could give each pool 2
-j
of the CPU time where j is the priority
! During 100 seconds priority 1 pool gets 50 seconds
! Priority 2 pool gets 25 seconds
! Priority 3 pool gets 12.5 seconds
! Etc.
5.81
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multi-Level Queues
Ready List
0

Ready List
1

Ready List
2

Ready List
3

Scheduler
CPU
Preemption or voluntary yield
Done
New
Process
All processes at level i run before
any process at level j
At a level, use another policy, e.g. RR
5.82
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multi-Level Queues
! Foreground and background processes
" Foreground processes in higher priority queue
" Background in low priority queue
" Background processes run only when there is no foreground process
! Foreground and background example:
" Priority 1: interrupt handler threads
" Priority 2: device drivers
" Priority 3: interactive processing jobs
" Priority 4: interactive editing processes
" Priority 5: normal batch jobs
" Priority 6: long batch jobs
5.83
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Queue
! 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 and 20%
to background in FCFS
5.84
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! A process can move between the various queues; aging can be
implemented this way.
! Example:
" If a process uses too much CPU time, it is moved to a lower priority
queue.
! Thus I/O-bound and interactive processes are left in higher-priority queue.
! Also, if a process waits too long in a lower-priority queue will be moved to a
higher-priority queue (aging).
5.85
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Multilevel feedback queues use multiple queues with different priorities
" Round robin at each priority level
" Run highest priority jobs rst
" Once those nish, run next highest priority, etc
" Jobs start in the highest priority queue
" If time slice expires, drop the job by one level
" If time slice does not expire, push the job up by one level
5.86
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
A B C
time = 0
Time
5.87
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
B C
time = 1
A
A
Time
5.88
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
C
time = 2
A B
A B
Time
5.89
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
C
time = 3
A B
A B C
Time
5.90
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
C
time = 3
A B
A B C
Time
suppose process A is blocked on an I/O
5.91
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
C
time = 3
A
B
A B C
Time
suppose process A is blocked on an I/O
5.92
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
time = 5
B A B C
Time
C
A
suppose process A is returned from an I/O
5.93
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
time = 6
B A B C
Time
C
A
5.94
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
time = 8
B A B C
Time
A C
C
5.95
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Priority 0 (time slice = 1):
! Priority 1 (time slice = 2):
! Priority 2 (time slice = 4):
time = 9
B A B C
Time
A C C
5.96
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queues
! Approximates SRTF
" A CPU-bound job drops like a rock
" I/O-bound jobs stay near the top
" Still unfair for long running jobs
" Counter-measure: Aging
! Increase the priority of long running jobs if they are not serviced for a
period of time.
5.97
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multilevel Feedback Queue
! Multilevel-feedback-queue scheduler dened by the following
parameters:
" number of queues
" scheduling algorithms for each queue
! allowing to combine the advantages of several algorithms
" 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
5.98
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example of Multilevel Feedback Queue
! Three queues:
" Q
0
RR with time quantum 8 milliseconds
" Q
1
RR time quantum 16 milliseconds
" Q
2
FCFS
! Scheduling
" A new job enters queue Q
0
. When it gains CPU, job receives 8
milliseconds. If it does not nish in 8 milliseconds, job is moved to
queue Q
1

" At Q
1
job receives 16 additional milliseconds. If it still does not
complete, it is preempted and moved to queue Q
2
.
5.99
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Lottery Scheduling
! Lottery scheduling is an adaptive scheduling approach to address the
fairness problem
" Each process owns some tickets
" On each time slice, a ticket is randomly picked
" On average, the allocated CPU time is proportional to the number of
tickets given to each job
5.100
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Lottery Scheduling
! How to assign tickets?
" To approximate SRTF, short running jobs get more, long running jobs
get fewer
" To avoid starvation, every job gets at least one ticket (everyone makes
progress)
! Advantage over strict priority scheduling: behaves gracefully as load
changes
" Adding or deleting a job affects all jobs proportionally, independent of
how many tickets each job possesses
5.101
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Lottery Scheduling Example
! short jobs: 10 tickets each
! long jobs: 1 ticket each
# short jobs/# long jobs % of CPU for each short job % of CPU for each long job
1/1 91% 9%
0/2 0% 50%
2/0 50% 0%
10/1 10% 1%
1/10 50% 5%
5.102
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Fair Share Scheduling
! All scheduling algorithms discussed so far treat collection of ready
processes as a homogeneous collection
! Generally a user application or job consists of many processes
! This structure is not recognized by traditional schedulers
! User is not interested in how an individual process performs, but it is
interested in how set of processes which constitute a single application,
performs
! The CPU usage is equally distributed among system users or groups, as
opposed to equal distribution among processes
5.103
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Fair Share Scheduling
! Introduces two levels of abstraction:
" users
" groups
! Users of the same department: one group
! Attempt to give each group/user similar service
" if there are 4 equally important departments (groups) and one
department has more processes than the others, degradation of
response time should be more pronounced for that department
5.104
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Users
! If four users (A,B,C,D) are concurrently executing one process each, the
scheduler will logically divide the available CPU cycles such that each user
gets 25% of the whole (100% / 4 = 25%).
! If user B starts a second process, each user will still receive 25% of the total
cycles, but both of user B's processes will now use 12.5%.
! On the other hand, if a new user starts a process on the system, the
scheduler will reapportion the available CPU cycles such that each user
gets 20% of the whole (100% / 5 = 20%).
5.105
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Groups
! Another layer of abstraction allows us to partition users into groups, and
apply the fair share algorithm to the groups as well. In this case, the
available CPU cycles are divided rst among the groups, then among the
users within the groups, and then among the processes for that user. For
example, if there are three groups (1,2,3) containing three, two, and four
users respectively, the available CPU cycles will be distributed as follows:
100% / 3 groups = 33.3% per group
Group 1: (33.3% / 3 users) = 11.1% per user
Group 2: (33.3% / 2 users) = 16.7% per user
Group 3: (33.3% / 4 users) = 8.3% per user
5.106
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Fair Share Scheduling
! FSS is all about making scheduling decisions based on process sets rather
than on basis of individual processes
! Each group/user is assigned a share of the processor
! Considers the execution history of a related group of processes, along with
the individual execution history of each process in making scheduling
decisions
! Scheduling is based on priority
5.107
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Fair Share Scheduling
! Each group is allocated a weight W
k
! 0<= W
k
<=1 &
! Each process is assigned a base priority
! Priority of a process drops as the process and its group uses the
processor
! Greater the weight assigned to a group, the less its utilization will affect
its priority
! Priorities are revised every xed interval of time
! CPU utilization is measured using clock ticks
5.108
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Revising Priorities
! CPU
j
[i] = Measure of processor usage by process j in time interval i
! GCPU
k
[i] = Measure of processor usage by group k in time interval i
! The priority decreases as the process and group use the processor
! With more weight W
k
, group usage decreases less the priority
5.109
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example
! 3 processes divided in 2 groups (group 1: process A, group 2: process B and C)
! Each group has weight = 0,5
! All processes are CPU-bound
! All processes have Base priority = 60
! Clock period is 1/60 sec.
! Priorities are revised every second
5.110
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
5.111
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Exercise 1
! Given the following information:
! Using SJF, draw a timeline showing the time that each process arrives and
the order that each is processed.
! Calculate the nish time for each process.
Process Arrival Time CPU Cycle
A 0 2
B 1 12
C 2 4
D 4 1
E 5 8
F 7 5
G 8 3
5.112
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Exercise 2
! Given the following information:
! draw a timeline for each of the following scheduling algorithms:
" FCFS
" SJF
" SRTF
" RR (using a time quantum of 5, ignoring context switching)
! Compute the waiting time and turn-around time for every process for each of the
previous algorithms.
Process Arrival Time CPU Cycle
A 0 10
B 2 12
C 3 3
D 6 1
E 9 15
5.113
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Exercise 3
! Assume that 5 processes arrive at the ready queue at the times shown below.
The estimated next burst times are also shown.
! What is the waiting time for each process if preemptive shortest remaining
time rst scheduling is used?
! What is the waiting time for each process if non-preemptive shortest job
rst scheduling is used?
Process Arrival Time Burst Time
P1 0 7
P2 1 1
P3 3 2
P4 4 4
P5 5 3
5.114
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Thread Scheduling
! Distinction between user-level and kernel-level threads
! Local Scheduling:
" Process-contention scope (PCS): scheduling competition is
within the process
! Many-to-one and many-to-many models, thread library
schedules user-level threads to run on a kernel thread called
LWP (light-weight process)
! Global Scheduling:
" Kernel thread scheduled onto available CPU is system-
contention scope (SCS) competition among all threads in
system
5.115
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Thread Scheduling
! The scope of a thread can only be specied before the thread is
created
! Every SCS thread and every group of PCS threads will be handled
like separate processes by the system scheduler
! A thread that has a scope SCS will ght with other processes and
other SCS threads for the CPU. That is, if there is 1 process P1 with
10 threads with scope SCS and a single threaded process P2, P2 will
get 1 timeslice out of 11 and every thread in P1 will get 1 timeslice out
of 11: P1 will get 10 time more timeslices than P2
5.116
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Thread Scheduling
! All threads of a process that have a scope of PCS will be grouped
together and this group of threads contents for the CPU. If there is a
process with 4 PCS threads and 2 SCS threads, then each of the SCS
threads will get 1 out of 3 timeslices, and the other 4 PCS threads will
share 1 out of 3 timeslices.
" How the PCS threads share their timeslice among themselves is
determined by the scheduling policy and the thread's priority.
5.117
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Pthread Scheduling
! API allows specifying either PCS or SCS during thread creation
" PTHREAD_SCOPE_PROCESS schedules threads using PCS
scheduling
" PTHREAD_SCOPE_SYSTEM schedules threads using SCS
scheduling.
5.118
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[])
{
int i;
pthread t tid[NUM THREADS];
pthread attr t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* set the scheduling algorithm to PROCESS or SYSTEM */
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
/* set the scheduling policy - FIFO, RR, or OTHER */
pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
/* create the threads */
for (i = 0; i < NUM THREADS; i++)
pthread_create(&tid[i],&attr,runner,NULL);
5.119
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Pthread Scheduling API
/* now join on each thread */
for (i = 0; i < NUM THREADS; i++)
pthread_join(tid[i], NULL);
}

/* Each thread will begin control in this function */
void *runner(void *param)
{
printf("I am a thread\n");
pthread_exit(0);
}
5.120
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Thread Scheduling
! A PCS thread has a priority. The priority is assigned with
pthread_setschedparam(). The scheduler will not change the priority of
a thread
! The scheduling policy could be SCHED_FIFO or SCHED_RR.
! The priority and scheduling policies are meaningless when a thread has
scope SCS.
! The one-to-one model is found in Windows XP, Solaris and Linux. This
model schedules threads using only the SCS scope approach.
5.121
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multiple-Processor Scheduling
! CPU scheduling becomes more complex when multiple CPUs are available
! Homogeneous processors within a multiprocessor
! Asymmetric multiprocessing only one processor accesses the system
data structures, alleviating the need for data sharing
! Symmetric multiprocessing (SMP): each processor is self-scheduling
" all processes in common ready queue (load sharing), problem:
synchronization. Dont want two processors choosing the same
process and dont want to lose a process.
" each processor has its own private queue of ready processes
5.122
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Processor Affinity
! Process has afnity for processor on which it is currently running (e.g., to
avoid repopulating caches)
" soft afnity (not guaranteed)
" hard afnity
5.123
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
NUMA and CPU Scheduling
! Non Uniform Memory Access
Architecture can affect processor affinity.
5.124
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Load Balancing
! On SMP multiprocessor systems, it is important to keep the workload balanced
among the processors
! Load balancing is critical in systems where each processor has its own private queue
of eligible processes to execute
" with a common runqueue, load balancing is automatically managed since, once a
processor is idle, it immediately extracts a runnable process from the common
runqueue
! Techniques:
" Push migration: a specic task periodically checks the load on each processor
and, if it nds an imbalance, evenly distributes the load by moving processes
from overloaded to idle or less-busy processors
" Pull migration: an idle processor pulls a waiting task from a busy processor
" Conicts with processor afnity
" Often combination of the pull and push migration approaches
! Example:
" Linux implements both techniques: it runs its load balancing algorithm every 200
ms (push migration) or whenever the runqueue for a processor is empty (pull
migration).
5.125
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multicore Processors
! Recent trend to place multiple processor cores on same physical chip
! Each core has its own register set: seen by OS as a processor
! Faster and consume less power
! Memory stall: when a processor accesses memory it spends a signicant
amount of time waiting for the data to become available.
5.126
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multicore Processors
! Multiple threads per core
" Takes advantage of memory stall to make progress on another thread
while memory retrieve happens
" Hardware threads: hardware support includes logic for thread
switching, thus decreasing the context switch time.
! 2 (or more) hardware threads are assigned to each core. If one thread
stalls, while waiting for memory, the core can switch to another thread.
! Example:
" UltraSPARC T1 CPU has 8 cores per chip and 4 hardware threads per
core
" Itanium is a dual core with 2 hardware threads per core.

5.127
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Multithreaded multicore processor
2 different levels of scheduling:
! Mapping software thread onto hardware thread
" traditional scheduling algorithms
! Which hardware thread a core will run next
" Round Robin (UltraSPARC) or dynamic priority-based (Itanium).
0
1
5.128
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Operating System Examples
! Solaris scheduling
! Linux scheduling
5.129
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
CPU scheduling in Solaris
! Priority-based scheduling
" 6 classes: real time, system, fair share, xed priority, time sharing,
interactive (in order of priority)
" Different priorities and algorithm in different classes
" Default class: time sharing
" Policy in the time sharing class:
! Multilevel feedback queue with variable time slices

5.130
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Solaris Scheduling
kernel threads
6 classes of scheduling
Default: time-sharing based
on a multi-level feedback
queue
5.131
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Solaris Dispatch Table
! Columns indicate:
" Priority: a higher number indicates a higher priority
" Time quantum: the lowest priority has the highest time quantum
" Time Quantum expired: the new priority of a thread that has used its entire time
quantum without blocking (CPU-bound threads)
" Return from sleep: the priority of a thread that is returning from sleeping (waiting
for I/O, I/O-boound threads).
Good response time for interactive
processes and good throughput
for CPU-bound processes
5.132
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Linux Scheduling Goals
! Provide good interactive performance.
! Provide fairness: no process should nd itself starved of timeslice for any
reasonable amount of time. Likewise, no process should receive an unfairly
high amount of timeslice.
! Soft RT Scheduling: RT tasks are assigned special scheduling modes and
the scheduler gives them priority over any other task on the system.
5.133
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Linux Policy
! CPU bound processes are scheduled less frequently but are given longer
timeslice.
! I/O bound processes are scheduled for short periods and more frequently.
! Leading to improved process response time at the cost of process
throughput.
5.134
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Linux Scheduling
! Preemptive, priority-based, with two priority ranges:
" time-sharing
" real-time
! Real-time range from 0 to 99 and nice value (time-sharing) from
100 to 140. (lower value is higher priority)
! Support for SMP: each processor has its own runqueue, with two
priority arrays, active and expired.
! When a task has exhausted its quantum, is considered expired and
cannot be run until all other tasks have also exhausted their time
quanta.
5.135
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Linux scheduling
! A priority-based, preemptive with global round-robin scheduling
" Each process have a priority
" Processes with a higher priority also have a larger time slices
" Before the time slice is used up, processes are scheduled based on
priority.
" After the time slice of a process is used up, the process must wait until
all ready processes to use up their time slice (or be blocked) a round-
robin approach.
! No starvation problem
" For a user process, its priority may + or 5 depending whether the
process is I/O- bound or CPU-bound.
! Giving I/O bound process higher priority.
5.136
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Priorities and Time-slice length
5.137
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Runqueue
! Runqueue keeps track of all runnable tasks assigned to a particular CPU. As such,
one runqueue is created and maintained for each CPU in a system.
! Each runqueue contains two priority arrays:
" Active processes: runnable processes that have not exhausted their time
quantum
" Expired processes: runnable processes that have exhausted their time quantum
" tasks on a CPU begin in one priority array, the active one, and as they run out of
their timeslices they are moved to the expired priority array
" When there are no more runnable tasks in the active priority arrays, it is simply
swapped with the expired priority array (simply updating two pointers). During the
move, a new timeslice is calculated.
5.138
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
List of Tasks Indexed According to Priorities
5.139
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
139
Scheduling Mechanism of Linux
! Each process has a quantum and priority associated with it
! Linux implicitly favors I/O-bound processes over CPU-bound ones
! In order to select a process to run, the Linux scheduler must consider the priority of
each process. Actually, there are two kinds of priority:
" Static priority assigned by the users to conventional processes and ranges from
100 to 140. It is never changed by the scheduler. Every conventional process
has its own static priority, which is a value used by the scheduler to rate the
process with respect to the other conventional processes in the system. The
kernel represents the static priority of a conventional process with a number
ranging from 100 (highest priority) to 139 (lowest priority). A new process always
inherits the static priority of its parent. However, a user can change the static
priority of the processes that he owns by passing some "nice values" to the nice()
and set_priority( ) system call.
" Dynamic priority: it is essentially the sum of the base time quantum (which is
therefore also called the base priority of the process) and of the number of ticks
of CPU time left to the process before its quantum expires in the current epoch.
! The static priority of a real-time process is always higher than the dynamic priority of
a conventional one: the scheduler will start running conventional processes only
when there is no real-time process in a TASK_RUNNING state.
5.140
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Dynamic Priorities
! Scheduler rewards I/O-bound tasks and punishes CPU-bound tasks by
adding or subtracting from a tasks static priority.
! This adjusted priority is dynamic priority
! Maximum Priority bonus and penalty is 5.

5.141
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
I/O-bound vs. CPU-bound Heuristics
! To determine how much penalty or bonus has to be given to a process
scheduler keeps track of how long the process sleeps as opposed to
running during its time slice.
! When a task is woken up from sleep, its total sleep time is added to its
sleep_avg variable (limiting the max value to MAX_SLEEP_AVG)
! When a task gives up the CPU, voluntarily or involuntarily, the time the
current task spent running is subtracted from its sleep_avg.
5.142
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Calculating dynamic priority
! The sleep_avg value is used to determine the Dynamic Priority as it is an
index of how much process is I/O bound or CPU bound
! effective_prio()
" It checks whether the function is RT or not, If it is RT then no bonus.
" It maps the sleep_avg value to bonus in range -5 to 5 which is then
subtracted from the priority to get Dynamic Priority.
5.143
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Linux Implements RT and Nice Scheduling
! In Linux real time tasks (RT) are assigned static priorities.
! All other tasks have dynamic priorities based on their nice values plus or minus 5.
! The degree of interactivity determines whether 5 will be added or subtracted from the
nice value.
" If 5 is added to the priority of a task, then its priority is lower (has higher number),
and conversely
! The length of time a task has been sleeping while waiting for an I/O is used to
determine the tasks degree of interactivity.
! Tasks more interactive have longer sleep times (more time awaiting the completion of
an I/o) and get an adjustment closer to -5 (increasing their priority).
" Hence the scheduler favors interactive tasks.
! In contrast, tasks with shorter sleep times are more likely CPU-bound jobs and will
have a number closer to +5 added to their nice values (thus decreasing their priority).
! Recalculation of the dynamic priority occurs only when a task has used up its time
quantum and needs more time.
" And, of course, It is moved to the expired array.
5.144
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Scheduling of Conventional Processes
! Static priority
" Conventional processes: 100-139
! Can be changed by nice(), setpriority() system calls
! Base time quantum: the time-quantum assigned by the scheduler if it has
exhausted its previous time quantum
5.145
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Typical Priority Values for a Conventional
Process
Description
Static
priority
Nice
value
Base time
quantum
Highest
static
priority
100 -20 800 ms
High static
priority
110 -10 600 ms
Default static
priority
120 0 100 ms
Low static
priority
130 +10 50 ms
Lowest static
priority
139 +19 5 ms
5.146
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Dynamic Priority and Average Sleep Time
! Besides a static priority, a conventional process also has a dynamic
priority, which is a value ranging from 100 (highest priority) to 139 (lowest
priority).
! Dynamic priority = max (100, min( static_priority bonus + 5, 139) )
" Bonus: 0-10
! <5: penalty
! >5: premium
" Dependent on the average sleep time
! Average number of nanoseconds that the process spent while
sleeping
5.147
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Average Sleep Time, Bonus Values, and Time
Slice Granularity
Average sleep time Bonus
>= 0 but < 100 ms 0
>= 100 ms but < 200 ms 1
>= 200 ms but < 300 ms 2
>= 300 ms but < 400 ms 3
>= 400 ms but < 500 ms 4
>= 500 ms but < 600 ms 5
>= 600 ms but < 700 ms 6
>= 700 ms but < 800 ms 7
>= 800 ms but < 900 ms 8
>= 900 ms but < 1000 ms 9
1 second 10

Potrebbero piacerti anche