0% found this document useful (0 votes)
32 views83 pages

CH 05

Uploaded by

aman28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views83 pages

CH 05

Uploaded by

aman28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 83

Chapter 5: CPU Scheduling

Operating System Concepts – 10 th Edition Silberschatz, Galvin and Gagne


Chapter 5: CPU Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
 Thread Scheduling
 Multiple-Processor Scheduling
 Real-Time CPU Scheduling
 Operating Systems Examples
 Algorithm Evaluation

Operating System Concepts – 10 th Edition 5.2 Silberschatz, Galvin and Gagne


Objectives

 To introduce CPU scheduling, which is the basis for


multiprogrammed operating systems
 To describe various CPU-scheduling algorithms
 To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system
 To examine the scheduling algorithms of several
operating systems

Operating System Concepts – 10 th Edition 5.3 Silberschatz, Galvin and Gagne


Basic Concepts

 Maximum CPU utilization


obtained with
multiprogramming
 CPU–I/O Burst Cycle –
Process execution
consists of a cycle of CPU
execution and I/O wait
 CPU burst followed by I/O
burst
 CPU burst distribution is of
main concern

Operating System Concepts – 10 th Edition 5.4 Silberschatz, Galvin and Gagne


CPU Burst Distribution
 The CPU bursts tend to have an exponential or hyper-
exponential curve.
 There is a large number of short CPU bursts, and there is a
small number of long CPU bursts.
 An I/O bound program
 many short CPU bursts.
 A CPU-bound program
 few long CPU bursts.

Operating System Concepts – 10 th Edition 5.5 Silberschatz, Galvin and Gagne


Histogram of CPU-burst Times

Operating System Concepts – 10 th Edition 5.6 Silberschatz, Galvin and Gagne


Schedulers
 Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue.
 Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU.
 The long-term scheduler controls the degree of
multiprogramming.
 The long-term scheduler must select a good process mix of
I/O-bound and CPU-bound processes.

Operating System Concepts – 10 th Edition 5.7 Silberschatz, Galvin and Gagne


CPU Scheduler
 Short-term scheduler selects from among the processes
in ready queue, and allocates the CPU to one of them
 Queue may be ordered in various ways
 CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
 Scheduling under 1 and 4 is nonpreemptive
 All other scheduling is preemptive
 Consider access to shared data
 Consider preemption while in kernel mode
 Consider interrupts occurring during crucial OS
activities

Operating System Concepts – 10 th Edition 5.8 Silberschatz, Galvin and Gagne


Dispatcher

 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

Operating System Concepts – 10 th Edition 5.9 Silberschatz, Galvin and Gagne


Scheduling Criteria

 CPU utilization – keep the CPU as busy as possible


 Throughput – # of processes that complete their execution per
time unit
 minimize overhead (context switching)
 efficient use of resources (CPU, disk, cache, …)
 Turnaround time – amount of time to execute a particular
process
 Waiting time – amount of time a process has been waiting in
the ready queue
 Response time – amount of time it takes from when a request
was submitted until the first response is produced, not output
(for time-sharing environment)
 Echo a keystroke in editor (acceptable delay ~50-
150millisec)
 Compile a program
 Run a large scientific problem
Operating System Concepts – 10 th Edition 5.10 Silberschatz, Galvin and Gagne
Scheduling Algorithm Optimization Criteria

 Max CPU utilization


 Max throughput
 Min turnaround time
 Min waiting time
 Min response time

Operating System Concepts – 10 th Edition 5.11 Silberschatz, Galvin and Gagne


First- Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

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


 Average waiting time: (0 + 24 + 27)/3 = 17

Operating System Concepts – 10 th Edition 5.12 Silberschatz, Galvin and Gagne


FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect - short process behind long process
 Consider one CPU-bound and many I/O-bound processes
 FCFS algorithm is nonpreemptive.
 Pros and Cons:
 Simple (+)
 Short jobs get stuck behind long ones (-)

Operating System Concepts – 10 th Edition 5.13 Silberschatz, Galvin and Gagne


Shortest-Job-First (SJF) Scheduling

 Associate with each process the length of its next CPU burst
 Use these lengths to schedule the process with the
shortest time
 SJF is optimal – gives minimum average waiting time for a
given set of processes
 The difficulty is knowing the length of the next CPU request
 Could ask the user
 If two processes have same length next CPU burst, FCFS
scheduling is used to break the tie.
 Two schemes:
 nonpreemptive – once CPU given to the process it cannot be
preempted until it completes its CPU burst.
 Preemptive – if a new process arrives with CPU burst length
less than remaining time of current executing process,
preempt. This scheme is know as the shortest-Remaining-
Time-First (SRTF).

Operating System Concepts – 10 th Edition 5.14 Silberschatz, Galvin and Gagne


Example of Non-preemptive SJF

ProcessArriva l Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Operating System Concepts – 10 th Edition 5.15 Silberschatz, Galvin and Gagne


Determining Length of Next CPU Burst
 Long-term scheduling (batch system)
 process time limit specified by the user
 SJF scheduling is used frequently in long-term scheduling
 SJF cannot be implemented at the level of short-term CPU
