Eng. Haneen El-masry 2013 CPU Scheduling Algorithms First Come First Served (FCFS) Shortest Job First (SJF) Priority Round Robin (RR) Multi-Level Queue Multi-Level Feedback Queue Example 1 Consider the following set of processes with the indicated arrival time. The length of the CPU-burst time is given in ms. 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 For each of the following scheduling algorithms, draw the Gantt chart and compute the avg. waiting time, avg. Response time, avg. turnaround time. FCFS Gantt Chart P1 0 P2 P3 10 11 P4 15 P5 17 20 Calculation 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 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. Non-Preemptive SJF Gantt Chart P1 P2 0 10 P4 11 P5 13 P3 16 20 Calculation 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 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. Preemptive SJF Gantt Chart P1 P2 P1 0 1 2 P3 3 P4 7 P1 P5 9 11 P1 14 20 Calculation 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 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. You can conclude that: Waiting Time = Turnaround Time - CPU Burst Non-Preemptive Priority Note: Smaller value means a higher priority. Gantt Chart P1 0 Calculation P2 10 11 P4 P3 13 P5 17 P T.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 W.T. 20 6.2 R.T. 6.2 Preemptive Priority Gantt Chart P1 P2 P1 P3 0 1 2 3 Calculation P P4 5 T.T. P3 7 P1 9 P5 P1 11 14 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 20 RR (q=2 ms) Note: If two process need to enter the ready queue at the same time, use the FCFS to choose between them. Gantt Chart P1 0 P2 2 P1 3 P3 5 P1 7 P4 9 P3 11 P1 13 P5 15 Calculation P T.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 W.T. 5.2 R.T. 2.2 P1 17 P5 19 20 RR (q=2 ms) Note: If two process need to enter the ready queue at the same time, the new process will be served first. Gantt Chart P1 0 P2 2 P1 3 P3 5 P4 7 P1 9 P3 11 P5 13 P1 15 Calculation P T.T. W.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 R.T. 1.4 P5 17 18 P1 20 Multi-level Queue 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. System Process (FCFS) Interactive Process (FCFS) Batch Process (SJF) Gantt Chart P1 0 P2 3 P3 4 P4 7 P1 9 P5 11 P3 14 15 Calculation P T.T. W.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 R.T. 1 P1 20 Multi-level feedback queue Assume both of the first and second levels use the RR scheduling with quantum size as shown below. Q=3 Q=2 FCFS Gantt Chart P1 P2 P1 0 1 2 P3 3 P4 5 P3 7 P1 9 P5 11 14 Calculation P T.T. W.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 p1 R.T. 0 20 CPU Utilization CPU Utilization = (CPU time/ Total Time) *100% Q: Compute the CPU Utilization for part (f). Assume the contextswitching time is 0.5 ms. Solution Context-Switching Time = 0.5*10= 5 ms CPU Utilization = (20/ (20+5) )*100% = 80 % Next CPU Burst Length 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. Starvation 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 Exercise 1 Consider five processes P1, P2, P3, P4 and P5 with the following Process Burst Times and Priorities: P CPU Burst Priority P1 15 2 P2 2 0 P3 4 2 P4 2 3 P5 8 1 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. With My Best Wishes
© Copyright 2025 Paperzz