You are on page 1of 55

CPU Scheduling

Dr. Amar Singh


Associate Professor, School of Computer Applications

Operating System Concepts


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 distribution

Operating System Concepts


Alternating Sequence of CPU And I/O Bursts

Operating System Concepts


CPU Scheduler
• Selects from among 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.
• Under NonPreemptive 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.

Operating System Concepts


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.

Operating System Concepts


Performance Criteria
• CPU utilization
– CPU should not be idle.
• Throughput
– Number of processes completed per time unit.
– Complete as many processes as possible per unit time
• Turnaround time
– Turnaround time is interval from the time of submission of a
process to the time of completion.
• Waiting time
– Waiting time is sum of periods spent waiting in the ready
queue.
– Process should not wait long in ready queue.
• Response Time
– Time from the submission of the request until the first response
Operating System Concepts
is produced.
Type of scheduling
• Preemptive
• Non preemptive

Operating System Concepts


Non-Preemptive Scheduling
• Once the CPU has been allocated to a process,
the process can not taken back until it releases
the CPU either by terminating or by switching
to the waiting state.
• Decision to schedule another process is made
only when currently executing process either
switches to waiting state or terminates.

Operating System Concepts


Preemptive Scheduling
• CPU can be taken back from the currently
executing process before its termination and
allocated to some other process.

Operating System Concepts


Scheduling Algorithms
• First come first serve
• Shortest job first (Preemptive and non-preemptive)
– Shortest remaining time first
• Priority scheduling (Preemptive and non-preemptive)
• Round robin scheduling (Preemptive and non-preemptive)
• Multilevel queue scheduling
• Multilevel feedback queue scheduling
• Real time scheduling

Operating System Concepts


First Come First Serve Algorithm
• The process that request the CPU first is allocated the CPU first.
• The processes are executed in the order of their arrival in the ready
queue.
• FCFS is Non-preemptive approach.
– Process continues until burst cycle ends.
• Advantages
– Simple.
– Fair
• Drawbacks
– All processes will wait for the one big process to get off the CPU
– Average waiting time under the FCFS policy is often quite long.

Operating System Concepts


First-Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3

Operating System Concepts


First-Come, First-Served (FCFS) Scheduling

Process Burst Time Waiting Time T.T.


P1 24 0
P2 3 24
P3 3 27
• 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
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17

Operating System Concepts


FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:
P2 P3 P1

0 3 6 30

• Waiting time for P1 = 6; P2 = 0; P3 = 3


• Average waiting time: (6 + 0 + 3)/3 = 3
• Convoy effect short process behind long process
Operating System Concepts
Example: FCFS
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 24 24 24 0 0
P2 0 3 27 27 24 24
P3 0 3 30 30 27 27

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Example: FCFS
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 24 24 24 0 0
P2 1 3 27 26 23 23
P3 29 3 32 3 0 0

P1 P2 idle P3

.0 .24 .27 .29 .32

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Exercise: FCFS
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 28
P2 1 2
P3 4 5

Operating System Concepts


Solution: FCFS
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 28 28 28 0
P2 1 2 30 29 27
P3 4 5 35 31 26

P1 P2 P3
0 28 30 35

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.
Operating System Concepts
Exercise: FCFS
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 1 5
P2 2 4
P3 2 7
P4 4 2

Operating System Concepts


Solution : FCFS
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 1 5 6 5 0 0
P2 2 4 10 8 4 4
P3 2 7 17 15 8 8
P4 4 2 19 15 13 13

Idle P1 P2 P3 P4

.0 .6 .10 .17 .19

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with
the shortest time.
• Two schemes:
– nonpreemptive – 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 know as the
Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time
for a given set of processes.

Operating System Concepts


Shortest-Job-First (SJF) Scheduling
• Disadvantages
– It is difficult to implement as it needs to know the
length of next CPU request.
– As long as the short processes continue to enter
the ready queue, the long processes will not be
allowed to get the CPU. This results in starvation
of long processes.

Operating System Concepts


Example of Non-Preemptive SJF
ProcessArrival TimeBurst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

Operating System Concepts


Example of Non-Preemptive 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)
P1 P3 P2 P4

0 3 7 8 12 16

• Average waiting time = (0 + 6 + 3 + 7)/4 = 4

Operating System Concepts


Example: SJF
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 7 7 7 0 0
P2 1 5 17 16 11 11
P3 3 2 09 6 4 4
P4 4 3 12 8 5 5

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Exercise
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 8
P2 1 1
P3 3 3
P4 4 5

Operating System Concepts


Solution
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 8 8 8 0
P2 1 1 9 8 7
P3 3 7 21 18 11
P4 4 5 14 10 5

P1 P2 P4 P3
0 8 9 14 21

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Shortest Remaining Time First – SRTF
SJF With Preemption
• If new processes arrives with a shorter burst
time than remaining of current process then
schedule new process.
• Reduces average waiting time and average
response time.

