IT 106 UNIT 2 Lesson 2 CPU Scheduling Algorithm
IT 106 UNIT 2 Lesson 2 CPU Scheduling Algorithm
2. Medium-term Scheduler
The medium-term scheduler may decide to
swap out a process (a) which has not been active
for some time, (b) or a process which has a low
priority, (c) or a process which is taking up a large
amount of memory in order to free up main
memory for other processes.
TYPES OF SCHEDULERS
Shortest
First-Come, Shortest-Job- Remaining
First-Served First (SJF) Time First
(FCFS) (SRTF)
0 24 27 30
First-Come, First-Served Scheduling (FCFS)
Example: cont...
Therefore the waiting time(WT) for each process is:
WT for P1 0-0 =0 Waiting time =
Start time –
WT for P2 24-0 =24 Arrival time
WT for P3 27-0 =27
Average Waiting Time = (0 + 24 + 27)/3 = 17 ms
The turnaround time(TT) for each process would be:
TT for P1 24-0 =24 Turnaround Time
TT for P2 27-0 =27 = Exit Time –
Arrival Time
TT for P3 30-0 =30
Average Turnaround Time = (24 + 27 + 30)/3 = 27 ms
First-Come, First-Served Scheduling (FCFS)
Example:
Supposed that the processes arrive in the order: P2, P3 and P1
The Gantt Chart for the schedule is:
P2 P3 P1
0 3 6 30
✓ NOTE:
Comparison of the two computations of
waiting time reveals that if the smaller burst jobs
get processed first, then the average wait by each
job is lessened. But because FCFS is a non-
preemptive algorithm, the computation in the
given example must be done using the sequence
P1, P2, P3 not P2, P3, P1.
First-Come, First-Served Scheduling (FCFS)
Here is another example of five processes arriving at different times. Each process
has a different burst time.
Process Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 10) Let's calculate the average waiting time for above example.
First-Come, First-Served Scheduling (FCFS)
0 3 9 16 24
Shortest-Job-First Scheduling Algorithm
Example: cont...
Therefore the waiting time(WT) for each process is:
WT for P1 3-0 =3 Note:
WT for P2 16-0 =16 If two processes
WT for P3 9-0 =9 have the same
_
length of CPU
WT for P4 0-0 =0 burst, FCFS
Average Waiting Time = (3 + 16 + 9 + 0)/4 = 7 ms scheduling is used
The turnaround time(TT) for each process would be: to break the tie by
considering which
TT for P1 9-0 =9
job arrived first.
TT for P2 24-0 =24
TT for P3 16-0 =16
TT for P4 0-0 =0
Average Turnaround Time = (9 + 24 + 16 + 0)/4 = 12.25 ms
Shortest-Job-First Scheduling Algorithm
Non-Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to process, the
process holds it till it reaches a waiting state or terminated.
Process Queue Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 11) Let’s calculate the average waiting time for the given example.
Waiting time = Start time - Arrival time
Wait time Process Queue Burst time Arrival time
P4 = 0-0=0
P1 = 3-2=1 P1 6 2
P2 = 9-5=4 P2 2 5
P5 = 11-4=7 P3 8 1
P3 = 15-1=14 P4 3 0
P5 4 4
❖ Advantage of SRTF:
• Shortest remaining time finish first
• Minimizes average waiting times
❖ Disadvantage of SRTF:
• Not fair for long jobs that arrives early
• Low priority process with shortest burst
time are still the one to be executed first by
the CPU
• Starvation of jobs with long CPU bursts.
Shortest Remaining Time First(SRTF)/(PSJF)
Example:
Consider the following four processes, with the length of the
CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 1
P4 3 5
0 1 2 3 6 11 18
Shortest Remaining Time First(SRTF)/(PSJF)
Example: cont...
Therefore the waiting time(WT) for each process is:
WT for P1 11-0-(1) =10
WT for P2 3-1-(1) =1 Waiting time =
Start time –
_
WT for P3 2-2 =0 Arrival time
WT for P4 6-3 =3
Average Waiting Time = (10 + 1 + 0 +3)/4 = 3.5 ms
The turnaround time(TT) for each process would be:
TT for P1 18-0 =18 Turnaround Time
TT for P2 6-1 =5 = Exit Time –
Arrival Time
TT for P3 3-2 =1
TT for P4 11-3 =8
Average Turnaround Time = (18 + 5 + 1 + 8)/4 = 8 ms
Shortest Remaining Time First(SRTF)/(PSJF)
Another Example:
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
0 3 7 8 12 16
0 2 4 5 7 11 16
❖Advantage of Priority:
• Jobs with high priority will be executed first
❖Disadvantage of Priority:
• Low priority process will be the last to be
executed
• Not fair low priority process that arrives early.
Conceptual and Implementation view of Priority Scheduling
PRIORITY SCHEDULING
Example:
Consider the following example:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
0 1 6 16 18 19
PRIORITY SCHEDULING
Example: cont... Therefore the waiting time(WT) for each process is:
WT for P1 6-0 =6
WT for P2 0-0 =0
WT for P3 16-0 =16
WT for P4 18-0 =18
WT for P5 1-0 =1
Average Waiting Time = (6 + 0 + 16 +18 + 1)/5 = 8.2 ms
The turnaround time(TT) for each process would be:
TT for P1 16-0 =16
TT for P2 1-0 =1
TT for P3 18-0 =18
TT for P4 19-0 =19
TT for P5 6-0 =6
Average Turnaround Time = (16 + 1 + 18 + 19 + 6)/5 = 12 ms
SCHEDULING ALGORITHMS
5. Preemptive Priority (P-Prio) Scheduling Algorithm –
will preempt the CPU if the priority of the newly arrived
process is higher than the current running process.
A major problem with priority scheduling algorithm
is indefinite blocking or starvation. A process that is
ready to run but lacking the CPU can be considered
blocked, waiting for the CPU. A priority-scheduling
algorithm can leave some low-priority processes
waiting indefinitely for the CPU. In a heavily loaded
computer system, a steady stream of higher-priority
processes can prevent a low-priority process from
ever getting the CPU.
PREEMPTIVE PRIORITY (P-PRIO)
Generally, one of two things will happen. Either the
process will eventually be run, or the computer system will
eventually crash and lose all unfinished low-priority
processes.
A solution to the problem of indefinite blockage of low-
priority processes is aging. Aging is a technique of gradually
increasing the priority of processes that wait in the system for
a long time. For example, if priorities range from 127 to 0, we
could increment the priority of a waiting process by 1 every
15 minutes. Eventually, even a process with an initial priority
of 127 would have the highest priority in the system and
would be executed. In fact, it would take no more than 32
hours for a priority 127 process to age to priority 0 process.
PREEMPTIVE PRIORITY (P-PRIO)
❖Advantage of P-Prio:
• Jobs with high priority will be executed first
• Jobs with a low priority will be replaced by
new process with a higher priority
❖Disadvantage of P-Prio:
• Not fair for low priority process that arrives
early.
• Indefinite blocking/starvation
• Solution: Aging
PREEMPTIVE PRIORITY (P-PRIO)
Example:
Consider the following example:
Process Arrival Time Burst Time Priority
P1 1 5 5
P2 2 10 4
P3 3 18 3
P4 4 7 2
P5 5 3 1
The CPU scheduler picks the first process from the ready
queue, sets a timer to interrupt after 1 time quantum, and
dispatches the process.
One of two things will then happen. The process may
have a CPU burst of less than 1 time quantum. In this case,
the process itself will release the CPU voluntarily. The
scheduler will then proceed to the next process in the ready
queue. Otherwise, if the CPU burst of the currently running
process is longer than 1 time quantum, the timer will go off
and will cause an interrupt to the operating system. A context
switch will be executed, and the process will be put at the tail
of the ready queue. The CPU scheduler will then select the
next process in the ready queue.
Representation of Round Robin Scheduling
ROUND ROBIN SCHEDULING
If there are n processes in the ready queue and the time
quantum is q,
o Each process gets1/n of the CPU time in chunks of at
most q time units at once.
oNo process waits more than (n-1)q time units until its
next time quantum.
• Example: If there are 5 processes, with a time
quantum of 20 ms, then each process will get up
to 20 ms every 100 ms.
❖Advantage of RR:
• Jobs get fair share of CPU (no need to
schedule by priority, or by burst time)
• Most fair CPU scheduling algorithm
• Shortest jobs finish relatively quickly
❖Disadvantage of RR:
• Poor average waiting time with similar job
lengths
• Performance depends on length of time-
slice
ROUND ROBIN SCHEDULING
Example of Round-robin Scheduling
Consider this following three processes
Step 1) The execution begins with process P1, which has burst time 4.
Here, every process executes for 2 seconds. P2 and P3 are still in the
waiting queue.
Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING
Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING
Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING
Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING
Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING
Process Burst
Queue time
P1 4
P2 3
P3 5
ROUND ROBIN SCHEDULING
Step 7) Let’s calculate the average waiting time for the Process Burst
Queue time
given example.
P1 4
P2 3
P1 = 8-4 = 4 P1 = 8-0 = 8
P2 = 9-3 = 6 P2 = 9-0 = 9
P3 = 12-5 = 7 P3 = 12-0 =12
ROUND ROBIN SCHEDULING
Example:
Consider the following example:
Process Burst Time
P1 53
P2 17
P3 68
P4 24