scheduling
 Can only estimate the length – should be similar to the previous
one
 Then pick process with shortest predicted next CPU burst

 Can be done by using the length of previous CPU bursts, using


exponential averaging
1. tn = actual length of nth CPU burst
2. n+1 = predicted value for the next CPU burst
3. , 0    1
3. Define: n+1 =  tn + (1 - ) n
 Commonly, α set to ½
 Preemptive version called shortest-remaining-time-first
Operating System Concepts – 10 th Edition 5.16 Silberschatz, Galvin and Gagne
Prediction of the Length of the Next CPU Burst

Operating System Concepts – 10 th Edition 5.17 Silberschatz, Galvin and Gagne


Examples of Exponential Averaging
  =0
 n+1 = n
 Recent history does not count
  =1
 n+1 = tn
 Only the actual last CPU burst counts
  = 1/2
 recent and past history are equally weighted
 0 can be a constant or overall system average
 If we expand the formula, we get:
n+1 =  tn+(1 - ) tn -1 +…
+(1 -  )j  tn -j + …
+(1 -  )n +1 0

 Since both  and (1 - ) are less than or equal to 1, each


successive term has less weight than its predecessor
Operating System Concepts – 10 th Edition 5.18 Silberschatz, Galvin and Gagne
Example of Shortest-remaining-time-first

 Now we add the concepts of varying arrival times and


preemption to the analysis
ProcessAarri Arrival TimeTBurst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4


= 6.5 msec

Operating System Concepts – 10 th Edition 5.19 Silberschatz, Galvin and Gagne


SRTF discussion
 SRTF Pros & Cons
 Optimal (average response time) (+)
 Hard to predict future (-)
 Unfair (-)

Operating System Concepts – 10 th Edition 5.20 Silberschatz, Galvin and Gagne


Priority Scheduling
 A priority number (integer) is associated with each
process

 The CPU is allocated to the process with the highest


priority (smallest integer  highest priority)
 Preemptive
 Nonpreemptive

 Equal-priority processes are scheduled in FCFS order


 SJF is priority scheduling where priority is the inverse of
predicted next CPU burst time

 Problem  Starvation – low priority processes may never


execute

 Solution  Aging – as time progresses increase the priority


of the process

Operating System Concepts – 10 th Edition 5.21 Silberschatz, Galvin and Gagne


Example of Priority Scheduling

ProcessAarri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Priority scheduling Gantt Chart

 Average waiting time = 8.2 msec

Operating System Concepts – 10 th Edition 5.22 Silberschatz, Galvin and Gagne


Round Robin (RR)

 Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready
queue.
 RR scheduling is implemented as a FIFO queue of processes.
 New processes are added to the tail of the ready queue.
 A process may itself leave the CPU if its CPU burst is less
than 1 quantum, otherwise timer will cause an interrupt and
the process will be put at the tail of the queue

Operating System Concepts – 10 th Edition 5.23 Silberschatz, Galvin and Gagne


Example: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
 The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

 Waiting time for P1=(77-20)+(121-97)= 81


P2=(20-0)=20
P3=(37-0)+(97-57)+(134-117)= 94
P4=(57-0)+(117-77)= 97
 Average waiting time = (81+20+94+97)/4=73
Operating System Concepts – 10 th Edition 5.24 Silberschatz, Galvin and Gagne
Round Robin (RR)
 Typically, higher average turnaround than SJF, but better
response.

 Round-Robin Pros and Cons:


 Better for short jobs, Fair (+)
 Context-switching time adds up for long jobs (-)

Operating System Concepts – 10 th Edition 5.25 Silberschatz, Galvin and Gagne


Round Robin (RR)
 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.
 Example
 5 processes, time quantum = 20
 each process will get up to 20 ms every 80 ms.
 Performance
 q large  FCFS
 q small  processor sharing
 appears as though each of n processes has its own
processor running at 1/n the speed of the real processor
 q must be large with respect to context switch, otherwise
overhead is too high.
 q usually 10ms to 100ms, context switch < 10 sec

Operating System Concepts – 10 th Edition 5.26 Silberschatz, Galvin and Gagne


Time Quantum and Context Switch Time

Operating System Concepts – 10 th Edition 5.27 Silberschatz, Galvin and Gagne


Turnaround Time Varies With The Time Quantum

80% of CPU bursts


should be shorter than q

Operating System Concepts – 10 th Edition 5.28 Silberschatz, Galvin and Gagne


Turnaround Time Varies With The Time Quantum

 The average turnaround time can be improved if most


processes finish their next CPU burst in a single time
quantum.
 Example
 3 processes, CPU burst = 10 for each
 quantum = 1, average turnaround time = 29
 quantum = 10, average turnaround time = 20
 If context-switch time is added, average turnaround
time will increase for smaller time quantum as there
will be more number of context switches.
 A rule of thumb
 80 percent of the CPU bursts should be shorter than the
time quantum.

Operating System Concepts – 10 th Edition 5.29 Silberschatz, Galvin and Gagne


Multilevel Queue
 Ready queue is partitioned into separate queues, eg:
 foreground (interactive)
 background (batch)
 Process permanently in a given 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
 20% to background in FCFS

