1 / 15

CPU Scheduler

CPU Scheduler. In multiprogramming, more than one process may be in memory ready to execute QUESTION: In what order these processes will be executed?. Criteria Fair All processes should be able to access all resources in a fair manner Discriminatory

Download Presentation

CPU Scheduler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPU Scheduler • In multiprogramming, more than one process may be in memory ready to execute • QUESTION: In what order these processes will be executed? • Criteria • Fair • All processes should be able to access all resources in a fair manner • Discriminatory • Processes with different priorities should be supported • Performance • Keep the CPU working as much as possible • Increase the number of processes completed per unit time • Minimize the time it takes to complete a process

  2. Dispatcher From the time a process is placed in main memory until completion, the process status changes between execution and Input/Output states CPU • Dispatcher (CPU Scheduler) • Selects one of the processes that are in the main memory and ready for execution and asks CPU to execute that process I/O CPU I/O CPU I/O

  3. First-Come-First-Served Process P1 P2 P3 CPU Time (sec) 24 3 3 • Order processes arrive at main memory: P1, P2, P3 Proces P1 P2 P3 Arrival time (sec) 0 1 2 P1 P2 P3 0 24 27 30 • Waiting times of processes: P1=0, P2=23, P3=25 • Average waiting time: (0+23+25)/3 = 16 • Order processes arrive at main memory: P2, P3, P1 Proc. P1 P2 P3 Arrival time (sec) 2 0 1 P2 P3 P1 0 3 6 30 • Waiting times of processes: P1=4, P2=0, P3=2 • Average waiting time: (4+0+2)/3 = 2

  4. Shortest-Job-First (SJF) • Calculate the remaining CPU time for each process. Schedule the job with the shortest remaining CPU time. • Two different versions • Non-preemptive SJF: Once CPU is given to a process, CPU will execute the process until the process can not continue (end or requests I/O). • Preemptive SJF: A new scheduling decision is made when either a new process is placed in the main memory or until the process can not continue. • Shortest Job First algorithm guarantees the minimum average waiting time and accordingly it is the best algorithm.

  5. Non-Preemptive SJF Process P1 P2 P3 P4 Arrival time (sec) 0 2 4 5 CPU time (sec) 7 4 1 4 • Waiting times of processes: P1=0, P2=6, P3=3, P4=7 • Average waiting time: (0+6+3+7)/4 = 4 P1 P3 P2 P4 0 3 7 8 12 16 • Waiting time of a process = Completion time – Arrival time – CPU time

  6. Preemptive SJF Processes P1 P2 P3 P4 Arrival time (sec) 0 2 4 5 CPU time (sec) 7 4 1 4 • Waiting time of a process = Completion time – Arrival time – CPU time • Waiting times of processes: P1=9, P2=1, P3=0, P4=2 • Average waiting time: (9+1+0+2)/4 = 3 P1 P2 P3 P2 P4 P1 0 2 4 5 7 11 16

  7. Round Robin • Give the CPU to each process for a small period time. In general, this time may vary between 10 ms and 100 ms. This duration is referred to as the Quantum (q). When the quantum expires, the execution is interrupted and the process is added to the end of the process queue. The CPU is given to the process waiting at the head of the queue. • If there are n processes waiting and quantum is equal to q, each process takes 1/n of the CPU time and holds the CPU for q time units. Accordingly, no process waits for CPU for more than (n-1)q time units. • The value of q should be longer than the dispatcher to schedule the next job. Otherwise CPU will spend more time to execute dispatcher than processes.

  8. Round Robin Processes P1 P2 P3 P4 CPU time (ms) 53 17 68 24 P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 quantum = 20 ms 0 117 20 37 57 77 97 121 134 154 162 • Waiting time of a process = Completion time – Arrival time – CPU time • Waiting time of each process: • P1=81, P2=20, P3=94, P4=97 • Average waiting time: (81+20+94+97)/4 = 73 ms

  9. Round Robin Processes P1 P2 P3 P4 CPU time (ms) 53 17 68 24 P1 P2 P3 P4 P1 P2 P3 P4 P1 P3 P4 P1 P3 P1 P3 P1 P3 P3 quantum = 10 ms 0 20 30 10 40 50 57 67 77 87 97 111 131 144 162 101 121 141 154 • Waiting time of a process = Completion time – Arrival time – CPU time • Waiting time of each process: • P1=91, P2=40, P3=94, P4=77 • Average waiting time: (91+40+94+77)/4 = 75.5 ms

  10. Process P1 P2 P3 P4 Arrival time (sec) 0 2 4 5 CPU time (sec) 7 4 1 1 Preemptive SJF Calculate the average waiting time 0 Non-preemptive SJF 0 Round Robin quantum=1 sn 0

  11. Processes P1 P2 P3 P4 Arrival time (sec) 0 2 4 5 CPU time (sec) 5 4 3 1 Calculate the average waiting time Preemptive SJF 0 Non Preemptive SJF 0 Round Robin quantum=1 sn 0

  12. Priority Scheduling • Assign a number to each process that specifies its priority • CPU is given to a process with the highest priority (smaller the number higher the priority) • Non-preemptive • Preemptive • Problem: Starvation (Low priority processes may never execute) • Solution: Aging (The priority of a process increases in time) Priority=0 CPU Priority=1

  13. Multi-Level Queues • Ready queue is divided into multiple levels of queues • Each queue can have its own scheduling algorithm • Queues need to be ordered among them • Fixed priority has the problem of starvation • Each queue can be given some total time before the CPU is given to another queue in some pre-defined order. System Processes Interactive Processes CPU Batch Processes Student Processes

  14. Multi-Level Queues with Feedback • A process can be moved from one queue to another (for example using aging) • Parameters of multi-level queues • Number of queues • Scheduling algorithm used in each queue • Aging method used to increase the age of a process • Aging method used to decrease the age of a process • Method used to determine which queue a new process will be added to

  15. Example • Three queues • Q0: FCFS + quantum for queue 0 is 8 ms • Q1: FCFS + quantum for queue 1 is 16 ms • Q2: FCFS • Scheduling • Processes in Q0 are scheduled using FCFS. CPU is given to this queue for at most 8 ms. At the end of this period, the CPU is given to Q1. • Processes in Q1 are scheduled using FCFS. CPU is given to this queue for at most 16 ms. At the end of this period, the CPU is given to Q2. • A process at Q2 runs until completion. At the end, the CPU is given to Q0.

More Related