2.4 CPU Scheduling
2.4 CPU Scheduling
Deepika R Sahu
Deepika R Sahu 1
CPU-I/O Burst Cycle
• Maximum CPU utilization obtained with
multiprogramming
Deepika R Sahu 2
Alternate Sequence of CPU and I/O Burst
Deepika R Sahu 3
CPU Scheduler
• Whenever the CPU becomes idle, the short term
scheduler selects a process from the processes in
memory that are ready to execute and allocates the
CPU to that process.
Deepika R Sahu 4
Preemptive Scheduling
• CPU scheduling decisions may take place under the
following four cases:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
• When scheduling takes place only under
circumstances 1 and 4, we say that the scheduling is
non-preemptive or cooperative. Otherwise, it is
preemptive.
Deepika R Sahu 5
Cont..
Non-Preemptive Scheduling
• Once the CPU has been allocated to a process, the
process keeps CPU until it releases the CPU either by
terminating or by switching to the waiting state.
Preemptive Scheduling
• Once the CPU has been allocated to a process, then
CPU can jump to next process, if the first process has
not completed its execution is called preemptive
scheduling
Deepika R Sahu 6
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
Deepika R Sahu 7
Scheduling Criteria
• CPU utilization: To keep the CPU as busy as
possible.
– CPU utilization range from 0 to 100%.
• Throughput – The number of processes that
complete their execution per unit time is called
throughput.
• Turnaround time – amount of time to execute a
particular process. The interval from the time of
submission of a process to the time of completion is
called turnaround time.
Deepika R Sahu 8
Scheduling Criteria
• Waiting time – The amount of time that a process
has been waiting in the ready queue is called waiting
time.
– It is the sum of the periods spent waiting in the
ready queue.
• Response time – The amount of time it takes from
when a request was submitted until the first response
is produced, not output (for time-sharing
environment)
Deepika R Sahu 9
Scheduling Algorithm Optimization Criteria
• Maximize throughput
Deepika R Sahu 11
First- Come, First-Served (FCFS)
Scheduling
• In FCFS, the process that request CPU first is
allocated the CPU first .
• Implementation is managed with a FIFO queue.
• When a process enters the ready queue, its PCB is
linked onto the tail of the queue.
• When the CPU is free, it is allocated to the process at
the head of the queue
• The running process is then removed from the queue.
Deepika R Sahu 12
FCFS
Process Burst Time
P1 24 ms
P2 3 ms
P3 3 ms
• Suppose that the processes arrive at t = 0 ms 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
Deepika R Sahu 14
Tasks-1
• Consider the following processes P1, P2, P3 with their CPU
burst time:
Process Arrival Time Burst Time(ms)
P1 0 10
P2 0 5
P3 0 5
Deepika R Sahu 16
Tasks-3
• Consider the following set of processes with arrival
time and CPU burst time:
Process Arrival Time(ms) Run Time(ms)
P1 10.00 2.00
P2 10.10 1.00
P3 10.25 0.25
P4 12.00 3.25
Deepika R Sahu 17
Note
• Lower the average turnaround time, better it is.
Deepika R Sahu 18
FCFS Advantages:
• FCFS is non-preemptive
• Suitable for batch systems
• Simple, Easy to implement.
FCFS Disadvantages:
• Not suitable for time-sharing systems
• Proper mix of CPU bound and I/O bound jobs is
required.
• Results in poor performance and low throughput.
Deepika R Sahu 19
Criteria
• Turnaround time = Finish Time – Arrival Time
Deepika R Sahu 20
Shortest-Job-First (SJF) Scheduling
• Use the lengths of CPU bursts to schedule the process
with shortest time.
• Two schemes:
– Non-preemptive : once CPU is given to a process
it cannot be preempted until it completes.
– Preemptive : if a new process arrives with CPU
burst length less than remaining time of current
executing process, preempt.
• This scheme is know as Shortest-Remaining-
Time-First (SRTF).
• SJF is optimal.
Deepika R Sahu 21
Example of Non-Preemptive SJF
Process Burst Time
P1 6 All processes arrive at 0 ms.
P2 8
P3 7
P4 3
• SJF (non-preemptive)
P4 P1 P3 P2
0 3 9 16 24
0 3 7 8 12 16
0 2 4 5 7 11 16
Deepika R Sahu 25
SJF
Advantages:
• This algorithm gives minimum average waiting time
so it is an optimal algorithm.
Disadvantages:
1. Difficult to know the length of next CPU burst time.
2. Cannot be implemented at the level of short term
CPU scheduler
3. Big jobs are waiting for CPU. This results in aging
problem.
Deepika R Sahu 26
Priority Scheduling
• A priority number (integer) is associated with each
process
Deepika R Sahu 27
Priority Scheduling
• The priority is the inverse of the next CPU burst.
• The larger the CPU burst , the lower the priority and
vice-versa.
Deepika R Sahu 28
Priority Scheduling
• Priority Scheduling can be either preemptive or non-
preemptive.
• When a process arrives at the ready queue, its priority
is compared with the priority of the currently running
process.
• A Non-Preemptive priority scheduling algorithm
will put the new process at the head of the ready
queue.
• A Preemptive priority scheduling algorithm will
preempt the CPU if the priority of the newly arrived
process is higher than the priority of the currently
running process
Deepika R Sahu 29
Example-I
Deepika R Sahu 30
Example-II
Deepika R Sahu 31
Example-II
Deepika R Sahu 33
Example-IV
Process no Arrival Time Burst Time Priority
1 0 3 3
2 1 6 4
3 2 1 9
4 3 2 7
5 4 4 8
Deepika R Sahu 34
Example - IV
Deepika R Sahu 35
Priority Scheduling
• In heavily loaded system, a steady stream of higher
priority processes can prevent a low priority process
from ever getting the CPU. [starvation]
Deepika R Sahu 36
Round Robin Scheduling
• Each process gets a small unit of CPU time (time
quantum).
Deepika R Sahu 38
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Deepika R Sahu 39
Example-II
Time Quantum = 4
Deepika R Sahu 42
Example-IV
Process Arrival Time Service Time
1 0 8
2 10 4
3 5 9
4 7 5
Time Quantum = 4
Deepika R Sahu 43
Example-V
Process Arrival Time Service Time
1 13 8
2 10 4
3 5 9
4 14 5
Time Quantum = 4
Deepika R Sahu 44
Round Robin Scheduling
• RR scheduling is preemptive.
• The performance of RR depends on the size of time
quantum.
1. If the time quantum is extremely large, the RR
policy is same as FCFS policy.
2. If the time quantum is extremely small, the RR
approach is called processor sharing. Each of the n
processes has its own processor running at 1/n th
speed of real processor.
Deepika R Sahu 45
Multilevel Queue Scheduling
• Another class of scheduling algorithms has been
created in which processes are classified into different
group.
• Common division is made between :
Foreground(interactive) processes
Background (batch) processes
Deepika R Sahu 46
Multilevel Queue Scheduling
• A multilevel queue scheduling algorithm partitions
the ready queue into several separate queues.
Deepika R Sahu 48
Multilevel Queue Scheduling
• Scheduling must be done between the queues:
1. Fixed priority scheduling:
• Each queue has absolute priority over lower priority
queues.
• No process in the batch queue could run unless
the queues for system processes, interactive
processes & interactive editing processes were all
empty.
• If an interactive editing process entered the ready
queue while a batch process are running, the batch
process would be preempted.
Deepika R Sahu 49
Multilevel Queue Scheduling
2. 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.
Advantages:
1. Processes permanently assigned to queue
2. Low scheduling overhead.
Disadvantages
3. Starvation of process occurs.
4. Processes are never allowed to change their queues.
Deepika R Sahu 50
Multilevel Feedback Queue Scheduling
• A process can move between the various queues.
• Idea : Separate process according to the
characteristics of their CPU burst.
• If a process uses too much CPU time , it will be
moved to a lower priority queue.
• This scheme leaves I/O bound and interactive
processes in the higher priority queues.
• A process that waits long in a lower priority queue
may be moved to a higher priority queue. This forms
aging prevents starvation.
Deepika R Sahu 51
Multilevel Feedback Queue Scheduling
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
Deepika R Sahu 52
Multilevel Feedback Queue Scheduling
• 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
Deepika R Sahu 53
Multilevel Feedback Queue Scheduling
• 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
Deepika R Sahu 54
GO THROUGH ALL
CPU SCHEDULING
ALGORITHMS
CAREFULLY.☻☻☻
Deepika R Sahu 55