Operating System Concepts – 10 th Edition 5.30 Silberschatz, Galvin and Gagne


Multilevel Queue Scheduling

Operating System Concepts – 10 th Edition 5.31 Silberschatz, Galvin and Gagne


Multilevel Feedback Queue
 A process can move between the various queues; aging can
be implemented this way
 CPU-bound processes are moved to lower-priority queue.
 I/O-bound and interactive processes get the higher-priority
queue.
 A process that waits too long in a lower-priority queue is
moved to a higher-priority queue.
 Multilevel-feedback-queue scheduler defined by the following
parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will
enter when that process needs service

Operating System Concepts – 10 th Edition 5.32 Silberschatz, Galvin and Gagne


Example of Multilevel Feedback Queue
 Three queues:
 Q0 – RR with time quantum 8
milliseconds
 Q1 – RR time quantum 16 milliseconds
 Q2 – FCFS

 Scheduling
 A new job enters queue Q0 which is
served FCFS
 When it gains CPU, job receives 8
milliseconds
 If it does not finish in 8
milliseconds, job is moved to
queue Q1
 At Q1 job is again served FCFS and
receives 16 additional milliseconds
 If it still does not complete, it is
preempted and moved to queue Q2
 Processes in Q2 are run on an FCFS
basis, only when Q0 and Q1 are empty
Operating System Concepts – 10 th Edition 5.33 Silberschatz, Galvin and Gagne
Fairness
 Unfair in scheduling:
 STCF: *must* be unfair to be optimal
 Favor I/O jobs? Penalize CPU.
 How do we implement fairness?
 Could give each queue a fraction of the CPU.
 But this isn’t always fair. What if there’s one long-
running job, and 100 short-running ones?
 Could adjust priorities: increase priority of jobs, as they
don’t get service. This is what’s done in UNIX.
 Problem is that this is ad hoc – what rate should you
increase priorities?
 And, as system gets overloaded, no job gets CPU time,
so everyone increases in priority.
 The result is that interactive jobs suffer – both short
and long jobs have high priority!

Operating System Concepts – 10 th Edition 5.34 Silberschatz, Galvin and Gagne


Lottery scheduling: random simplicity
 Give every job some number of lottery tickets
 On each time slice, randomly pick a winning ticket.
 On average, CPU time is proportional to # of tickets given to
each job.

 How do you 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 (so
everyone makes progress).

Operating System Concepts – 10 th Edition 5.35 Silberschatz, Galvin and Gagne


Grace under load change
 Adding or deleting a job (and their tickets):
 affects all jobs proportionately, independent of how many
tickets each job has.
 For example, if short jobs get 10 tickets, and long jobs get 1 each,
then:

# short jobs / % of CPU each short % of CPU each


long
# long jobs job gets job gets

1/1 91% 9%
0/2 NA 50%
2/0 50% NA
10/1 10% 1%
1/10 50% 5%
Silberschatz, Galvin and Gagne
Operating System Concepts – 10 th Edition 5.36
Thread Scheduling

 Distinction between user-level and kernel-level threads


 When threads supported, threads scheduled, not processes
 Many-to-one and many-to-many models, thread library schedules
user-level threads to run on LWP
 Known as process-contention scope (PCS) since scheduling
competition is within the process
 Typically done via priority set by programmer and are not
adjusted by the thread library
 Preempt the thread currently running in favor of a higher-priority
thread
 no guarantee of time slicing among threads of equal priority
 Kernel thread scheduled onto available CPU is system-contention
scope (SCS) – competition among all threads in system
 One-to-one model, such as Windows and Linux schedule threads
using only SCS

Operating System Concepts – 10 th Edition 5.37 Silberschatz, Galvin and Gagne


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
 In the many-to-many model, the PTHREAD_SCOPE_PROCESS
policy schedules user-level threads onto available LWPs
 The number of LWPs is maintained by the thread library
 The PTHREAD_SCOPE_SYSTEM scheduling policy will create and
bind an LWP for each user-level thread
 The Pthread IPC (Interprocess Communication) provides two
functions for setting and getting the contention scope policy:
 pthread_attr_setscope(pthread_attr_t *attr, int scope)
 pthread_attr_getscope(pthread_attr_t *attr, int *scope)
 Can be limited by OS – Linux and Mac OS X only allow
