Successfully reported this slideshow.
Your SlideShare is downloading. ×

OS_Ch05.pdf

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
ch5_EN_CPUSched.pdf
ch5_EN_CPUSched.pdf
Loading in …3
×

Check these out next

1 of 179 Ad

More Related Content

Advertisement

OS_Ch05.pdf

  1. 1. Operating Systems Chapter 5: CPU Scheduling Dr. Ahmed Hagag Scientific Computing Department, Faculty of Computers and Artificial Intelligence Benha University 2019
  2. 2. Chapter 5: CPU Scheduling • Basic Concepts • Scheduling Criteria • Scheduling Algorithms 2 ©Ahmed Hagag Operating Systems
  3. 3. • CPU scheduling is the central in multi-programming system. • Maximum CPU utilization obtained with multiprogramming (prevent CPU from being idle). • Processes residing in the main memory is selected by the Scheduler that is: ➢ Concerned with deciding a policy about which process is to be selected. ➢ Process selection based on a scheduling algorithm. 3 ©Ahmed Hagag Operating Systems Basic Concepts (1/11)
  4. 4. 4 ©Ahmed Hagag Operating Systems Basic Concepts (2/11)
  5. 5. 5 ©Ahmed Hagag Operating Systems Basic Concepts (2/11)
  6. 6. • Process execution consists of a cycle of CPU execution and I/O wait. • Processes alternate between these two states. Process execution begins with a CPU burst. That is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on. • CPU bursts vary greatly from process to process and from computer to computer. 6 ©Ahmed Hagag Operating Systems Basic Concepts (3/11)
  7. 7. Schedulers • Long-term scheduler chooses some of them to go to memory (ready queue). • Then, short-term scheduler (or CPU scheduler) chooses from ready queue a job to run on CPU. • Medium-term scheduler may move (swap) some partially-executed jobs from memory to disk (to enhance performance). 7 ©Ahmed Hagag Operating Systems Basic Concepts (4/11)
  8. 8. 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. 8 ©Ahmed Hagag Operating Systems Basic Concepts (5/11)
  9. 9. CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates 9 ©Ahmed Hagag Operating Systems Basic Concepts (6/11)
  10. 10. Scheduling can be • Non-preemptive ➢ Once a process is allocated the CPU, it does not leave until terminate. • Preemptive ➢ OS can force (preempt) a process from CPU at anytime. ✓ Say, to allocate CPU to another higher-priority process. 10 ©Ahmed Hagag Operating Systems Basic Concepts (7/11)
  11. 11. Non-preemptive and Preemptive Which is harder to implement? and why? 11 ©Ahmed Hagag Operating Systems Basic Concepts (8/11)
  12. 12. Non-preemptive and Preemptive • Preemptive is harder: Need to maintain consistency of data shared between processes, and more importantly, kernel data structures (e.g., I/O queues). 12 ©Ahmed Hagag Operating Systems Basic Concepts (9/11)
  13. 13. 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. 13 ©Ahmed Hagag Operating Systems Basic Concepts (10/11)
  14. 14. Dispatcher 14 ©Ahmed Hagag Operating Systems Basic Concepts (11/11)
  15. 15. Dispatcher 15 ©Ahmed Hagag Operating Systems Basic Concepts (11/11)
  16. 16. Dispatcher 16 ©Ahmed Hagag Operating Systems Basic Concepts (11/11)
  17. 17. Dispatcher 17 ©Ahmed Hagag Operating Systems Basic Concepts (11/11)
  18. 18. • 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. (time from submission to termination) • 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. 18 ©Ahmed Hagag Operating Systems Scheduling Criteria (1/2)
  19. 19. Scheduling Algorithm Optimization Criteria • Max CPU utilization. • Max throughput. • Min turnaround time. • Min waiting time. • Min response time. 19 ©Ahmed Hagag Operating Systems Scheduling Criteria (2/2)
  20. 20. • There are many different CPU-scheduling algorithms: 1. First Come, First Served (FCFS). 2. Shortest Job First (SJF). ➢ Preemptive SJF. ➢ Non-Preemptive SJF. 3. Priority. 4. Round Robin. 5. Multilevel queues. 20 ©Ahmed Hagag Operating Systems Scheduling Algorithms (1/30)
  21. 21. 1. First-Come, First-Served (FCFS) Scheduling 21 ©Ahmed Hagag Operating Systems Scheduling Algorithms (2/30) 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: Note: A process may have many CPU bursts, but in the following examples we show only one for simplicity.
  22. 22. 1. First-Come, First-Served (FCFS) Scheduling 22 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/30) 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: P P P 1 2 3 0 24 30 27
  23. 23. 1. First-Come, First-Served (FCFS) Scheduling 23 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/30) 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: P P P 1 2 3 0 24 30 27
  24. 24. 1. First-Come, First-Served (FCFS) Scheduling 24 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/30) 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: P P P 1 2 3 0 24 30 27
  25. 25. 1. First-Come, First-Served (FCFS) Scheduling 25 ©Ahmed Hagag Operating Systems Scheduling Algorithms (4/30) 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: P P P 1 2 3 0 24 30 27 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  26. 26. 1. First-Come, First-Served (FCFS) Scheduling 26 ©Ahmed Hagag Operating Systems Scheduling Algorithms (4/30) 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: P P P 1 2 3 0 24 30 27 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 0 24 27 Average waiting time: (0 + 24 + 27)/3 = 17
  27. 27. 1. First-Come, First-Served (FCFS) Scheduling 27 ©Ahmed Hagag Operating Systems Scheduling Algorithms (5/30) 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: P P P 1 2 3 0 24 30 27 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  28. 28. 1. First-Come, First-Served (FCFS) Scheduling 28 ©Ahmed Hagag Operating Systems Scheduling Algorithms (5/30) 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: P P P 1 2 3 0 24 30 27 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 24 27 30 Average turnaround time: (24 + 27 + 30)/3 = 27
  29. 29. 1. First-Come, First-Served (FCFS) Scheduling 29 ©Ahmed Hagag Operating Systems Scheduling Algorithms (6/30) 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: P P P 1 2 3 0 24 30 27 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  30. 30. 1. First-Come, First-Served (FCFS) Scheduling 30 ©Ahmed Hagag Operating Systems Scheduling Algorithms (6/30) 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: P P P 1 2 3 0 24 30 27 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 0 24 27 Average response time: (0 + 24 + 27)/3 = 17
  31. 31. 1. First-Come, First-Served (FCFS) Scheduling 31 ©Ahmed Hagag Operating Systems Scheduling Algorithms (7/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is:
  32. 32. 1. First-Come, First-Served (FCFS) Scheduling 32 ©Ahmed Hagag Operating Systems Scheduling Algorithms (8/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3
  33. 33. 1. First-Come, First-Served (FCFS) Scheduling 33 ©Ahmed Hagag Operating Systems Scheduling Algorithms (8/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3
  34. 34. 1. First-Come, First-Served (FCFS) Scheduling 34 ©Ahmed Hagag Operating Systems Scheduling Algorithms (8/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3
  35. 35. 1. First-Come, First-Served (FCFS) Scheduling 35 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  36. 36. 1. First-Come, First-Served (FCFS) Scheduling 36 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 6 0 3 Average waiting time: (6 + 0 + 3)/3 = 3
  37. 37. 1. First-Come, First-Served (FCFS) Scheduling 37 ©Ahmed Hagag Operating Systems Scheduling Algorithms (10/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  38. 38. 1. First-Come, First-Served (FCFS) Scheduling 38 ©Ahmed Hagag Operating Systems Scheduling Algorithms (10/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 30 3 6 Average turnaround time: (30 + 3 + 6)/3 = 13
  39. 39. 1. First-Come, First-Served (FCFS) Scheduling 39 ©Ahmed Hagag Operating Systems Scheduling Algorithms (11/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  40. 40. 1. First-Come, First-Served (FCFS) Scheduling 40 ©Ahmed Hagag Operating Systems Scheduling Algorithms (11/30) Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt Chart for the schedule is: P1 0 3 6 30 P2 P3 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 6 0 3 Average response time: (6 + 0 + 3)/3 = 3
  41. 41. • FCFS is fair in the formal sense or human sense of fairness. • but it is unfair in the sense that long jobs take priority over short jobs and unimportant jobs make important jobs wait. • One of the major drawbacks of this scheme is that the waiting time and the average turnaround time is often quite long. 41 ©Ahmed Hagag Operating Systems Scheduling Algorithms (12/30) 1. First-Come, First-Served (FCFS) Scheduling
  42. 42. • 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. 42 ©Ahmed Hagag Operating Systems Scheduling Algorithms (13/30) 2. Shortest-Job-First (SJF) Scheduling
  43. 43. 2.1 Shortest-Job-First (SJF) Scheduling 43 ©Ahmed Hagag Operating Systems Scheduling Algorithms (14/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart
  44. 44. 2.1 Shortest-Job-First (SJF) Scheduling 44 ©Ahmed Hagag Operating Systems Scheduling Algorithms (15/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2
  45. 45. 2.1 Shortest-Job-First (SJF) Scheduling 45 ©Ahmed Hagag Operating Systems Scheduling Algorithms (15/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2
  46. 46. 2.1 Shortest-Job-First (SJF) Scheduling 46 ©Ahmed Hagag Operating Systems Scheduling Algorithms (15/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2
  47. 47. 2.1 Shortest-Job-First (SJF) Scheduling 47 ©Ahmed Hagag Operating Systems Scheduling Algorithms (15/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2
  48. 48. 2.1 Shortest-Job-First (SJF) Scheduling 48 ©Ahmed Hagag Operating Systems Scheduling Algorithms (16/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  49. 49. 2.1 Shortest-Job-First (SJF) Scheduling 49 ©Ahmed Hagag Operating Systems Scheduling Algorithms (16/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 3 16 9 0 Average waiting time: (3 + 16 + 9 + 0)/4 = 7
  50. 50. 2.1 Shortest-Job-First (SJF) Scheduling 50 ©Ahmed Hagag Operating Systems Scheduling Algorithms (17/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  51. 51. 2.1 Shortest-Job-First (SJF) Scheduling 51 ©Ahmed Hagag Operating Systems Scheduling Algorithms (17/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 9 24 16 3 Average Turnaround time: (9 + 24 + 16 + 3)/4 = 13
  52. 52. 2.1 Shortest-Job-First (SJF) Scheduling 52 ©Ahmed Hagag Operating Systems Scheduling Algorithms (18/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  53. 53. 2.1 Shortest-Job-First (SJF) Scheduling 53 ©Ahmed Hagag Operating Systems Scheduling Algorithms (18/30) ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P3 0 3 24 P4 P1 16 9 P2 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 3 16 9 0 Average Response time: (3 + 16 + 9 + 0)/4 = 7
  54. 54. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 54 ©Ahmed Hagag Operating Systems Scheduling Algorithms (19/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart
  55. 55. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 55 ©Ahmed Hagag Operating Systems Scheduling Algorithms (20/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17
  56. 56. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 56 ©Ahmed Hagag Operating Systems Scheduling Algorithms (20/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17
  57. 57. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 57 ©Ahmed Hagag Operating Systems Scheduling Algorithms (20/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17
  58. 58. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 58 ©Ahmed Hagag Operating Systems Scheduling Algorithms (20/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart
  59. 59. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 59 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  60. 60. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 60 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 (0 − 0) (1 − 1) (10 − 2) (5 − 3) Average waiting time: (0 + 0 + 8 + 2)/4 = 2.5 msec
  61. 61. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 61 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  62. 62. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 62 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 (1 − 0) (5 − 1) (17 − 2) (10 − 3) Average Turnaround time: (1 + 4 + 15 + 7)/4 = 6.75 msec
  63. 63. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 63 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  64. 64. 2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF ) 64 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 1 P2 1 4 P3 2 7 P4 3 5 Non-Preemptive SJF Gantt Chart Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 (0 − 0) (1 − 1) (10 − 2) (5 − 3) Average Response time: (0 + 0 + 8 + 2)/4 = 2.5 msec
  65. 65. 65 ©Ahmed Hagag Operating Systems Scheduling Algorithms (24/30) Determining Length of Next CPU Burst Can only estimate the length – should be similar to the previous one Then pick process with shortest predicted next CPU burst Can be done by using the length of previous CPU bursts, using exponential averaging Commonly, set to ½ Preemptive version called shortest-remaining-time-first : Define 4. 1 0 , 3. burst CPU next for the value predicted 2. burst CPU of length actual 1. 1   = = +   n th n n t ( ) . 1 1 n n n t     − + = = 
  66. 66. 66 ©Ahmed Hagag Operating Systems Scheduling Algorithms (25/30) Prediction of the Length of the Next CPU Burst
  67. 67. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 67 ©Ahmed Hagag Operating Systems Scheduling Algorithms (26/30) 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
  68. 68. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 68 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) 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 0 ms
  69. 69. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 69 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) 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 0 ms P4 0 1 26 P1 P2 10 P3 P1 5 17
  70. 70. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 70 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 1 ms P4 0 1 26 P1 P2 10 P3 P1 5 17
  71. 71. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 71 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 2 ms P4 0 1 26 P1 P2 10 P3 P1 5 17 2 3
  72. 72. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 72 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 2 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 3 ms P4 0 1 26 P1 P2 10 P3 P1 5 17 2 3
  73. 73. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 73 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 2 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 3 ms P4 0 1 26 P1 P2 10 P3 P1 5 17 2 3
  74. 74. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 74 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 2 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 5 ms
  75. 75. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 75 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 2 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 10 ms P4 0 1 26 P1 P2 10 P3 P1 5 17
  76. 76. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 76 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 2 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 17 ms P4 0 1 26 P1 P2 10 P3 P1 5 17
  77. 77. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 77 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/30) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 8 7 P2 1 4 3 2 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart 26 ms P4 0 1 26 P1 P2 10 P3 P1 5 17
  78. 78. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 78 ©Ahmed Hagag Operating Systems Scheduling Algorithms (28/30) ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  79. 79. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 79 ©Ahmed Hagag Operating Systems Scheduling Algorithms (28/30) ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 10 − 1 1 − 1 17 − 2 5 − 3 = 𝟗 = 𝟎 = 𝟏𝟓 = 𝟐 Average waiting time = [9+0+15+2]/4 = 26/4 = 6.5 msec
  80. 80. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 80 ©Ahmed Hagag Operating Systems Scheduling Algorithms (29/30) ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  81. 81. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 81 ©Ahmed Hagag Operating Systems Scheduling Algorithms (29/30) ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 17 − 0 5 − 1 26 − 2 10 − 3 = 𝟏𝟕 = 𝟒 = 𝟐𝟒 = 𝟕 Average turnaround time = [17+4+24+7]/4 = 52/4 = 13 msec
  82. 82. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 82 ©Ahmed Hagag Operating Systems Scheduling Algorithms (30/30) ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  83. 83. 2.2 Shortest-remaining-time-first (Preemptive SJF ) 83 ©Ahmed Hagag Operating Systems Scheduling Algorithms (30/30) ProcessAarri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P4 0 1 26 P1 P2 10 P3 P1 5 17 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 − 0 1 − 1 17 − 2 5 − 3 = 𝟎 = 𝟎 = 𝟏𝟓 = 𝟐 Average response time = [0+0+15+2]/4 = 17/4 = 4.25 msec
  84. 84. • 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. (time from submission to termination) • 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. 84 ©Ahmed Hagag Operating Systems Scheduling Criteria (1/2)
  85. 85. Scheduling Algorithm Optimization Criteria • Max CPU utilization. • Max throughput. • Min turnaround time. • Min waiting time. • Min response time. 85 ©Ahmed Hagag Operating Systems Scheduling Criteria (2/2)
  86. 86. • There are many different CPU-scheduling algorithms: 1. First Come, First Served (FCFS). 2. Shortest Job First (SJF). ➢ Preemptive SJF. ➢ Non-Preemptive SJF. 3. Priority. 4. Round Robin. 5. Multilevel queues. 86 ©Ahmed Hagag Operating Systems Scheduling Algorithms (1/25)
  87. 87. 87 ©Ahmed Hagag Operating Systems Scheduling Algorithms (1/25) 3. 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 priority scheduling where priority is the inverse of predicted next CPU burst time Problem  Starvation – low priority processes may never execute Solution  Aging – as time progresses increase the priority of the process
  88. 88. 3. Priority Scheduling 88 ©Ahmed Hagag Operating Systems Scheduling Algorithms (2/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart
  89. 89. 3. Priority Scheduling 89 ©Ahmed Hagag Operating Systems Scheduling Algorithms (2/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Highest priority
  90. 90. 3. Priority Scheduling 90 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart
  91. 91. 3. Priority Scheduling 91 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart
  92. 92. 3. Priority Scheduling 92 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart
  93. 93. 3. Priority Scheduling 93 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart
  94. 94. 3. Priority Scheduling 94 ©Ahmed Hagag Operating Systems Scheduling Algorithms (3/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart
  95. 95. 3. Priority Scheduling 95 ©Ahmed Hagag Operating Systems Scheduling Algorithms (4/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
  96. 96. 3. Priority Scheduling 96 ©Ahmed Hagag Operating Systems Scheduling Algorithms (4/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓 6 0 16 18 1 Average Waiting time = [6+0+16+18+1]/5 = 8.2 msec
  97. 97. 3. Priority Scheduling 97 ©Ahmed Hagag Operating Systems Scheduling Algorithms (5/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
  98. 98. 3. Priority Scheduling 98 ©Ahmed Hagag Operating Systems Scheduling Algorithms (5/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓 16 1 18 19 6 Average Turnaround time = [16+1+18+19+6]/5 = 12 msec
  99. 99. 3. Priority Scheduling 99 ©Ahmed Hagag Operating Systems Scheduling Algorithms (6/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
  100. 100. 3. Priority Scheduling 100 ©Ahmed Hagag Operating Systems Scheduling Algorithms (6/25) ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓 6 0 16 18 1 Average Response time = [6+0+16+18+1]/5 = 8.2 msec
  101. 101. 101 ©Ahmed Hagag Operating Systems Scheduling Algorithms (7/25) 4. Round Robin (RR) Scheduling Each process gets a small unit of CPU time (time quantum q), 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 𝑛 processes in the ready queue and the time quantum is 𝑞, then each process gets 1/𝑛 of the CPU time in chunks of at most 𝑞 time units at once. No process waits more than (𝑛 − 1)𝑞 time units.
  102. 102. 102 ©Ahmed Hagag Operating Systems Scheduling Algorithms (7/25) 4. Round Robin (RR) Scheduling
  103. 103. 4. Round Robin (RR) Scheduling 103 ©Ahmed Hagag Operating Systems Scheduling Algorithms (8/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms
  104. 104. 4. Round Robin (RR) Scheduling 104 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  105. 105. 4. Round Robin (RR) Scheduling 105 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  106. 106. 4. Round Robin (RR) Scheduling 106 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  107. 107. 4. Round Robin (RR) Scheduling 107 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  108. 108. 4. Round Robin (RR) Scheduling 108 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  109. 109. 4. Round Robin (RR) Scheduling 109 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  110. 110. 4. Round Robin (RR) Scheduling 110 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  111. 111. 4. Round Robin (RR) Scheduling 111 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  112. 112. 4. Round Robin (RR) Scheduling 112 ©Ahmed Hagag Operating Systems Scheduling Algorithms (9/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  113. 113. 4. Round Robin (RR) Scheduling 113 ©Ahmed Hagag Operating Systems Scheduling Algorithms (10/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms # of context switches = ?? P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  114. 114. 4. Round Robin (RR) Scheduling 114 ©Ahmed Hagag Operating Systems Scheduling Algorithms (10/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms # of context switches = 7 P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1
  115. 115. 4. Round Robin (RR) Scheduling 115 ©Ahmed Hagag Operating Systems Scheduling Algorithms (11/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  116. 116. 4. Round Robin (RR) Scheduling 116 ©Ahmed Hagag Operating Systems Scheduling Algorithms (11/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1 Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 0 + (10 − 4) 4 7 Average waiting time: (6 + 4 + 7)/3 = 5.667 ms
  117. 117. 4. Round Robin (RR) Scheduling 117 ©Ahmed Hagag Operating Systems Scheduling Algorithms (12/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  118. 118. 4. Round Robin (RR) Scheduling 118 ©Ahmed Hagag Operating Systems Scheduling Algorithms (12/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1 Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 30 7 10 Average Turnaround time: (30 + 7 + 10)/3 = 15.667 ms
  119. 119. 4. Round Robin (RR) Scheduling 119 ©Ahmed Hagag Operating Systems Scheduling Algorithms (13/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑
  120. 120. 4. Round Robin (RR) Scheduling 120 ©Ahmed Hagag Operating Systems Scheduling Algorithms (13/25) ProcessA arri Burst TimeT P1 24 P2 3 P3 3 All the processes arrive at the same time 0. Round Robin (RR) scheduling of quantum: 4 ms P P P 1 1 1 0 18 30 26 14 4 7 10 22 P2 P3 P1 P1 P1 Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 0 4 7 Average Response time: (0 + 4 + 7)/3 = 3.667 ms
  121. 121. • There are many different CPU-scheduling algorithms: 1. First Come, First Served (FCFS). 2. Shortest Job First (SJF). ➢ Preemptive SJF. ➢ Non-Preemptive SJF. 3. Priority. 4. Round Robin. 5. Multilevel queues. 121 ©Ahmed Hagag Operating Systems Scheduling Algorithms (14/25)
  122. 122. 122 ©Ahmed Hagag Operating Systems Scheduling Algorithms (15/25) 5. Multilevel Queue Scheduling Ready queue is partitioned into separate queues, eg: foreground (interactive) background (batch) Process permanently in a given queue Each queue has its own scheduling algorithm: foreground – RR. background – FCFS.
  123. 123. 123 ©Ahmed Hagag Operating Systems Scheduling Algorithms (16/25) 5. Multilevel Queue Scheduling 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.
  124. 124. 124 ©Ahmed Hagag Operating Systems Scheduling Algorithms (17/25) 5. Multilevel Queue Scheduling
  125. 125. 125 ©Ahmed Hagag Operating Systems Scheduling Algorithms (18/25) 5. Multilevel Queue Scheduling Three queues: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS
  126. 126. 126 ©Ahmed Hagag Operating Systems Scheduling Algorithms (19/32) 5. Multilevel Queue Scheduling Scheduling A new job enters queue 𝑄0 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 𝑄1. At 𝑄1 job is again served FCFS and receives 16 additional milliseconds  If it still does not complete, it is preempted and moved to queue 𝑄2.
  127. 127. 5. Multilevel Queue Scheduling 127 ©Ahmed Hagag Operating Systems Scheduling Algorithms (20/32) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40 Multilevel Queue Fixed priority non-preemptive Using multi-processors or multi-core processor
  128. 128. 5. Multilevel Queue Scheduling 128 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2
  129. 129. 5. Multilevel Queue Scheduling 129 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40 𝑃1 𝑄0 𝑄1 𝑄2 7ms 𝑄0 𝑄1 𝑄2 7ms
  130. 130. 5. Multilevel Queue Scheduling 130 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40 𝑃1 𝑄0 𝑄1 𝑄2 7ms 𝑄0 𝑄1 𝑄2 7ms 𝑃2 8ms
  131. 131. 5. Multilevel Queue Scheduling 131 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 P3 2 20 P4 3 40 𝑃1 𝑃2 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 15ms 7ms 8ms
  132. 132. 5. Multilevel Queue Scheduling 132 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 P3 2 20 P4 3 40 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 15ms 7ms 8ms 8ms
  133. 133. 5. Multilevel Queue Scheduling 133 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 P3 2 20 12 P4 3 40 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 23ms 7ms 8ms 8ms
  134. 134. 5. Multilevel Queue Scheduling 134 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 P3 2 20 12 P4 3 40 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 23ms 𝑃4 7ms 8ms 8ms 8ms
  135. 135. 5. Multilevel Queue Scheduling 135 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 P3 2 20 12 P4 3 40 32 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 31ms 𝑃4 7ms 8ms 8ms 8ms 16ms
  136. 136. 5. Multilevel Queue Scheduling 136 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 P3 2 20 12 P4 3 40 32 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 31ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 12ms 36ms
  137. 137. 5. Multilevel Queue Scheduling 137 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 P3 2 20 12 P4 3 40 32 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 43ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 12ms 36ms
  138. 138. 5. Multilevel Queue Scheduling 138 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 P3 2 20 12 P4 3 40 32 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 43ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms
  139. 139. 5. Multilevel Queue Scheduling 139 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 59ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms
  140. 140. 5. Multilevel Queue Scheduling 140 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 67ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms
  141. 141. 5. Multilevel Queue Scheduling 141 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 67ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms
  142. 142. 5. Multilevel Queue Scheduling 142 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms
  143. 143. 5. Multilevel Queue Scheduling 143 ©Ahmed Hagag Operating Systems Scheduling Algorithms (21/32) ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms
  144. 144. 5. Multilevel Queue Scheduling 144 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  145. 145. 5. Multilevel Queue Scheduling 145 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 = 𝟎
  146. 146. 5. Multilevel Queue Scheduling 146 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 7 − 1 = 𝟎 = 𝟔
  147. 147. 5. Multilevel Queue Scheduling 147 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 7 − 1 15 + 8 − 2 = 𝟎 = 𝟔 = 𝟐𝟏
  148. 148. 5. Multilevel Queue Scheduling 148 ©Ahmed Hagag Operating Systems Scheduling Algorithms (22/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 7 − 1 15 + 8 − 2 23 + 12 + 8 − 3 = 𝟎 = 𝟔 = 𝟐𝟏 = 𝟒𝟎
  149. 149. 5. Multilevel Queue Scheduling 149 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  150. 150. 5. Multilevel Queue Scheduling 150 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 7 − 0 = 𝟕
  151. 151. 5. Multilevel Queue Scheduling 151 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 7 − 0 67 − 1 = 𝟕 = 𝟔𝟔
  152. 152. 5. Multilevel Queue Scheduling 152 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 7 − 0 67 − 1 43 − 2 = 𝟕 = 𝟔𝟔 = 𝟒𝟏
  153. 153. 5. Multilevel Queue Scheduling 153 ©Ahmed Hagag Operating Systems Scheduling Algorithms (23/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 7 − 0 67 − 1 43 − 2 83 − 3 = 𝟕 = 𝟔𝟔 = 𝟒𝟏 = 𝟖𝟎
  154. 154. 5. Multilevel Queue Scheduling 154 ©Ahmed Hagag Operating Systems Scheduling Algorithms (24/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
  155. 155. 5. Multilevel Queue Scheduling 155 ©Ahmed Hagag Operating Systems Scheduling Algorithms (24/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 − 0 = 𝟎
  156. 156. 5. Multilevel Queue Scheduling 156 ©Ahmed Hagag Operating Systems Scheduling Algorithms (24/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 − 0 7 − 1 = 𝟎 = 𝟔
  157. 157. 5. Multilevel Queue Scheduling 157 ©Ahmed Hagag Operating Systems Scheduling Algorithms (24/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 − 0 7 − 1 15 − 2 = 𝟎 = 𝟔 = 𝟏𝟑
  158. 158. 5. Multilevel Queue Scheduling 158 ©Ahmed Hagag Operating Systems Scheduling Algorithms (24/32) ProcessAarri Arrival TimeT P1 0 P2 1 P3 2 P4 3 𝑃1 𝑃2 𝑃2 𝑃3 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 83ms 𝑃4 7ms 8ms 8ms 𝑃2 𝑃3 8ms 16ms 𝑃4 12ms 16ms 36ms 𝑃4 16ms 7ms 67ms 59ms 43ms 31ms 15ms 23ms Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 − 0 7 − 1 15 − 2 23 − 3 = 𝟎 = 𝟔 = 𝟏𝟑 = 𝟐𝟎
  159. 159. 5. Multilevel Queue Scheduling 159 ©Ahmed Hagag Operating Systems Scheduling Algorithms (25/32) Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40 Multilevel Queue Fixed priority non-preemptive Using single-core processor
  160. 160. 5. Multilevel Queue Scheduling 160 ©Ahmed Hagag Operating Systems Scheduling Algorithms (26/32) 𝑄0 𝑄1 𝑄2 𝑄0 𝑄1 𝑄2 ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40
  161. 161. 5. Multilevel Queue Scheduling 161 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑄0 𝑄1 𝑄2 𝑃1 𝑄0 𝑄1 𝑄2 7ms 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40
  162. 162. 5. Multilevel Queue Scheduling 162 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑄0 𝑄1 𝑄2 𝑃1 𝑄0 𝑄1 𝑄2 7ms 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 P3 2 20 P4 3 40
  163. 163. 5. Multilevel Queue Scheduling 163 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms
  164. 164. 5. Multilevel Queue Scheduling 164 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms
  165. 165. 5. Multilevel Queue Scheduling 165 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 44 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms
  166. 166. 5. Multilevel Queue Scheduling 166 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms
  167. 167. 5. Multilevel Queue Scheduling 167 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms
  168. 168. 5. Multilevel Queue Scheduling 168 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms
  169. 169. 5. Multilevel Queue Scheduling 169 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms
  170. 170. 5. Multilevel Queue Scheduling 170 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms
  171. 171. 5. Multilevel Queue Scheduling 171 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms
  172. 172. 5. Multilevel Queue Scheduling 172 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms127ms 𝑃4 16ms
  173. 173. 5. Multilevel Queue Scheduling 173 ©Ahmed Hagag Operating Systems Scheduling Algorithms (27/32) 𝑃1 𝑄0 𝑄1 𝑄2 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms127ms 𝑃4 16ms
  174. 174. 5. Multilevel Queue Scheduling 174 ©Ahmed Hagag Operating Systems Scheduling Algorithms (28/32) 𝑃1 7ms ProcessAarri Arrival TimeT Burst Time P1 0 7 P2 1 60 52 36 36 24 8 P3 2 20 12 P4 3 40 32 16 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms127ms 𝑃4 16ms
  175. 175. 5. Multilevel Queue Scheduling 175 ©Ahmed Hagag Operating Systems Scheduling Algorithms (29/32) 𝑃1 7ms ProcessAarri Arrival TimeT P1 0 P2 1 36 24 8 P3 2 P4 3 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms127ms 𝑃4 16ms Waiting Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 6 + 16 + 28 13 + 24 20 + 28 + 36 = 𝟎 = 𝟓𝟎 = 𝟑𝟕 = 𝟖𝟒
  176. 176. 5. Multilevel Queue Scheduling 176 ©Ahmed Hagag Operating Systems Scheduling Algorithms (30/32) 𝑃1 7ms ProcessAarri Arrival TimeT P1 0 P2 1 36 24 8 P3 2 P4 3 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms127ms 𝑃4 16ms Turnaround Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 7 − 0 111 − 1 59 − 2 127 − 3 = 𝟕 = 𝟏𝟏𝟎 = 𝟓𝟕 = 𝟏𝟐𝟒
  177. 177. 5. Multilevel Queue Scheduling 177 ©Ahmed Hagag Operating Systems Scheduling Algorithms (31/32) 𝑃1 7ms ProcessAarri Arrival TimeT P1 0 P2 1 36 24 8 P3 2 P4 3 𝑃2 8ms 7ms 15ms 𝑃3 23ms 𝑃4 8ms 31ms 8ms 𝑃2 16ms 47ms 𝑃3 12ms 59ms 𝑃4 16ms 75ms 𝑃2 36ms 111ms127ms 𝑃4 16ms Response Time 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 0 − 0 7 − 1 15 − 2 23 − 3 = 𝟎 = 𝟔 = 𝟏𝟑 = 𝟐𝟎
  178. 178. Student Example: 178 ©Ahmed Hagag Operating Systems Scheduling Algorithms (32/32) Using a Multilevel Queue Scheduling algorithm in Fig.1. Consider the following processes with the relative CPU bursts. ProcessAarri Arrival TimeT Burst Time P1 0 20 P2 1 60 P3 2 5 P4 2 40 Multilevel Queue Fixed priority non-preemptive Fig.1
  179. 179. Dr. Ahmed Hagag ahagag@fci.bu.edu.eg https://www.youtube.com/channel/UCzYgAyyZTLfnLFjQexOKxbQ https://www.facebook.com/ahmed.hagag.71/

×