View
319
Download
15
Category
Preview:
Citation preview
Scheduling Algorithms
Presented By:
Shekhar Singh Tomar
ContentsContents
Introduction Scheduling Algorithms- First Come, First Served (FCFS)
Shortest Job First (SJF)
Priority
Round Robin (RR)
IntroductionIntroduction
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is to be allocated the CPU.
1- First Come, First Served (FCFS) 1- First Come, First Served (FCFS) SchedulingScheduling
Implementation: As each process becomes ready, it joins the ready queue. When the current process finishes, the oldest process is selected
next. Characteristics:
Simple to implement Non-preemptive
First-Come, First-Served (FCFS) SchedulingFirst-Come, First-Served (FCFS) Scheduling
Example- Process Burst Time
P1 24
P2 3
P3 3
With FCFS, the process that requests the CPU first is allocated the CPU first
Case 1: Suppose that the processes arrive in the order: P1 , P2 , P3
The Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 Average turn-around time: (24 + 27 + 30)/3 = 27
P1 P2 P3
24 27 300
FCFS Scheduling (Cont.)FCFS Scheduling (Cont.)
Case 2: Suppose that the processes arrive in the order: P2 , P3 , P1
The 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 Case #1)
Average turn-around time: (3 + 6 + 30)/3 = 13
P1P3P2
63 300
2-Shortest-Job-First (SJF) Scheduling2-Shortest-Job-First (SJF) Scheduling
The SJF algorithm associates with each process the length of its next CPU burst
When the CPU becomes available, it is assigned to the process that has the smallest next CPU burst (in the case of matching bursts, FCFS is used)
Two schemes: Non-preemptive – once the CPU is given to the process, it
cannot be preempted until it completes its CPU burst. Preemptive – It will preempt the currently executing process.
This scheme is know as the Shortest-Remaining-Time-First (SRTF)
SJF Scheduling…SJF Scheduling…Exponential Averaging
Estimation based on historical data tn = length of the nth CPU burst
n = Estimate for nth CPU burst (stores past history)
n+1 = predicted value for n+1st or next CPU burst
, 0 ≤ ≤1
n+1 = tn + (1- ) n
04/11/23 8
ProcessArrival Time Burst Time
P1 0.0 6
P2 0.0 4
P3 0.0 1
P4 0.0 5
SJF (non-preemptive, simultaneous arrival)
Average waiting time = (0 + 1 + 5 + 10)/4 = 4
Average turn-around time = (1 + 5 + 10 + 16)/4 = 8
Example 1: Non-Preemptive SJFExample 1: Non-Preemptive SJF(simultaneous arrival)(simultaneous arrival)
P1P3 P2
51 160
P4
10
ProcessArrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive, varied arrival times)
Average waiting time = ( (0 – 0) + (8 – 2) + (7 – 4) + (12 – 5) )/4 = (0 + 6 + 3 + 7)/4 = 4
Average turn-around time: = ( (7 – 0) + (12 – 2) + (8 - 4) + (16 – 5))/4
= ( 7 + 10 + 4 + 11)/4 = 8
Example 2: Non-Preemptive SJFExample 2: Non-Preemptive SJF(varied arrival times)(varied arrival times)
P1 P3 P2
73 160
P4
8 12
Waiting time : sum of time that a process has spent waiting in the ready queue
Example 3: Preemptive SJFExample 3: Preemptive SJF(Shortest-remaining-time-first)(Shortest-remaining-time-first)
ProcessArrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive , varied arrival times)
Average waiting time = ( [(0 – 0) + (11 - 2)] + [(2 – 2) + (5 – 4)] + (4 - 4) + (7 – 5) )/4
= 9 + 1 + 0 + 2)/4 = 3
Average turn-around time = (16 + 7 + 5 + 11)/4 = 9.75
P1 P3P2
42 110
P4
5 7
P2 P1
16
Waiting time : sum of time that a process has spent waiting in the ready queue
3- Priority Scheduling3- Priority Scheduling
The SJF algorithm is a special case of the general priority scheduling algorithm
Each process (or class of processes) is given a priority (integer number) . The CPU is allocated to the process with the highest priority (smallest
integer = highest priority) Priority scheduling can be either preemptive or non-preemptive
A preemptive approach will preempt the CPU if the priority of the newly-arrived process is higher than the priority of the currently running process
A non-preemptive approach will simply put the new process (with the highest priority) at the head of the ready queue
SJF is a priority scheduling algorithm where priority is the predicted next CPU burst time.
The main problem with priority scheduling is starvation, that is, low priority processes may never execute.
A solution is aging; as time progresses, the priority of a process in the ready queue is increased
Example of Priority SchedulingExample of Priority SchedulingProcess Burst time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
0 1 6 16 18 19
Average wating time = (6+0+16+18+1)/5=8.2 milliseconds
P2 P5 P1 P3 P4
4- Round-Robin Scheduling4- Round-Robin Scheduling
Developed in response to time-sharing systems.
Each process gets a small unit of CPU time (time quantum),
Each job is given control of the CPU for that time quantum. (In the range of 10-100 milliseconds.)
Control then passes to the next job (FIFO?)
Average waiting time dependent on job size, quantum size
RR scheduling algorithm is preemptive.
Example of RR with Time Quantum = 4Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The chart is:
Typically, higher average turnaround than SJF, but better response.
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Time Quantum and Context Switch TimeTime Quantum and Context Switch Time
Thank YouThank You
Recommended