PTHREAD_SCOPE_SYSTEM
Operating System Concepts – 10 th Edition 5.38 Silberschatz, Galvin and Gagne
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[]) {
int i, scope;
pthread_t tid[NUM THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* first inquire on the current scope */
if (pthread_attr_getscope(&attr, &scope) != 0)
fprintf(stderr, "Unable to get scheduling scope\n");
else {
if (scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS");
else if (scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM");
else
fprintf(stderr, "Illegal scope value.\n");
}
Operating System Concepts – 10 th Edition 5.39 Silberschatz, Galvin and Gagne
Pthread Scheduling API
/* set the scheduling algorithm to PCS or SCS */
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* create the threads */
for (i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i],&attr,runner,NULL);
/* 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)
{
/* do some work ... */
pthread_exit(0);
}

Operating System Concepts – 10 th Edition 5.40 Silberschatz, Galvin and Gagne


Multiple-Processor Scheduling
 CPU scheduling more complex when multiple CPUs are
available
 Homogeneous processors within a multiprocessor: use any
available processor to run any processes in the queue
 Asymmetric multiprocessing – only one processor – the master
server, accesses the system data structures, alleviating the
need for data sharing
 -ve: master server becomes a potential bottleneck where
overall system performance may be reduced
 Symmetric multiprocessing (SMP) – each processor is self-
scheduling, two strategies for scheduling:
 all processes in common ready queue; possible race
condition
 each has its own private queue of ready processes; most
common

Operating System Concepts – 10 th Edition 5.41 Silberschatz, Galvin and Gagne


SMP - Multicore Processors

 Recent trend to place multiple processor cores on same


physical chip
 Faster and consumes less power
 Complicates scheduling issues
 Memory stall - a processor accessing memory spends a
significant amount of time waiting for the data to become
available
 modern processors operate at much faster speeds than
memory
 a memory stall can also occur because of a cache miss
 processor can spend up to 50 percent of its time waiting
for data to become available from memory

Operating System Concepts – 10 th Edition 5.42 Silberschatz, Galvin and Gagne


SMP - Multithreaded Multicore System
 Multiple threads per core also growing
 if one hardware thread stalls while waiting for memory,
the core can switch to another thread

Operating System Concepts – 10 th Edition 5.43 Silberschatz, Galvin and Gagne


SMP - Multithreaded Multicore System
 Chip multithreading - each hardware thread
maintains its architectural state, such as
instruction pointer and register set, and
thus appears as a logical CPU that is
available to run a software thread

 Hyper-threading (simultaneous
multithreading or SMT) - assigning multiple
hardware threads to a single processing
core.
 Intel processors—such as the i7—support
two threads per core
 Oracle Sparc M7 processor supports eight
threads per core, with eight cores per
processor, thus providing the operating
system with 64 logical CPUs.

Operating System Concepts – 10 th Edition 5.44 Silberschatz, Galvin and Gagne


SMP - Multithreaded Multicore System
 Two ways to multithread a processing core: coarse-grained and fine-
grained multithreading
 Coarse-grained multithreading - a thread executes on a core until a
long-latency event such as a memory stall occurs
 the core must switch to another thread to begin execution
 cost of switching is high, since the instruction pipeline must be
flushed
 Fine-grained (or interleaved) multithreading - switches between
threads at a much finer level of granularity, typically at the boundary
of an instruction cycle
 the architectural design includes logic for thread switching
 cost of switching between threads is small
 Resources of the physical core (such as caches and pipelines) must be
shared among its hardware threads,
 => a processing core can only execute one hardware thread at a
time.
 Consequently, a multithreaded, multicore processor actually
requires two different levels of scheduling
Operating System Concepts – 10 th Edition 5.45 Silberschatz, Galvin and Gagne
SMP - Multithreaded Multicore System
 Level 1 of scheduling decisions
 operating system chooses which
software thread to run on each
hardware thread (logical CPU)
 Level 2 of scheduling decisions
 how each core decides which hardware
thread to run
 UltraSPARC T3 – round-robin
 Intel Itanium - assigned to each
hardware thread is a dynamic urgency
value 0 - 7, with 0 representing the
lowest urgency and 7 the highest
 five different events that may trigger
a thread switch
 thread with the highest urgency
value executes on the processor
core
 Two different levels of scheduling are not
necessarily mutually exclusive
Operating System Concepts – 10 th Edition 5.46 Silberschatz, Galvin and Gagne
SMP – Load Balancing

 If SMP, need to keep all CPUs loaded for efficiency


 Load balancing attempts to keep workload evenly distributed
across all processors in an SMP system
 Two general approaches to load balancing: push migration
and pull migration
 Push migration – a specific task periodically checks the load
on each processor, and if finds imbalance, pushes task from
overloaded CPU to other CPUs
 Pull migration – idle processor pulls a waiting task from a busy
processor
 Push and pull migration need not be mutually exclusive and
are, in fact, often implemented in parallel on load-balancing
systems

Operating System Concepts – 10 th Edition 5.47 Silberschatz, Galvin and Gagne


SMP - Processor Affinity

 Processor affinity – process has affinity for processor on which it


is currently running
 Successive memory accesses by the thread are often
satisfied in cache memory (known as a “warm cache”)
 Processor migration by threads due to load balancing results
in invalidating and repopulating caches, a high cost operation
 keep a thread running on the same processor and take
advantage of a warm cache
 soft affinity - operating system will attempt to keep a process on
a single processor, but it is possible for a process to migrate
between processors during load balancing
 hard affinity - system calls allow a process to specify a subset of
processors on which it can run
 Linux provides both soft and hard affinity
 Variations including processor sets

Operating System Concepts – 10 th Edition 5.48 Silberschatz, Galvin and Gagne


Processor Affinity in NUMA systems
 The main-memory architecture of a system can affect
processor affinity issues
 NUMA (non-uniform memory access) - two physical processor
chips each with their own CPU and local memory
 all CPUs in a NUMA system share one physical address
space; a CPU has faster access to its local memory than to
memory local to another CPU
 Operating system's CPU scheduler and memory-placement
algorithms are NUMA-aware
 a thread scheduled onto a particular CPU can be
allocated memory closest to where the CPU resides,
providing fastest possible memory access

Operating System Concepts – 10 th Edition 5.49 Silberschatz, Galvin and Gagne


Heterogeneous Multiprocessing
 Heterogeneous Multiprocessing (HMP) - systems designed using
cores that run the same instruction set, yet vary in terms of their
clock speed and power management, including the ability to adjust
the power consumption of a core to the point of idling the core
 Better manages power consumption by assigning tasks to certain
cores based upon the specific demands of the task
 ARM processors that support this type of architecture is known as
big.LITTLE where higher-performance big cores are combined with
energy efficient LITTLE cores.
 Big cores consume greater energy and therefore should only be
used for short periods of time
 Likewise, little cores use less energy and can therefore be used
for longer periods
 CPU scheduler can assign tasks not requiring high performance,
but that run for longer periods, (such as background tasks) to little
cores
 Interactive applications which require more processing power,
but may run for shorter durations, can be assigned to big cores
Operating System Concepts – 10 th Edition 5.50 Silberschatz, Galvin and Gagne
Real-Time CPU Scheduling
 Can present obvious challenges
 Soft real-time systems – no
guarantee as to when critical real-
time process will be scheduled
 They guarantee only that the
process will be given preference
over noncritical processes
 Hard real-time systems – task must
be serviced by its deadline
 For hard real-time systems,
interrupt latency must be bounded
to meet the strict requirements of
these systems

Two types of latencies affect performance


1.Interrupt Latency - time from arrival of interrupt to start of routine
that services interrupt
2.Dispatch Latency - time for scheduler to take current process off
CPU and switch to another
Operating System Concepts – 10 th Edition 5.51 Silberschatz, Galvin and Gagne
Real-Time CPU Scheduling (Cont.)
 Real-time operating systems require that interrupts be disabled for only very short periods
of time
 Conflict phase of dispatch latency has two components:
1. Preemption of any process running in kernel mode
2. Release by low-priority process of resources needed by high-priority processes

Operating System Concepts – 10 th Edition 5.52 Silberschatz, Galvin and Gagne


Priority-based Scheduling
 For real-time scheduling, scheduler must support
preemptive, priority-based scheduling
 This only guarantees soft real-time functionality
 For hard real-time must also provide ability to meet
deadlines
 Processes have new characteristics: periodic ones require
CPU at constant intervals
 Has processing time t, deadline d, period p
 0≤t≤d≤p
 Rate of periodic task is 1/p

Operating System Concepts – 10 th Edition 5.53 Silberschatz, Galvin and Gagne


Virtualization and Scheduling
 Virtualization software schedules multiple guests onto
CPU(s)
 Each guest doing its own scheduling
 Not knowing it doesn’t own the CPUs
 Can result in poor response time
 Can effect time-of-day clocks in guests
 Can undo good scheduling algorithm efforts of guests

Operating System Concepts – 10 th Edition 5.54 Silberschatz, Galvin and Gagne


Rate Montonic Scheduling
 A priority is assigned based on the inverse of its period
 Shorter periods = higher priority;
 Longer periods = lower priority
 P1 is assigned a higher priority than P2.
 Example: Let period for P1 is 50, and for P2 is 100
 The processing times are t1 = 20 for P1 and t2 = 35 for P2
 The deadline for each process requires that it complete
its CPU burst by the start of its next period
 P1 has higher priority than P2
 CPU utilization of a process Pi as the ratio of its burst to
its period—ti/pi
 CPU utilization of P1 is 20/50 = 0.40 and P2 is 35/100 = 0.35,
for a total CPU utilization of 75 percent

Operating System Concepts – 10 th Edition 5.55 Silberschatz, Galvin and Gagne


Missed Deadlines with Rate Monotonic Scheduling
 Rate-monotonic scheduling is considered optimal in that if a set of
processes cannot be scheduled by this algorithm, it cannot be
scheduled by any other algorithm that assigns static priorities
 A set of processes that cannot be scheduled using the rate-monotonic
algorithm
 Example: P1 has a period of p1 = 50 and a CPU burst of t1 = 25. P2 has a
period of p2 = 80 and t2 = 35
 The total CPU utilization of the two processes is (25/50) + (35/80) =
0.94

 Despite being optimal, rate-monotonic scheduling has a limitation: CPU


utilization is bounded, and it is not always possible to maximize CPU
resources fully

Operating System Concepts – 10 th Edition 5.56 Silberschatz, Galvin and Gagne


Earliest Deadline First Scheduling (EDF)
 Priorities are assigned according to deadlines: the earlier
the deadline, the higher the priority; the later the
deadline, the lower the priority
 Priorities may have to be adjusted to reflect the deadline
of the newly runnable process
 Example: P1 has a period of p1 = 50 and a CPU burst of t1 =
25. P2 has a period of p2 = 80 and t2 = 35
 Since deadline p1 < p2, initially P1 has higher priority
than P2
 At t = 50, P1 does not pre-empt P2 as P2 has higher
priority as deadline for P2 is 80, whereas deadline for
P1 is 100

Operating System Concepts – 10 th Edition 5.57 Silberschatz, Galvin and Gagne


Proportional Share Scheduling

 T shares are allocated among all processes in the system

 An application receives N shares of time, where N < T

 This ensures each application will receive N / T of the total


processor time

 Example: T = 100 shares is to be divided among three


processes, A, B, and C. A is assigned 50 shares, B is assigned
15 shares, and C is assigned 20 shares
 Total allocated shares = 85
 If a new process D requested 30 shares, the admission
controller would deny D entry into the system

Operating System Concepts – 10 th Edition 5.58 Silberschatz, Galvin and Gagne


POSIX Real-Time Scheduling
 The POSIX.1b standard
 API provides functions for managing real-time threads
 Defines two scheduling classes for real-time threads:
1. SCHED_FIFO - threads are scheduled using a FCFS strategy with
a FIFO queue. There is no time-slicing for threads of equal
priority
2. SCHED_RR - similar to SCHED_FIFO except time-slicing occurs
for threads of equal priority
 Defines two functions for getting and setting scheduling policy:
1. pthread_attr_getsched_policy(pthread_attr_t *attr, int
*policy)
2. pthread_attr_setsched_policy(pthread_attr_t *attr, int
policy)

Operating System Concepts – 10 th Edition 5.59 Silberschatz, Galvin and Gagne


POSIX Real-Time Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[])
{
int i, policy;
pthread_t_tid[NUM_THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* get the current scheduling policy */
if (pthread_attr_getschedpolicy(&attr, &policy) != 0)
fprintf(stderr, "Unable to get policy.\n");
else {
if (policy == SCHED_OTHER) printf("SCHED_OTHER\n");
else if (policy == SCHED_RR) printf("SCHED_RR\n");
else if (policy == SCHED_FIFO) printf("SCHED_FIFO\n");
}

Operating System Concepts – 10 th Edition 5.60 Silberschatz, Galvin and Gagne


POSIX Real-Time Scheduling API (Cont.)

/* set the scheduling policy - FIFO, RR, or OTHER */


if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0)
fprintf(stderr, "Unable to set policy.\n");
/* create the threads */
for (i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i],&attr,runner,NULL);
/* 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)
{
/* do some work ... */
pthread_exit(0);
}

