0% found this document useful (0 votes)
41 views53 pages

CPU Scheduling

Uploaded by

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

CPU Scheduling

Uploaded by

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

CPU Scheduling

2024-9-19
2024-9-20
2024-9-21
2024-9-25
2024-9-26
2024-9-27
2024-9-28
CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling
• Real-Time CPU Scheduling
• Operating Systems Examples
• Algorithm Evaluation
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
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
Histogram of CPU-burst Times
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 (aka cooperative)
• All other scheduling is preemptive
• Consider access to shared data
• Consider preemption while in kernel mode
• Consider interrupts occurring during crucial OS activities
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
• Linux command
vmstat 1 3
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – Number of processes that complete their execution per time unit
• Turnaround time – The interval from the time of submission of a process to the
time of completion is the turnaround time.
Turnaround time
= waiting time in ready queue + execution time on CPU + time doing I/O

• 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)
Scheduling Algorithm Optimization Criteria
• Maximize CPU utilization • Maximize (average) CPU utilization
• Maximize throughput • Maximize (average) throughput
• Minimize turnaround time • Minimize (average) turnaround time
• Minimize waiting time • Minimize (average) waiting time
• Minimize response time • Minimize (maximum) response time
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
First- Come, First-Served (FCFS) Scheduling
Suppose that the processes arrive in the order, with previous burst times:
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 - all processes wait behind big process to leave CPU
• Consider one CPU-bound and many I/O-bound processes
• FCFS non-preemptive – disastrous for interactive systems
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
Example of SJF
ProcessArriva l TimeBurst 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


Determining Length of Next CPU Burst
• 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 (more technically, exponentially weighted moving average, EWMA)
1. t n  actual length of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :  n 1   t n  1    n .

• Commonly, α set to ½
• Preemptive version called shortest-remaining-time-first
Examples of Exponential Averaging
•  =0
• n+1 = n
• Recent history does not count
•  =1
• n+1 =  tn
• Only the actual last CPU burst counts
• 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
Prediction of the Length of the Next CPU
Burst
Example of Shortest-remaining-time-first
(Preemptive SJF)
• Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst 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


• For non preemptive SJF, 7.75 msec
For non preemptive SJF
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority
• Preemptive
• Nonpreemptive
• SJF is priority scheduling where priority is the inverse of predicted
next CPU burst time (The larger the CPU burst, the lower the priority)
• Problem  Starvation – low priority processes may never execute
• Solution  Aging – as time progresses increase the priority of the
process
Priority Scheduling (contd…)
• Some fixed range of numbers, such as 0 to 7, represents priority
• Some systems use low numbers to represent low priority; others use
low numbers for high priority – no general agreement
• In our discussion: Smallest integer  highest priority
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


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.
• 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.
• Timer interrupts every quantum to schedule next process
• Performance
• q large  FIFO
• q small  q must be large with respect to context switch, otherwise overhead
is too high
Round Robin Scheduling:
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

• Typically, higher average turnaround than SJF, but better response


• q should be large compared to context switch time
• q usually 10ms to 100ms, context switch < 10 usec
Time Quantum and Context Switch Time
Turnaround Time Varies With The Time
Quantum

80% of CPU bursts


should be shorter than q
• Time quantum should be large with respect to the context switch
time.
• In general, the average turnaround time can be improved if most
processes finish their next CPU burst in a single time quantum
• (consider three process of 10 ms each. And consider 1 ms time
quanta, and then 10 ms quanta).
• If the time quantum is too large, RR scheduling degenerates to an
FCFS policy
Multiprocessor Scheduling

Symmetric multiprocessing architecture


• By increasing the number of processors, we expect to get more work
done in less time
• Speed-up ratio: Time ( 1 processor ) / Time ( “ N ” processors )
• Expected value ≈ N
• Overhead incurred in keeping all parts working correctly plus
contention for shared resources Speedup less than N
• Symmetric multiprocessing (SMP): Each peer CPU processor
performs all tasks, including operating-system functions and user
processes
• Each CPU processor has its own set of registers, as well as a private
(local) cache
• All processors share physical memory over the system bus
• Since the CPUs are separate, one may be sitting idle while another is
overloaded, resulting in inefficiencies
• Solution: share certain data structures and resources, like cache and
memory
A dual-core design with two cores on the same chip.
NUMA: non-uniform memory access
• Contention for the system bus is a
bottleneck for the multiprocessor system

