Rushdi Shams, Dept of CSE, KUET 1
Operating Systems
CPU Scheduling
Version 1.0
Rushdi Shams, Dept of CSE, KUET 2
Basic Concepts
The objective of multiprogramming is to run
processes all time, keeping the CPU busy.
Uniprocessing system is different. Only one process
can run at a time- means processes have to wait until
the CPU is free.
Rushdi Shams, Dept of CSE, KUET 3
Basic Concepts
A process is executed until the completion of an IO
request
The CPU then sits idle and all this waiting time is
wasted
With multiprogramming, we keep several processes
in memory at one time.
When one process has to wait, the OS takes the CPU
away from that process and gives the CPU to another
process
Rushdi Shams, Dept of CSE, KUET 4
Basic Concepts
Almost all computer resources are scheduled before
use. The CPU is one of the primary resources. Thus,
its scheduling is central to operating systems
Rushdi Shams, Dept of CSE, KUET 5
CPU burst and IO burst
Rushdi Shams, Dept of CSE, KUET 6
CPU Scheduler
 Selects from the processes in memory that are ready to execute,
and allocates the CPU to one of them
 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
Rushdi Shams, Dept of CSE, KUET 7
CPU Scheduler
In case of Non-preemptive scheduling, the process
does not release the CPU unless it is finished (or
terminated) or going to the waiting state.
Rushdi Shams, Dept of CSE, KUET 8
Dispatcher
Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves:
switching context
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
Rushdi Shams, Dept of CSE, KUET 9
Scheduling Criteria
CPU utilization
How much of the CPU is in operation, how much of it
is utilized to serve processes.
Measured in Percentage
Should be maximized
Rushdi Shams, Dept of CSE, KUET 10
Scheduling Criteria
Throughput
Number of processes executed and served per unit
time.
Measured in process per unit time
Should be maximized
Rushdi Shams, Dept of CSE, KUET 11
Scheduling Criteria
Turnaround time
Amount of time to execute a particular process
Interval between the submission of the process and
its completion
Should be minimized
Rushdi Shams, Dept of CSE, KUET 12
Scheduling Criteria
Waiting time
amount of time a process has been waiting in the
ready queue
Should be minimized
Rushdi Shams, Dept of CSE, KUET 13
Scheduling Criteria
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)
Should be minimized
Rushdi Shams, Dept of CSE, KUET 14
First Come, First Served (FCFS)
The simplest CPU Scheduling algorithm
The process that requests CPU first is allocated the
CPU first
Easily managed with a FIFO queue
Rushdi Shams, Dept of CSE, KUET 15
FCFS
Proces 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:
 Waiting time for P1 = 0; P2 = 24; P3= 27
 Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
Rushdi Shams, Dept of CSE, KUET 16
FCFS
Proces Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order
P2 , P3 , P1
 The Gantt chart for the schedule is:
 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
P1P3P2
63 300
Rushdi Shams, Dept of CSE, KUET 17
Shortest Job First (SJF)
 Associate with each process the length of its next CPU burst.
Use these lengths to schedule the process with the shortest
time
 Two schemes:
 non-preemptive – once CPU given to the process it cannot be
preempted until 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 also called Shortest-Remaining-Time-First (SRTF)
 SJF is optimal – gives minimum average waiting time for a given
set of processes
Rushdi Shams, Dept of CSE, KUET 18
SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
 SJF (non-preemptive)
 Average waiting time = (0 + 6 + 3 + 7)/4 = 4
P1 P3 P2
73 160
P4
8 12
Rushdi Shams, Dept of CSE, KUET 19
SRTF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
 SJF (preemptive)
• Average waiting time = (9 + 1 + 0 +2)/4 = 3
P1 P3P2
42 110
P4
5 7
P2 P1
16
Rushdi Shams, Dept of CSE, KUET 20
Priority Scheduling
The SJF Algorithm is special case of Priority
Scheduling
A priority is associated with each process and the CPU
is given to the process with higher priority
If processes have equal priority, FCFS is followed
SJF is priority algorithm where
priority p = inverse of CPU burst
Priorities are generally some fixed range of numbers
such as 0 to 7 or 0 to 4,095
Rushdi Shams, Dept of CSE, KUET 21
Priority Scheduling
However, there is no agreement on the highest
priority- if it would be 0 or 4,095
Priorities can be external or internal
Internal priorities are number of files required to
execute the process, its memory usage, resources
required, time to compute, etc.
External priorities are set by outside computer-
mainly by human based on the choice of process,
economical factors, etc.
Rushdi Shams, Dept of CSE, KUET 22
Priority Scheduling
The Average Waiting Time is (6+0+16+18+1)/5=8.2 ms
Rushdi Shams, Dept of CSE, KUET 23
Priority Scheduling: Starvation
 Higher priority schedules prohibits lower priority
schedules for getting the CPU
 There are two possibilities-
1. If no mechanism is applied, lower priority schedules
will get CPU on Friday morning (that’s the holiday)
2. If mechanism applied, lower priority schedules will
be swapped away by the CPU- those that did not get
a chance for a specific period
 IBM 7094 was shut down in 1973… during theIBM 7094 was shut down in 1973… during the