Operating System Concepts – 10 th Edition 5.61 Silberschatz, Galvin and Gagne


Operating System Examples

 Linux scheduling

 Windows scheduling

 Solaris scheduling

Operating System Concepts – 10 th Edition 5.62 Silberschatz, Galvin and Gagne


Linux Scheduling Through Version 2.5

 Prior to kernel version 2.5, ran variation of standard UNIX


scheduling algorithm
 Did not adequately support SMP systems
 Version 2.5 moved to constant order O(1) scheduling time
 ran in constant time regardless of the number of tasks in the system
 increased support for SMP systems, including processor affinity and
load balancing between processors, Preemptive, priority based
 Two priority ranges: time-sharing and real-time
 Higher priority gets larger q
 Task run-able as long as time left in time slice (active)
 If no time left (expired), not run-able until all other tasks use their
slices
 All run-able tasks tracked in per-CPU runqueue data structure
 Two priority arrays (active, expired)
 Tasks indexed by priority
 When no more active, arrays are exchanged
 Worked well, but poor response times for interactive
processes

Operating System Concepts – 10 th Edition 5.63 Silberschatz, Galvin and Gagne


Linux Scheduling in Version 2.6.23 +
 Completely Fair Scheduler (CFS)
 Scheduling classes
 Each class has specific priority
 Scheduler picks highest priority task in highest priority scheduling
