CPU Scheduling
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
P2 P3 P1
0 3 6 30
• 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
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
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
• Load balancing and minimizing memory access times are competing goals