Alternative approach:
• Provide each CPU (or group of CPUs) with its
own local memory that is accessed via a small,
fast local bus, and
• Connect the CPUs by a shared system
interconnect, so that all CPUs share one
physical address space
• Advantage: when a CPU accesses its local
memory, not only is it fast, but there is also no
contention over the system interconnect
Multiple-Processor Scheduling
• CPU scheduling more complex when multiple CPUs are available
• Multiprocess may be any one of the following architectures:
• Multicore CPUs
• Multithreaded cores
• NUMA systems
• Heterogeneous multiprocessing
Multithreading
• Most modern applications are multithreaded
• Multiple tasks with the application can be implemented by separate
threads. E.g. a word processor can
• Update display
• Fetch data
• Spell checking
• Answer a network request
• Process creation is heavy-weight while thread creation is light-weight
• Can simplify code, increase efficiency
• Kernels are also generally multithreaded
Single and Multithreaded Processes
• A thread is a basic unit of CPU utilization
• It comprises a thread ID, a program counter (PC), a register set, and a
stack
• It shares with other threads belonging to the same process its code
section, data section, and other operating-system resources, such as
open files and signals
• A traditional process has a single thread of control
Multithreaded Server Architecture
Concurrency vs. Parallelism
• Concurrency supports more than one task making progress by allowing all the
tasks to make progress
• Single processor / core, scheduler providing concurrency

• Parallelism implies a system can perform more than one task simultaneously
User Threads and Kernel Threads
• User threads - management done by user-level threads library
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
• Kernel threads - Supported by the Kernel
• Examples – virtually all general-purpose operating systems, including:
• Windows
• Linux
• Mac OS X
• iOS
• Android
User and Kernel Threads
Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
• Many user-level threads mapped to single kernel thread
• One thread blocking causes all to block
• Multiple threads may not run in parallel on multicore system because
only one may be in kernel at a time
• Few systems currently use this model
• Examples:
• Solaris Green Threads
• GNU Portable Threads
One-to-One
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Number of threads per process sometimes restricted due to overhead
• Examples
• Windows
• Linux
Many-to-Many Model
• Allows many user level threads to be mapped to many kernel threads
• Allows the operating system to create a sufficient number of kernel
threads
• Windows with the ThreadFiber package
• Otherwise not very common
Multiple-Processor Scheduling
Asymmetric multiprocessing
• A multiprocessor system has all scheduling decisions, I/O processing,
and other system activities handled by a single processor — the
master server. The other processors execute only user code.
• This approach is simple because only one core accesses the system
data structures, reducing the need for data sharing.
• The drawback: the master server becomes a potential bottleneck
where overall system performance may be reduced
Multiple-Processor Scheduling
• Symmetric multiprocessing (SMP) is where each processor is self scheduling.
Scheduler of each processor examines the ready queue
Two alternatives:
• All threads (processes) may be in a common ready queue (a). (Race condition.
Need for locking the queue)
• Each processor may have its own private queue of threads (b). (More common.
Needs load balancing algos)
Multicore Processors
• Recent trend to place multiple processor cores on same physical chip
• Faster and consumes less power
• Multiple threads per core also growing
• Takes advantage of memory stall to make progress on another thread while
memory retrieve happens
• Figure
Multithreaded Multicore System
• Each core has > 1 hardware threads.
• If one thread has a memory stall, switch to another thread!
Multithreaded Multicore System
• Chip-multithreading (CMT) assigns
multiple hardware threads to each core.
(Intel refers to this as hyperthreading)
• Intel i7: On a quad-core system with 2
hardware threads per core, the
operating system sees 8 logical
processors
• Oracle Sparc M7 processor: 8 threads
per core, with 8 cores per processor,
thus providing the operating system
with 64 logical CPUs
Multithreaded Multicore System
• Two levels of
scheduling:
1. How operating
system decides
which software
thread to run on a
logical CPU

2. How each core


decides which
hardware thread to
run on the physical
core.
Multiple-Processor Scheduling – Load
Balancing
• If SMP, need to keep all CPUs loaded for efficiency
• Load balancing attempts to keep workload evenly distributed
• Push migration – periodic task checks load on each processor, and if
found pushes task from overloaded CPU to other CPUs
• Pull migration – idle processors pulls waiting task from busy
processor
Multiple-Processor Scheduling – Processor Affinity
• When a thread has been running on one processor, the cache contents of
that processor stores the memory accesses by that thread.
• We refer to this as a thread having affinity for a processor (i.e., “processor
affinity”)
• Load balancing may affect processor affinity as a thread may be moved
from one processor to another to balance loads, yet that thread loses the
contents of what it had in the cache of the processor it was moved off of.
• Soft affinity – the operating system attempts to keep a thread running on
the same processor, but no guarantees.
• Hard affinity – allows a process to specify a set of processors it may run on
NUMA and CPU Scheduling
• If the operating system is NUMA-aware, it will assign memory closest to
the CPU the thread is running on.

• Load balancing and minimizing memory access times are competing goals

You might also like