class
 2 scheduling classes included in standard Linux kernels, others can be
added
1. Default scheduling class using CFS scheduling
2. real-time scheduling class
 Rather than associating a relative priority value with the length of
a time quantum, the CFS scheduler assigns a proportion of CPU
processing time to each task
 This proportion is based on nice value, which ranges from -20 to +19 ,
lower value is higher relative priority
 Tasks with lower nice values receive a higher proportion of CPU
processing time, default nice value is 0
 Instead of discrete time slices, proportion of CPU time allocated
from value of target latency – interval of time during which task
should run at least once
 Target latency can increase if number of active tasks increases beyond
a threshold
Operating System Concepts – 10 Edition
th 5.64 Silberschatz, Galvin and Gagne
Linux Scheduling in Version 2.6.23 +
 Priorities not assigned directly
 CFS scheduler maintains per task virtual run time in variable
vruntime
 Associated with decay factor based on priority of task – lower
priority is higher decay rate
 For tasks at normal priority (nice value 0), virtual run time
identical to actual run time
 If a task with default priority runs for 200 milliseconds, its
vruntime = 200 milliseconds
 If a lower-priority task runs for 200 milliseconds, its vruntime
> 200 milliseconds
 If a higher-priority task runs for 200 milliseconds, its vruntime
< 200 milliseconds
 To decide next task to run, scheduler picks task with lowest
