Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Recap

• CPU burst
• I/O burst
• Scheduling
• Long Term scheduling
Evocation
CPU Scheduling
Objectives

• To introduce CPU scheduling, which is the basis for


multiprogrammed operating systems
• To describe various CPU-scheduling algorithms
• To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system
• To examine the scheduling algorithms of several
operating systems
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 followed by
I/O burst
• CPU burst distribution is of
main concern
Histogram of CPU-burst Times
CPU Scheduler
• Whenever the CPU becomes idle, the operating
system must select one of the processes in the ready
queue to be executed.
• The selection process is carried out by the short-term
scheduler (or CPU scheduler).
• CPU-scheduling decisions may take place under the following
four circumstances

1. Switches from running to waiting state


2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates

• For situations 1 and 4, there is no choice in terms of


scheduling. A new process (if one exists in the ready queue)
must be selected for execution.
• Preemptive scheduling
– Prioritized computation
– Limited time period is fixed for every process in the CPU

• Non-Preemptive scheduling
• When a process enters the state of running the state of that
process is not deleted from the scheduler until it finishes .
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
Scheduling Criteria

• CPU utilization – keep the CPU as busy as possible


• Throughput – # of processes that complete their execution
per time unit
• Turnaround time – amount of time to execute a particular
process
• Waiting time – amount of time a process has been waiting in
the ready queue
• 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)
Scheduling Algorithm Optimization Criteria

• Max CPU utilization


• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
First Come First Serve Algorithm

• Nonpreemptive scheduling algorithm.


• Follow first in first out (FIFO) method.
• One ready queue.
• FIFO strategy assigns priority to processes in the order in which
they request the processor.
• When a process comes in, add its PCB to the tail of ready queue.
• Next process from head of queue.
• Dispatcher selects first job in queue and this job runs to
completion of CPU burst.
• Running process does not give up the CPU until it terminates or
it performs IO
First- Come, First-Served (FCFS) Scheduling

Process 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 ms


• Average waiting time: (0 + 24 + 27)/3 = 17 ms
• Total turn around time =24+27+30 =81 ms
• Average turn around time = 81/3 =27 ms
FCFS Scheduling (Cont.)
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
– Consider one CPU-bound and many I/O-bound processes
Example 2

Process Arrival time Service time


P1 0 8
P2 1 4
p3 2 9
p4 3 5
• Total Wait Time = 0 + 7 + 10 + 18 = 35 ms
• Average Waiting Time = (Total Wait Time) / (Total number of
processes) = 35/4 = 8.75 ms
• Turn around time =Completion time – arrival time
• Total Turn Around Time = 8 + 11 + 19 + 23 = 61 ms
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
• SJF is optimal – gives minimum average waiting time
for a given set of processes
– The difficulty is knowing the length of the next
CPU request
– Could ask the user
Example of SJF

ProcessArriva l Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

• SJF scheduling chart

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


Example of Shortest-remaining-time-first

• Now we add the concepts of varying arrival times and


preemption to the analysis
ProcessA arri Arrival TimeT Burst
Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• Preemptive SJF Gantt Chart

• Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 =


26/4 = 6.5 msec
• Non-preemptive

• Waiting time =0+(8-1)+(12-3)+(17-2) =31 ms


• Average waiting time = 31/4=7.75 ms
• Turnaround time = CT-AT
= 8+11+15+17=51 ms
• Average Turnaround time =51/4 =12.75 ms
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.
• Preemptive

Gantt chart
P1 P2 P3 P1
0 2 8 11 14
Waiting time = (0-0)+(11-2)+(2-2) +(8-3)
= 9+5
=14/3=4.66ms
Non-preemptive

P1 P2 p3

0 5 11 14
• Average waiting time =0+3+8=11/3=3.66ms
• Average turnaround time = 5+9+11=25/3=8.33ms
Round Robin
• Designed for time sharing system
• It is similar to FCFS scheduling, but preemption is added to
switch between processes.
• A small unit of time, called a time quantum or time slice, is
defined. A time quantum is generally from 10 to 100
milliseconds.
• To implement RR scheduling, we keep the ready queue as a
FIFO queue of processes.
• 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.
An operating system uses the Shortest Remaining Time first
(SRTF) process scheduling algorithm. Consider the arrival times
and execution times for the following processes:
Process Execution time Arrival time
P1 20 0
P2 25 15
P3 10 30
P4 15 45
What is the total waiting time for process P2?
(A) 5
(B) 15
(C) 40
(D) 55
Consider the following table of arrival time and burst time for
three processes P0, P1 and P2. (GATE-CS-2011)
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The pre-emptive shortest job first scheduling algorithm is used.
Scheduling is carried out only at arrival or completion of
processes. What is the average waiting time for the three
processes?
(A) 5.0 ms
(B) 4.33 ms
(C) 6.33
(D) 7.33
Mind Map
Summary

• Discussion

You might also like