Scheduling Algorithms Update

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

CPU Scheduling

Scheduling Algorithms
for a single processing core

Week 11
CPU scheduling

A process of determining which


process will own CPU for execution
while another process is on hold.

The main task of CPU scheduling is to


make sure that whenever the CPU
remains idle, the OS at least select one
of the processes available in the ready
queue for execution.
CPU Scheduling Benefits

Scheduling is a major job of an operating system OS.

The methods of scheduling play a big role in the


performance of the central processing unit (CPU) since it
decides the utilization of the resources.

CPU scheduling algorithms have a significant function in


multiprogramming operating systems.

When the CPU scheduling is effective a high rate of


computation could be done correctly and also the system
will maintain in a stable state.
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:

 A process switches from the running to the waiting state.


 Specific process switches from the running state to the ready state.
 Specific process switches from the waiting state to the ready state.
 Process finished its execution and terminated.

 Scheduling under 1 and 4 is nonpreemptive


 Other scheduling is preemptive
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
Optimization Criteria
Important CPU scheduling Terminologies
Burst Time/Execution Time: It is a time required by the process to complete
execution. It is also called running time.
Arrival Time: when a process enters in a ready state
Finish Time: when process complete and exit from a system
Multiprogramming: A number of programs which can be present in memory at
the same time.
Jobs: It is a type of program without any kind of user interaction.
User: It is a kind of program having user interaction.
Process: It is the reference that is used for both job and user.
CPU/IO burst cycle: Characterizes process execution, which alternates between
CPU and I/O activity. CPU times are usually shorter than the time of I/O.
First-Come First-Serve (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:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0;


P2 = 24;
P3 = 27
 Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order: Is it preemptive or
Non-preemptive ?
P2 , P3 , P1
How is it for
 The Gantt chart for the schedule is:
interactive systems?

P2 P3 P1
0 3 6 30

 Waiting time for P2 = 0; P3 = 3, P1 = 6;


 Average waiting time: (0 + 3 + 6) / 3 = 3
 Much better than previous case
 Convoy effect - short process behind long process
 All the other processes wait for the one big process to get off the CPU
 Results in lower CPU and device utilization than might be possible if the shorter
processes were allowed to go first

• Note that the FCFS scheduling algorithm is non-preemptive. Once the CPU has been allocated to a
process, that process keeps the CPU until it releases the CPU, either by terminating or by requesting
I/O.
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
 more appropriate name would be the shortest-next-CPU-burst
 If the next CPU bursts of two processes are the same, FCFS scheduling is used to
break the tie.

 SJF is optimal –
 It gives minimum average waiting time for a given set of processes
 Moving a short process before a long one decreases the waiting time of the short process
more than it increases the waiting time of the long process.
 Consequently, the average waiting time decreases.

 BUT:
 The difficulty is knowing the length of the next CPU request 
 However, how to know the length of the next CPU burst
 Could ask the user; (in a batch system, we may do so, but what about interactive )
 What about approximation or prediction?
Shortest-Job-First (SJF) Scheduling

 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
SJF Example 1
ProcessArriva Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

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

 By comparison, if we were using the FCFS scheduling scheme, the average


waiting time would be 10.25.
Example 2
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16

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


Example 3

Process Arrival Time Burst Time


P1 0 7
P2 2 4
P3 4 1
P4 5 4
 SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

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


Determining Length of Next CPU Burst
 Can only estimate the length
 Can be done by using the length of previous CPU bursts, using exponential
averaging

1. t n  actual length of n th CPU burst


2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :
 n 1   t n  1    n .
Prediction of the Length of the Next CPU Burst

An exponential average with α = 1/2 and 0 = 10.


Shortest-Remaining-Time-First

 Is SJF Preemptive or Non-preemptive ?

 The choice arises when a new process arrives at the ready queue while a
previous process is still executing

 The next CPU burst of the newly arrived process may be shorter than what
is left of the currently executing process.

 A preemptive SJF algorithm will preempt the currently executing process,

 whereas a non-preemptive SJF algorithm will allow the currently running


process to finish its CPU burst

 Preemptive SJF scheduling is sometimes called:

 Shortest-Remaining-Time- First
Example of Shortest-Remaining-Time-First

 Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 msec


Round Robin (RR)
 Each process gets a small unit of CPU time (time quantum q) or (time slice),
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.
 The average waiting time under the RR policy is often long.

 Timer interrupts every quantum to schedule next process


 So, is it preemptive or non-preemptive?

 Performance ??
 The ready queue is a circular FIFO queue, new processes are added to the tail (end)
 If q is large enough  FCFS
 If q is small enough  q must be large with respect to context switch, otherwise
overhead is too high; not worthy 
Example 1 : RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

 The average waiting time for this schedule.


 P1 waits for 6 milliseconds (10 − 4),
 P2 waits for 4 milliseconds, and
 P3 waits for 7 milliseconds.
 Thus, the average waiting time is 17/3 = 5.66 milliseconds.

 Typically, higher average turnaround than SJF, but better response


 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 usec
Time Quantum and Context Switch Time
 In the RR scheduling algorithm, no process is allocated the CPU for more than 1
time quantum in a row (unless it is the only runnable process).
 However, the performance of the RR algorithm depends heavily on the size of the
time quantum. Turnaround time also depends on the size of the time quantum.
 Thus, we want the time quantum to be large with respect to the context switch time.
But it should not be too large; (then it may become FCFS).
Turnaround Time Varies With The Time Quantum

80% of CPU bursts should be


shorter than q
Priority Scheduling
 The SJF algorithm is a special case of the general priority-scheduling algorithm
 A priority number (integer) is associated with each process
 Equal-priority processes are scheduled in FCFS order

 The CPU is allocated to the process with the highest priority


(smallest integer  highest priority), it is either:
 Preemptive: simply preempt the CPU if the priority of the newly arrived process is
higher than the priority of the currently running process
 Non-preemptive: simply put the new process at the head of the ready queue
 SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
 The larger the CPU burst, the lower the priority, and vice versa

 Problem  Starvation (indefinite blocking) – low priority processes may never


execute (A process that is ready to run but waiting for the CPU can be considered blocked)
 Solution  Aging – as time progresses increase the priority of the process
Example : Priority Scheduling

ProcessA arri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Priority scheduling Gantt Chart

What do you think we need


 Average waiting time = 8.2 msec
to do if the arrival time is
added to the processes?
Priority Scheduling w/ Round-Robin
ProcessA arri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
 Run the process with the highest priority.
 Processes with the same priority run round-robin
 Notice that when process P2 finishes at time 16, process P3 is the highest-
priority process, so it will run until it completes execution
 Gantt Chart with q = 2 ms time quantum
Multilevel Queue Scheduling
 With priority scheduling, have separate queues for each priority.
 Schedule the process in the highest-priority queue!
Multilevel Queue Scheduling
 Prioritization based upon process type,
 Each queue has absolute priority over lower-priority queues
 Another possibility is to time-slice among the queues

Real Time Processes: i.e. the scheduling algorithm itself (scheduling process);
i.e. the computer inside the Engine Control Unit in a car has to manage the engine
at every moment based on what the driver wants to do (in real-time fashion).
Scheduling is a real-time process too
Try out:
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
Multilevel Queue Scheduling
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
Example of Multilevel Feedback Queue

 Three queues:
 Q0 – RR with time quantum 8 milliseconds
 Q1 – RR 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.
Multilevel Feedback Queues

You might also like