shut down, MIT found a lower prioritizedshut down, MIT found a lower prioritized
process still ignored that was submitted in 1967!process still ignored that was submitted in 1967!
Rushdi Shams, Dept of CSE, KUET 24
Priority Scheduling: Aging
Technique to increase the priority of the processes
If priorities are ranged from 0 to 127 (127 is the highest
priority), then we can increase the priority of a process
for every 15 minutes.
The lowest prioritized process will now take no more
than 32 hours to get the CPU! 
Rushdi Shams, Dept of CSE, KUET 25
Round-Robin (RR) Scheduling
Similar to FCFS but pre-emption is added to switch
between processes
A smaller unit of time called time quantum (or time
slice) is defined generally from 10 to 100 ms.
The ready queue is treated as circular queue
Scheduler goes around the ready queue, allots CPU to
each process for a time interval of up to 1 time
quantum
Rushdi Shams, Dept of CSE, KUET 26
Round-Robin (RR) Scheduling
We need to treat the ready queue as FIFO
New processes are added at the end of the queue.
The CPU scheduler picks the first process from the
queue, sets a timer to interrupt the process after 1
time quantum and dispatches the process
Rushdi Shams, Dept of CSE, KUET 27
RR Scheduling
2 things may happen-
The process may have a CPU burst less than time
quantum- the process will release the CPU
voluntarily.
CPU burst of the process is longer than time
quantum-the process will be put at the end of the
queue.
Rushdi Shams, Dept of CSE, KUET 28
RR Scheduling
Time quantum is of 4 ms
Average waiting time: ((10-4)+4+7)/3 = 5.66 ms
Typically, higher average turnaround than SJF, but
better response
Rushdi Shams, Dept of CSE, KUET 29
RR Scheduling
The performance heavily depends on the size of time
quantum
If the time quantum is very large, at one extreme, it
would be similar to FCFS
If the time quantum is very small, the RR approach is
called processor sharing- as it seems every process has
its own CPU
Smaller time quantum requires more context
switching.
Rushdi Shams, Dept of CSE, KUET 30
Context switch with Quantum
Rushdi Shams, Dept of CSE, KUET 31
Turnaround time with Quantum
Rushdi Shams, Dept of CSE, KUET 32
80% of the CPU bursts
should be shorter than the
time quantum