Operating System Concepts


Example of Preemptive SJF (Non-
Preemptive)
ProcessArrival TimeBurst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

Operating System Concepts


Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

• Average waiting time = (9 + 1 + 0 +2)/4 = 3

Operating System Concepts


Example: SJF (Preemptive)
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0 7 16 16 9 0
P2 2 4 7 5 1 0
P3 4 1 5 1 0 0
P4 5 4 11 6 2 2

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.
Response Time = CPU First Time – Arrival
Time
Operating System Concepts
Exercise
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0.0 8
P2 0.4 4
P3 1 1

Operating System Concepts


Solutions
Process No. Arrival Time Burst Time Completion Turnaround Waiting Response
Time Time Time time
P1 0.0 8 13 13 5
P2 0.4 4 5.4 5 1
P3 1 1 2 1 0

P1 P2 P3 P2 P1
0.0 0.4 1.0 2.0 5.4 13

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


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.
• Problem  Starvation – low priority processes may
never execute.
• Solution  Aging – as time progresses increase the
priority of the process.
Operating System Concepts
Example of Non Preemptive
Priority Scheduling
ProcessArrival TimeBurst Time Prority
P1 0 7 2
P2 2 4 1
P3 4 1 4
P4 5 4 3

Operating System Concepts


Example of Preemptive Priority
Scheduling
Process TimeBurst Time Prority
P1 7 2
P2 4 1
P3 1 4
P4 4 3

Operating System Concepts


Example (Non Preemptive)
Process No. Arrival Time Burst Time Priority Completion Turnaround Waiting
Time Time Time
P1 0 7 4 7 7 0
P2 1 4 3 16 15 11
P3 3 3 1 10 7 4
P4 4 2 2 12 8 6

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Example (Preemptive)
Process No. Arrival Time Burst Time Priority Completion Turnaround Waiting
Time Time Time
P1 0 7 4 16 16 9
P2 1 4 3 10 9 5
P3 3 3 1 6 3 0
P4 4 2 2 8 4 2

T.A.T = C.T – A.T


W.T. = T.A.T – B.T.

Operating System Concepts


Round Robin (RR)
• Execute process for a time slice then move
back to the ready queue.

Operating System Concepts


Round Robin (RR)
• Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After this time
has elapsed, the process is preempted and added to
the end of the ready queue.
• If there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once. No
process waits more than (n-1)q time units.
• Performance
– q large  FIFO
– q small  q must be large with respect to context switch,
otherwise overhead is too high.

Operating System Concepts


Example of RR with Time Quantum = 4

Process Burst Time


P1 53
P2 17
P3 68
P4 24

Operating System Concepts


Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 5
P3 3

Operating System Concepts


Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 5
P3 3

P3 P2 P1
Ready Queue
CPU

• The Gantt chart is:

Operating System Concepts


Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 5
P3 3

P3
Ready Queue
P2
P1
CPU
• The Gantt chart is:

P1
Operating System Concepts
0
Example of RR with Time Quantum = 4

Process Burst Time


Time : 4
P1 24
P2 5
P3 3

P3
Ready Queue
P2
P1
CPU
• The Gantt chart is:

P1
Operating System Concepts
0
Example of RR with Time Quantum = 4

Process Burst Time


Time : 4
P1 24
P2 5
P3 3

P1
Ready Queue
P3
P2
CPU
• The Gantt chart is:

P1 P2
Operating System Concepts
0 4
Example of RR with Time Quantum = 4

Process Burst Time


Time : 8
P1 24
P2 5
P3 3

P1
Ready Queue
P3
P2
CPU
• The Gantt chart is:

P1 P2
Operating System Concepts
0 4 8
Example of RR with Time Quantum = 4

Process Burst Time


Time : 10
P1 24
P2 5
P3 3

P2
Ready Queue
P1
P3
CPU
• The Gantt chart is:

P1 P2 P3
Operating System Concepts
0 4 7 10
Example of RR with Time Quantum = 4

Process Burst Time


Time : 10
P1 24
P2 5
P3 3

Ready Queue
P2
P1
CPU
• The Gantt chart is:

P1 P2 P3 P1
Operating System Concepts
0 4 7 10
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

0 20 37 57 77 97 117 121 134 154 162

• Typically, higher average turnaround than SJF, but better


response.

Operating System Concepts


Multilevel Queue
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• 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

Operating System Concepts


Multilevel Queue Scheduling

Operating System Concepts


Multilevel Feedback Queue
• A process can move between the various queues;
aging can be implemented this way.
• 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

Operating System Concepts


Example of Multilevel Feedback Queue
• Three queues:
– Q0 – time quantum 8 milliseconds
– Q1 – 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.
– 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.
Operating System Concepts
Multilevel Feedback Queues

Operating System Concepts

You might also like