eng. haneen el-masry 2013site.iugaza.edu.ps/hmasry/files/cpu-scheduling1.pdf · 2013. 11. 16. ·...
Post on 23-Jan-2021
1 Views
Preview:
TRANSCRIPT
Eng. Haneen El-masry
2013
First Come First Served (FCFS)
Shortest Job First (SJF)
Priority
Round Robin (RR)
Multi-Level Queue
Multi-Level Feedback Queue
CPU Scheduling Algorithms
Consider the following set of processes with the indicated arrival time. The length of the CPU-burst time is given in ms.
For each of the following scheduling algorithms, draw the Gantt chart and compute the avg. waiting time, avg. Response time, avg. turnaround time.
Example 1
Process Arrival Time CPU-Burst Time Priority
P1 0 10 7
P2 1 1 2
P3 3 4 4
P4 5 2 3
P5 11 3 5
P1 P2 P3 P4 P5
FCFS Gantt Chart
0 10 11 15 17 20
Calculation
Avg. W. T.= (0+9+8+10+6)/5= 6.6 ms.
Avg. R. T.= (0+9+8+10+6)/5= 6.6 ms.
Avg. T. T.= (10+10+12+12+9)/5= 10.6 ms.
Process Waiting Time Response Time Turnaround Time
P1 0 0 10-0=10
P2 10-1=9 10-1=9 11-1=10
P3 11-3=8 11-3=8 15-3=12
P4 15-5=10 15-5=10 17-5=12
P5 17-11=6 17-11=6 20-11=9
Gantt Chart
0 10 11 13 16 20
Calculation
Avg. W. T.= (0+9+13+6+2)/5 = 6 ms. Avg. R. T. = (0+9+13+6+2)/5 = 6 ms. Avg. T. T. = (10+10+17+8+5)/5 = 10 ms.
Non-Preemptive SJF
P1 P2 P4 P5 P3
P W.T. R.T. T.T.
P1 0 0 10
P2 10-1=9 10-1=9 11-1=10
P3 16-3=13 16-3=13 20-3=17
P4 11-5=6 11-5=6 13-5=8
P5 13-11=2 13-11=2 16-11=5
Gantt Chart
0 1 2 3 7 9 11 14 20
Calculation
Avg. W. T.= (10+0+0+2+0)/5 = 2.4 ms. Avg. R. T. = (0+0+0+2+0)/5 = 0.4 ms. Avg. T. T. = (20+1+4+4+3)/5 = 6.4 ms.
Preemptive SJF
P1 P2 P1 P3 P4 P1 P5 P1
P W.T. R.T. T.T.
P1 (2-1)+(9-3)+(14-11)= 10 0 20-0=20
P2 1-1=0 1-1=0 2-1=1
P3 3-3=0 3-3=0 7-3=4
P4 7-5=2 7-5=2 9-5=4
P5 11-11=0 11-11=0 14-11=3
You can conclude that:
Waiting Time = Turnaround Time - CPU Burst
Note: Smaller value means a higher priority.
Gantt Chart
0 10 11 13 17 20
Calculation
Non-Preemptive Priority
P1 P2 P4 P3 P5
P T.T. W.T. R.T.
P1 10-0=10 10-10=0 0
P2 11-1=10 10-1=9 10-1=9
P3 17-3=14 14-4=10 13-3=10
P4 13-5=8 8-2=6 11-5=6
P5 20-11=9 9-3=6 17-11=6
Avg. 10.2 6.2 6.2
Gantt Chart
0 1 2 3 5 7 9 11 14 20
Calculation
Preemptive Priority
P1 P2 P1 P3 P4 P3 P1 P5 P1
P T.T. W.T. R.T.
P1 20-0=20 20-10=10 0
P2 2-1=1 1-1=0 1-1=0
P3 9-3=6 6-4=2 3-3=0
P4 7-5=2 2-2=0 5-5=0
P5 14-11=3 3 - 3=0 11-11=0
Avg. 6.4 2.4 0
Note: If two process need to enter the ready queue at the same time,
use the FCFS to choose between them.
Gantt Chart
0 2 3 5 7 9 11 13 15 17 19 20
Calculation
RR (q=2 ms)
P1 P2 P1 P3 P1 P4 P3 P1 P5 P1 P5
P T.T. W.T. R.T.
P1 19-0=19 19-10=9 0
P2 3-1=2 2-1=1 2-1=1
P3 13-3=10 10-4=6 5-3=2
P4 11-5=6 6-2=4 9-5=4
P5 20-11=9 9-3=6 15-11=4
Avg. 9.2 5.2 2.2
Note: If two process need to enter the ready queue at the same time,
the new process will be served first.
Gantt Chart
0 2 3 5 7 9 11 13 15 17 18 20
Calculation
RR (q=2 ms)
P1 P2 P1 P3 P4 P1 P3 P5 P1 P5 P1
P T.T. W.T. R.T.
P1 20-0=20 20-10=10 0
P2 3-1=2 2-1=1 2-1=1
P3 13-3=10 10 - 4=6 5 - 3=2
P4 9-5=4 4 -2=2 7 - 5=2
P5 18-11=7 7 - 3=4 13 -11=2
Avg. 8.6 4.6 1.4
Assume both of P1 and P3 are batch processes, P2 is interactive processes, and both of P4 and P5 are system processes.
Assume also that the batch processes queue is scheduled using the preemptive SJF and the other queues use FCFS.
Multi-level Queue
System Process (FCFS)
Interactive Process (FCFS)
Batch Process (SJF)
Gantt Chart
0 3 4 7 9 11 14 15 20
Calculation
P1 P2 P3 P4 P1 P5 P3 P1
P T.T. W.T. R.T.
P1 20-0=20 20-10=10 0
P2 4-1=3 3-1=2 3-1=2
P3 15-3=12 12 - 4=8 4 - 3=1
P4 9-5=4 4 -2=2 7 - 5=2
P5 14-11=3 3 - 3=0 11-11=0
Avg. 8.4 4.4 1
Assume both of the first and second levels use the RR scheduling with quantum size as shown below.
Multi-level feedback queue
Q=3
Q=2
FCFS
Gantt Chart
0 1 2 3 5 7 9 11 14 20
Calculation
P1 P2 P1 P3 P4 P3 P1 P5 p1
P T.T. W.T. R.T.
P1 20-0=20 20-10=10 0
P2 2-1=1 1-1=0 1-1=0
P3 9-3=6 6- 4=2 3 - 3=0
P4 7-5=2 2 -2=0 5 - 5=0
P5 14-11=3 3 - 3=0 11-11=0
Avg. 6.4 2.4 0
CPU Utilization = (CPU time/ Total Time) *100%
Q: Compute the CPU Utilization for part (f). Assume the context-switching time is 0.5 ms.
Solution
Context-Switching Time = 0.5*10= 5 ms
CPU Utilization = (20/ (20+5) )*100%
= 80 %
CPU Utilization
Q: If the length of the next CPU burst was predicted to be 8ms, the actual length used in this prediction was 5ms. According to the average exponential formula, what was the previous predicted value of CPU burst if α=0.4.
Solution
ιn+1= 8ms.
tn= 5ms.
ιn= ?
α= 0.4
ιn+1= α*tn + (1- α)*ιn
8= 0.4*5+0.6* ιn
ιn= (8-2)/0.6=10 ms.
Next CPU Burst Length
Starvation: low priority processes may never execute.
Q. Which of the following scheduling algorithms could result in starvation ?
A. FCFS
B. SJF
C. RR
D. Priority
Starvation
Consider five processes P1, P2, P3, P4 and P5 with the following Process Burst Times and Priorities:
The processes are assumed to have arrived in the order P1, P2, P3,
P4, and P5 all at time 0. Smaller value means a higher priority.
Draw a Gantt diagram representing the allocation of the CPU and calculate the average Turnaround time and average wait time using FCFS, SJF, Priority, and Round Robin (q = 3). Show all calculations.
Exercise 1
P CPU Burst Priority
P1 15 2
P2 2 0
P3 4 2
P4 2 3
P5 8 1
With My Best Wishes
top related