CPU scheduling
CPU scheduling
Principles of Operating
System
Course Code CIT11
CPU Scheduling
• Scheduling Criteria
• Scheduling Algorithms
• Thread Scheduling
• Multiple-Processor Scheduling
Objectives
Basic Concepts
• Maximum CPU utilization obtained with
multiprogramming
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
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
• Max throughput
P2 P3 P1
0 3 6 30
Example of SJF
ProcessArriva l Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
P4 P1 P3 P2
0 3 9 16 24
Example of Shortest-remaining-time-first
• Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessA arri 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
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
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
Wedad Al-Sorori 14 September 2017 Introduction 20/51
CIT11: Principles of Operating System
• 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
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
• Kernel thread scheduled onto available CPU is system-
contention scope (SCS) – competition among all threads in
system
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
• Can be limited by OS – Linux and Mac OS X only allow
PTHREAD_SCOPE_SYSTEM
Multiple-Processor Scheduling
• CPU scheduling 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, or each has its own private queue
of ready processes
• Currently, most common
• Processor affinity – process has affinity for processor on which it is
currently running
• Soft affinity
• Hard affinity
• Variations including processor sets
Multicore Processors
Priority-based Scheduling
• For real-time scheduling, scheduler must support preemptive,
priority-based scheduling
• But only guarantees soft real-time
Summary
• CPU scheduling is the base of multiprogrammed operating
systems.
• Several scheduling algorithms can be used with different
environments and can be evaluated according to some
scheduling criteria such as average waiting time.
Thanks