Lect5 CPU Scheduling
Lect5 CPU Scheduling
CPU Scheduling
Basic Concepts
CPU Scheduling
• It is the basis of multi-programmed operating
systems
• The operating system can make the computer
more productive
• Maximum CPU utilization obtained with
multiprogramming
CPU–I/O Burst Cycle
• These are process execution consists of a cycle
of CPU execution and I/O wait
Basic Concepts
CPU Scheduling
CPU–I/O Burst Cycle
Properties of Process
1. Cycle of CPU Execution
2. I/O Wait
CPU Burst Distribution
CPU Scheduler
• It is a part of operating system that is responsible
for dividing which process gets the CPU burst,
after a process through its sharing.
• Selects from among the processes in memory that
are ready to execute, and allocates the CPU to
one of them
Implementation of Ready Queue
1. FIFO (First – In First – Out) Queue
2. Priority Queue
3. Tree
4. Unordered Linked List
CPU Scheduler
• 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
o Scheduling under 1 and 4 is nonpreemptive
o All other scheduling is preemptive
CPU Scheduler
Scheduling Schemes
Non – Preemptive Scheduling
o The CPU has been allocated to a process, then
process keeps the CPU until it releases the CPU
either by terminating or by switching to waiting
state.
o It is the only method that can be used on certain
hardware platforms, because it does not require
the special hardware.
o Once CPU given to the process it cannot be
preempted until completes its CPU burst
Example: Microsoft System
CPU Scheduler
Preemptive Scheduling
o Operating system interrupts and executing
process when it sees fit
o If a new process arrives with CPU burst length
less than remaining time of current executing
process, preempt.
o This scheme is known as the
Shortest-Remaining-Time-First (SRTF)
Dispatcher
• Selects a process from the ready queue
Dispatcher Module – gives control of the CPU to
the process selected by the short-term scheduler
Functions:
– 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
Scheduling Criteria
1. CPU Utilization – is the percentage of time that
the CPU is busy.
2. System Throughput – is the number of jobs
completed per unit of time
3. Turnaround Time – is the elapsed time from
the time that the job is submitted for execution
to the time it is completed.
4. Waiting Time – is the amount of time spent by
a job in ready state.
Scheduling Criteria
Example:
Process Burst Time
P1 24
P2 3
P3 3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
Scheduling Algorithm
First-Come, First-Served (FCFS) Scheduling
0 3 6 30
Scheduling Algorithm
First-Come, First-Served (FCFS) Scheduling
• Waiting Time : P1 = 6; P2 = 0; P3 = 3
• Average Waiting Time: (6 + 0 + 3)/3 = 3
Scheduling Algorithm
0 1 5 9 16
0 3 8 12 16
7
Scheduling Algorithm
Shortest-Job-First (SJF) Scheduling
• Waiting Time:
Process= Waiting Time – Arrival Time
P1 = 0 – 0 = 0
P2 = 8 – 2 = 6
P3 = 7 – 4 = 3
P4 = 12 – 5 = 7
Average Waiting Time = (0 + 6 + 3 + 7)/4 = 4
Scheduling Algorithm
Shortest-Job-First (SJF) Scheduling (Preemptive)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• Gantt Chart:
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Scheduling Algorithm
Shortest-Job-First (SJF) Scheduling (Preemptive)
• Waiting Time:
P1 = (11 – 2) – 0 = 9
P2 = (5 – 2) – 2 = 1
P3 = 4 – 4 = 0
P4 = 7 – 5 = 2
• Average Waiting Time = (9 + 1 + 0 +2)/4 = 3
Scheduling Algorithm
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
• SJF is a priority scheduling where priority is the
predicted next CPU burst time
Scheduling Algorithm
Priority Scheduling
• Problem Starvation – low priority processes may
never execute
• Solution Aging – as time progresses increase
the priority of the process
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
Scheduling Algorithm
Priority Scheduling
• Gantt Chart:
P2 P5 P1 P3 P4
0 1 6 9 11 18
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Average
0 Waiting
20 37 Time:
57 77 97 117 121 134 154 162
P1 = (77 – 20) + (121 – 97)= 81
P2 = 20
P3 = 37 + (97 – 57) + (134 – 117)= 94
P4 = 57 + (117 – 77) = 97
AWT = (81 + 20 + 94 + 97) / 4 = 73ms
Scheduling Algorithm
Multilevel Queue Scheduling
• Ready queue is partitioned into separate queues:
─ Foreground (interactive)
─ Background (batch)
• Each queue has its own scheduling algorithm
– Foreground – Round Robin
– Background – FCFS
• Scheduling must be done between the queues
– Fixed priority scheduling; (i.e., serve all from foreground
then from background). Possibility of starvation.
Multilevel Queue Scheduling
Scheduling Algorithm
Multilevel Queue Scheduling
• Scheduling must be done between the queues
– 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
Multilevel Feedback Queue
• A process can move between the various queues;
aging can be implemented this way
Scheduling Algorithm
Multilevel Feedback 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
Scheduling Algorithm
Multilevel Feedback Queue
Example:
• 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 .
Scheduling Algorithm
Multilevel Feedback Queue
Example:
• Scheduling
– 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.
Multilevel Feedback Queues
Multiple – Processor Scheduling
• CPU scheduling more complex when multiple
CPUs are available
Homogenous System
• A system where the processor are identical in
terms of functionality
• Any processor available can then be used to run
any processes in the queue
• Homogeneous processors within a multiprocessor
Multiple – Processor Scheduling
Heterogeneous System
• A system where the processor are different
• Only compiled programs for a given processor’s
instruction set could be run on that processor.
Load sharing
• It will provide a separate queue for each processor
Asymmetric multiprocessing
• Only one processor accesses the system data
structures, alleviating the need for data sharing
Real – Time Scheduling
• The scheduling Facility needed to support real –
time computing within a general purpose computer
system
Hard Real – Time System
• A system that required to complete a critical task
within a guaranteed amount of time
• These are composed of special – purpose
software running on hardware dedicated to their
critical process, and lack the full functionality of
modern computers and operating systems
Real – Time Scheduling
Soft Real – Time System
• It is less restrictive
• It requires that critical processes receive priority
over less fortunate ones
• It requires careful design of the scheduler and
related aspect of the operating system
Algorithm Evaluation
Deterministic Modeling
Analytic Evaluation
• It a major class of evaluation methods.
• It uses the given algorithm and the system workload to
produce a formula or number that evaluates the
performance of the algorithm for that workload.
• It one type of analytic evaluation method
• It takes a particular predetermined workload and
defines the performance of each algorithm for
that workload
Algorithm Evaluation
Deterministic Modeling
• Consider the FCFS, SJF, and Round Robin
(quantum = 10 ms), which algorithm would give the
minimum average waiting time?
Example:
Process Burst Time
P1 10
P2 29
P3 3
P4 7
P4 12
Algorithm Evaluation
Queueing Modeling
• It can be used to determine the distribution of
CPU and I/O burst.
• It may be useful in comparing scheduling
algorithms but it has limitations
• The arrival and service distributions are often
defined unrealistic, but mathematically tractable
ways.
Algorithm Evaluation
Simulations Modeling
• It is more accurate evaluation of scheduling
algorithm
• It involves programming a model of the computer
system.
• Software Data Structures represent the major
components of the 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.
CPU Scheduler by Simulation
Algorithm Evaluation
Implementation Modeling
• The only completely accurate way to evaluate a
scheduling algorithm is to code it up, to put it in
the operating system, and se how it works.
• It puts the actual algorithm in the real system for
evaluation under real operating conditions.
• Major Difficulty
a. Cost
b. Environment used will change
Activity