vruntime value
 a higher-priority task that becomes available to run can
preempt a lower-priority task

Operating System Concepts – 10 th Edition 5.65 Silberschatz, Galvin and Gagne


CFS Performance

Operating System Concepts – 10 th Edition 5.66 Silberschatz, Galvin and Gagne


Linux Scheduling (Cont.)
 Real-time scheduling according to POSIX.1b
 Any task scheduled using either the SCHED_FIFO or the
SCHED_RR real-time policy runs at a higher priority than
normal (non-real-time) tasks
 Real-time tasks have static priorities within the range of 0 to
99, and normal tasks are assigned priorities from 100 to 139
 Real-time plus normal ranges map into global priority
scheme, lower value indicates higher relative priority
 Normal tasks assigned priorities based on nice values where
values of -20 maps to global priority 100, nice value of +19
maps to priority 139

Operating System Concepts – 10 th Edition 5.67 Silberschatz, Galvin and Gagne


Load Balancing by CFS Scheduler
 Load of each thread is a combination of the thread's priority
and its average rate of CPU utilization
 A thread that has a high priority, and mostly I/O-bound, requires
little CPU usage, has a generally low load, similar to the load of
a low-priority thread that has high CPU utilization.
 The load of a queue is the sum of the loads of all threads in the
queue, and balancing is simply ensuring that all queues have
approximately the same load.
 Migrating a thread may result in a memory access penalty
 A scheduling domain is a set of CPU cores that can be balanced
against one another

Operating System Concepts – 10 th Edition 5.68 Silberschatz, Galvin and Gagne


Windows Scheduling
 Windows uses priority-based preemptive scheduling
 Highest-priority thread runs next
 Dispatcher - The portion of the Windows kernel that handles
scheduling
 Thread runs until (1) blocks, e.g. I/O (2) uses time slice, (3)
preempted by higher-priority thread (4) terminates
 Real-time threads can preempt non-real-time
 32-level priority scheme
 Variable class, threads with priorities 1-15, real-time class
priorities 16-31
 Priority 0 is memory-management thread
 Queue for each priority
 If no run-able thread, runs idle thread

Operating System Concepts – 10 th Edition 5.69 Silberschatz, Galvin and Gagne


Windows Priority Classes
 Win32 API identifies several priority classes to which a
process can belong
 REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
ABOVE_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS,
BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS
 All are variable except REALTIME
 The priority class of a process can be altered with the
SetPriorityClass() function in the Windows API
 A thread within a given priority class has a relative priority
 TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL,
BELOW_NORMAL, LOWEST, IDLE
 Priority class and relative priority combine to give numeric
priority
 Base priority is NORMAL within the class
 If quantum expires, priority lowered, but never below base

Operating System Concepts – 10 th Edition 5.70 Silberschatz, Galvin and Gagne


Windows Priorities

 The base priorities for each priority class are as follows:


 REALTIME_PRIORITY_CLASS—24
 HIGH_PRIORITY_CLASS—13
 ABOVE_NORMAL_PRIORITY_CLASS—10
 NORMAL_PRIORITY_CLASS—8
 BELOW_NORMAL_PRIORITY_CLASS—6
 IDLE_PRIORITY_CLASS—4

Operating System Concepts – 10 th Edition 5.71 Silberschatz, Galvin and Gagne


Windows Priority Classes (Cont.)
 Windows has a special scheduling rule for processes in the
NORMAL_PRIORITY_CLASS
 Windows distinguishes between the foreground process that is
currently selected on the screen and the background processes
 When a process moves into the foreground, Windows increases the
scheduling quantum by 3 giving 3x longer to run before preemption
by time-sharing process
 Windows 7 added user-mode scheduling (UMS)
 Applications create and manage threads independent of kernel
 For large number of threads, much more efficient
 UMS is not intended to be used directly by the programmer, the
schedulers come from programming language libraries that build
on top of UMS
 Microsoft provides Concurrency Runtime (ConcRT), a concurrent
programming framework for C++ designed for task-based
parallelism on multicore processors.
 ConcRT provides a user-mode scheduler together with facilities
for decomposing programs into tasks, which can then be
scheduled on the available processing cores
Operating System Concepts – 10 th Edition 5.72 Silberschatz, Galvin and Gagne
Solaris
 Priority-based scheduling
 Six classes available
 Time sharing (default) (TS)
 Interactive (IA)
 Real time (RT)
 System (SYS)
 Fair Share (FSS)
 Fixed priority (FP)
 Given thread can be in one class at a time
 Each class has different priority and different scheduling
algorithm
 Time sharing class dynamically alters priorities and assigns
time slices of different lengths using a multi-level feedback
queue
 Loadable table configurable by sysadmin

Operating System Concepts – 10 th Edition 5.73 Silberschatz, Galvin and Gagne


Solaris Dispatch Table
 Simplified dispatch table for
scheduling time-sharing and
interactive threads
 Priority: higher number is higher
priority
 Time Quantum: The time
quantum for the associated
priority
 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 (such as from waiting
for I/O)
 Threads in the real-time class
are given the highest priority
 system class runs kernel