Lecture 5, 6 and 7 cpu scheduling

  • 1.
    Rushdi Shams, Deptof CSE, KUET 1 Operating Systems CPU Scheduling Version 1.0
  • 2.
    Rushdi Shams, Deptof CSE, KUET 2 Basic Concepts The objective of multiprogramming is to run processes all time, keeping the CPU busy. Uniprocessing system is different. Only one process can run at a time- means processes have to wait until the CPU is free.
  • 3.
    Rushdi Shams, Deptof CSE, KUET 3 Basic Concepts A process is executed until the completion of an IO request The CPU then sits idle and all this waiting time is wasted With multiprogramming, we keep several processes in memory at one time. When one process has to wait, the OS takes the CPU away from that process and gives the CPU to another process
  • 4.
    Rushdi Shams, Deptof CSE, KUET 4 Basic Concepts Almost all computer resources are scheduled before use. The CPU is one of the primary resources. Thus, its scheduling is central to operating systems
  • 5.
    Rushdi Shams, Deptof CSE, KUET 5 CPU burst and IO burst
  • 6.
    Rushdi Shams, Deptof CSE, KUET 6 CPU Scheduler  Selects from the processes in memory that are ready to execute, and allocates the CPU to one of them  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
  • 7.
    Rushdi Shams, Deptof CSE, KUET 7 CPU Scheduler In case of Non-preemptive scheduling, the process does not release the CPU unless it is finished (or terminated) or going to the waiting state.
  • 8.
    Rushdi Shams, Deptof CSE, KUET 8 Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context 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
  • 9.
    Rushdi Shams, Deptof CSE, KUET 9 Scheduling Criteria CPU utilization How much of the CPU is in operation, how much of it is utilized to serve processes. Measured in Percentage Should be maximized
  • 10.
    Rushdi Shams, Deptof CSE, KUET 10 Scheduling Criteria Throughput Number of processes executed and served per unit time. Measured in process per unit time Should be maximized
  • 11.
    Rushdi Shams, Deptof CSE, KUET 11 Scheduling Criteria Turnaround time Amount of time to execute a particular process Interval between the submission of the process and its completion Should be minimized
  • 12.
    Rushdi Shams, Deptof CSE, KUET 12 Scheduling Criteria Waiting time amount of time a process has been waiting in the ready queue Should be minimized
  • 13.
    Rushdi Shams, Deptof CSE, KUET 13 Scheduling Criteria 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) Should be minimized
  • 14.
    Rushdi Shams, Deptof CSE, KUET 14 First Come, First Served (FCFS) The simplest CPU Scheduling algorithm The process that requests CPU first is allocated the CPU first Easily managed with a FIFO queue
  • 15.
    Rushdi Shams, Deptof CSE, KUET 15 FCFS Proces 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:  Waiting time for P1 = 0; P2 = 24; P3= 27  Average waiting time: (0 + 24 + 27)/3 = 17 P1 P2 P3 24 27 300
  • 16.
    Rushdi Shams, Deptof CSE, KUET 16 FCFS Proces Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order P2 , P3 , P1  The Gantt chart for the schedule is:  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 P1P3P2 63 300
  • 17.
    Rushdi Shams, Deptof CSE, KUET 17 Shortest Job First (SJF)  Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time  Two schemes:  non-preemptive – once CPU given to the process it cannot be preempted until 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 also called Shortest-Remaining-Time-First (SRTF)  SJF is optimal – gives minimum average waiting time for a given set of processes
  • 18.
    Rushdi Shams, Deptof CSE, KUET 18 SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4  SJF (non-preemptive)  Average waiting time = (0 + 6 + 3 + 7)/4 = 4 P1 P3 P2 73 160 P4 8 12
  • 19.
    Rushdi Shams, Deptof CSE, KUET 19 SRTF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4  SJF (preemptive) • Average waiting time = (9 + 1 + 0 +2)/4 = 3 P1 P3P2 42 110 P4 5 7 P2 P1 16
  • 20.
    Rushdi Shams, Deptof CSE, KUET 20 Priority Scheduling The SJF Algorithm is special case of Priority Scheduling A priority is associated with each process and the CPU is given to the process with higher priority If processes have equal priority, FCFS is followed SJF is priority algorithm where priority p = inverse of CPU burst Priorities are generally some fixed range of numbers such as 0 to 7 or 0 to 4,095
  • 21.
    Rushdi Shams, Deptof CSE, KUET 21 Priority Scheduling However, there is no agreement on the highest priority- if it would be 0 or 4,095 Priorities can be external or internal Internal priorities are number of files required to execute the process, its memory usage, resources required, time to compute, etc. External priorities are set by outside computer- mainly by human based on the choice of process, economical factors, etc.
  • 22.
    Rushdi Shams, Deptof CSE, KUET 22 Priority Scheduling The Average Waiting Time is (6+0+16+18+1)/5=8.2 ms
  • 23.
    Rushdi Shams, Deptof CSE, KUET 23 Priority Scheduling: Starvation  Higher priority schedules prohibits lower priority schedules for getting the CPU  There are two possibilities- 1. If no mechanism is applied, lower priority schedules will get CPU on Friday morning (that’s the holiday) 2. If mechanism applied, lower priority schedules will be swapped away by the CPU- those that did not get a chance for a specific period  IBM 7094 was shut down in 1973… during theIBM 7094 was shut down in 1973… during the shut down, MIT found a lower prioritizedshut down, MIT found a lower prioritized process still ignored that was submitted in 1967!process still ignored that was submitted in 1967!
  • 24.
    Rushdi Shams, Deptof CSE, KUET 24 Priority Scheduling: Aging Technique to increase the priority of the processes If priorities are ranged from 0 to 127 (127 is the highest priority), then we can increase the priority of a process for every 15 minutes. The lowest prioritized process will now take no more than 32 hours to get the CPU! 
  • 25.
    Rushdi Shams, Deptof CSE, KUET 25 Round-Robin (RR) Scheduling Similar to FCFS but pre-emption is added to switch between processes A smaller unit of time called time quantum (or time slice) is defined generally from 10 to 100 ms. The ready queue is treated as circular queue Scheduler goes around the ready queue, allots CPU to each process for a time interval of up to 1 time quantum
  • 26.
    Rushdi Shams, Deptof CSE, KUET 26 Round-Robin (RR) Scheduling We need to treat the ready queue as FIFO New processes are added at the end of the queue. The CPU scheduler picks the first process from the queue, sets a timer to interrupt the process after 1 time quantum and dispatches the process
  • 27.
    Rushdi Shams, Deptof CSE, KUET 27 RR Scheduling 2 things may happen- The process may have a CPU burst less than time quantum- the process will release the CPU voluntarily. CPU burst of the process is longer than time quantum-the process will be put at the end of the queue.
  • 28.
    Rushdi Shams, Deptof CSE, KUET 28 RR Scheduling Time quantum is of 4 ms Average waiting time: ((10-4)+4+7)/3 = 5.66 ms Typically, higher average turnaround than SJF, but better response
  • 29.
    Rushdi Shams, Deptof CSE, KUET 29 RR Scheduling The performance heavily depends on the size of time quantum If the time quantum is very large, at one extreme, it would be similar to FCFS If the time quantum is very small, the RR approach is called processor sharing- as it seems every process has its own CPU Smaller time quantum requires more context switching.
  • 30.
    Rushdi Shams, Deptof CSE, KUET 30 Context switch with Quantum
  • 31.
    Rushdi Shams, Deptof CSE, KUET 31 Turnaround time with Quantum
  • 32.
    Rushdi Shams, Deptof CSE, KUET 32 80% of the CPU bursts should be shorter than the time quantum