threads, e.g. scheduler and
paging
Operating System daemon
Concepts – 10 Edition
th 5.74 Silberschatz, Galvin and Gagne
Solaris Scheduling
 The fixed-priority and fair-share classes were
introduced with Solaris 9, uses one-to-one model
 Threads in the fixed-priority class have the
same priority range as those in the time-sharing
class; their priorities are not dynamically
adjusted.
 The fair-share class uses CPU shares instead of
priorities to make scheduling decisions.
 CPU shares indicate entitlement to available
CPU resources and are allocated to a set of
processes, known as a project.
 class-specific priorities are converted into global
priorities, and the thread with the highest global
priority is run by the scheduler
 The selected thread runs on the CPU until it (1)
blocks, (2) uses its time slice, or (3) is preempted
by a higher-priority thread
 Multiple threads with the same priority run using
RR
 ten threads for servicing interrupts, not in any
class
Operating System Concepts – 10 th Edition 5.75 Silberschatz, Galvin and Gagne
Algorithm Evaluation
 How to select CPU-scheduling algorithm for an OS?
 Determine criteria, then evaluate algorithms
 Deterministic modeling
 Type of analytic evaluation
 Takes a particular predetermined workload and defines the
performance of each algorithm for that workload
 Consider 5 processes arriving at time 0, in the order given:

 Consider the FCFS, SJF, and RR (quantum = 10 milliseconds)


scheduling algorithms for this set of processes

Operating System Concepts – 10 th Edition 5.76 Silberschatz, Galvin and Gagne


Deterministic Evaluation

 For each algorithm, calculate minimum average waiting time


 Simple and fast, but requires exact numbers for input, applies
only to those inputs
 FCS, average waiting time = (0 + 10 + 39 + 42 + 49)/5 = 28 ms

 Non-preemptive SFJ, average waiting time = (10 + 32 + 0 + 3 +


20)/5 =13ms

 RR, average waiting time = (0 + 32 + 20 + 23 + 40)/5 = 23ms:

Operating System Concepts – 10 th Edition 5.77 Silberschatz, Galvin and Gagne


Queueing Models
 Describes the arrival of processes, and CPU and I/O bursts
probabilistically
 Commonly exponential, and described by mean
 Computes average throughput, utilization, waiting time,
etc
 Computer system described as network of servers, each with
queue of waiting processes
 Knowing arrival rates and service rates
 Computes utilization, average queue length, average wait
time, etc
 This area of study is called queueing-network analysis

Operating System Concepts – 10 th Edition 5.78 Silberschatz, Galvin and Gagne


Little’s Formula
 n = average queue length
 W = average waiting time in queue
 λ = average arrival rate into queue
 Little’s law – in steady state, processes leaving queue must equal
processes arriving, thus:
n=λxW
 Valid for any scheduling algorithm and arrival distribution
 For example, if on average 7 processes arrive per second, and
normally 14 processes in queue, then average wait time per process =
2 seconds
 Limitations:
 the classes of algorithms and distributions that can be handled are
fairly limited
 The mathematics of complicated algorithms and distributions can
be difficult to work with
 queueing models are often only approximations of real systems,
and
 the accuracy of the computed results may be questionable

Operating System Concepts – 10 th Edition 5.79 Silberschatz, Galvin and Gagne


Simulations
 Simulations more accurate
 Programming a model of a computer system
 The simulator has a variable representing a clock
 As this variable's value is increased, the simulator modifies
the system state to reflect the activities of the devices, the
processes, and the scheduler
 Gather statistics indicating algorithm performance
 Data to drive simulation gathered via
 Random number generator programmed to generate
processes, CPU burst times, arrivals, departures, according
to probability distributions
 Distributions defined mathematically (uniform, exponential,
Poisson) or empirically
 For empirically defined distribution, measurements of the
actual system under study are taken

Operating System Concepts – 10 th Edition 5.80 Silberschatz, Galvin and Gagne


Evaluation of CPU Schedulers by Simulation

 A distribution-driven
simulation may be
inaccurate
 Trace tapes record
sequences of real
events in real systems
 Trace files provide an
excellent way to
compare two
algorithms on exactly
the same set of real
inputs
 Simulations can be
expensive in terms of
computing time

Operating System Concepts – 10 th Edition 5.81 Silberschatz, Galvin and Gagne


Implementation
 Even simulations have limited accuracy
 Just implement new scheduler and test in real systems
 High cost, high risk
 Environments vary
 Approach 1: Most flexible schedulers that can be altered by
system managers or users so that they can be tuned for a
specific application or set of applications
 Different scheduling needs for graphics, web server, file
server
 Solaris provides the dispadmin command to allow the
system administrator to modify the parameters of the
scheduling classes
 Approach 2: APIs to modify priorities of process or thread
 The Java, POSIX, and Windows APIs provide such
functions
 Disadvantage: performance-tuning a system or application
most
Operating System often
Concepts – 10does
th
Edition not result in
5.82improved performance inGalvin
Silberschatz, more and Gagne
End of Chapter 5

Operating System Concepts – 10 th Edition Silberschatz, Galvin and Gagne

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy