Scheduling (Priority Based) Lecture 09
CPU Scheduling Scheduling processes in the ready queue Short-term scheduler Different types of schedulers 16 September 2018
Life of a Process 16 September 2018
Histogram of CPU-burst Times 16 September 2018
CPU Scheduler Short-term scheduler Selects a process from among the processes in the ready queue Invokes the dispatcher to have the CPU allocated to the selected process 16 September 2018
Dispatcher Dispatcher 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 start (or restart) it 16 September 2018
Dispatcher Dispatch latency – time it takes for the dispatcher to stop one process and start another running. Typically, a few microseconds 16 September 2018
CPU Scheduler CPU scheduling decisions may take place when a process: Switches from running to waiting state Switches from running to ready state Switches from waiting to ready Terminates 16 September 2018
CPU Scheduler Scheduling under 1 and 4 is nonpreemptive. All other scheduling is preemptive. 16 September 2018
Scheduling Criteria 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 16 September 2018
Scheduling Criteria 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 (for time-sharing environment) 16 September 2018
Optimization Criteria Maximize CPU utilization Maximize throughput Minimize turnaround time Minimize waiting time Minimize response time 16 September 2018
FCFS Scheduling The process that enters the ready queue first is scheduled first, regardless of the size of its next CPU burst Example: Process Burst Time P1 24 P2 3 P3 3 Suppose that processes arrive into the system in the order: P1, P2 , P3 16 September 2018
FCFS Scheduling Processes are served in the order: P1, P2, P3 The Gantt Chart for the schedule is: Waiting times P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0+24+27)/3 = 17 P1 P2 P3 24 27 30 16 September 2018
FCFS Scheduling Suppose that processes arrive in the order: P2 , P3 , P1 . The Gantt chart for the schedule is: Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Convoy effect short process behind long process P1 P3 P2 6 3 30 16 September 2018
Shortest-Job-First (SJF) Scheduling Process with the shortest CPU burst is scheduled first. Non-preemptive – once CPU given to a process it cannot be preempted until completes its CPU burst. 16 September 2018
Shortest-Job-First (SJF) Scheduling Preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt it—Shortest-Remaining-Time-First (SRTF). SJF is optimal non-preemptive scheduling algorithm – gives minimum average waiting time for a given set of processes. 16 September 2018
Non-Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 Gantt chart Average waiting time = (0+6+3+7)/4 = 4 P1 P3 P2 7 16 P4 12 16 September 2018
Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 Gantt chart Average waiting time = (9 + 1 + 0 +2)/4 = 3 P3 P2 4 2 11 P4 5 7 P1 16 16 September 2018
Example of Shortest-remaining-time-first Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec 16 September 2018
SJF—CPU Burst of a Process Can only estimate the length of the next CPU burst. Can be done by using the length of previous CPU bursts, using exponential averaging. 16 September 2018
SJF is Optimal Logical Argument: Decrease in the wait times for short processes is much more than increase in the wait times for long processes P1 P2 P3 5 3 2 P3 P2 P1 2 3 5 16 September 2018
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 Non-preemptive 16 September 2018
Priority Scheduling SJF is a priority scheduling where priority is the predicted next CPU burst time. Problem Starvation – low priority processes may never execute. Solution Aging – as time progresses increase the priority of the process. 16 September 2018
Example of Priority Scheduling Process A arrive Burst Time Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = 8.2 msec 16 September 2018
Round Robin (RR) Each process gets a small unit of CPU time, called time slice or quantum, which is usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. 16 September 2018
Round Robin (RR) If there are n processes in the ready queue, the time quantum is q, and context switch time is tcs, then no process waits more than (n-1)(q+tcs) time units Used in time-sharing systems where response time is an important performance criteria 16 September 2018
Round Robin (RR) Performance q large FCFS q small q must be large with respect to context switch, otherwise overhead is too high. 16 September 2018
Round Robin Example The Gantt chart with quantum 20 is: Process Burst Time P1 53 — 33 — 13 P2 17 P3 68 — 48 — 28 — 8 P4 24 — 4 The Gantt chart with quantum 20 is: P1 P2 P3 P4 20 37 57 77 97 117 121 134 154 162 16 September 2018
Round Robin Example Average waiting time = 73 Process Turnaround Time Waiting Time P1 134 134 – 53 = 81 P2 37 37 – 17 = 20 P3 162 162 – 68 = 94 P4 121 121 – 24 = 97 Average waiting time = 73 Average waiting time for SJF = 38 Typically, higher average turnaround than SJF, but better response. 16 September 2018
Example of RR with Time Quantum = 4 Process Burst Time P1 24 P2 3 P3 3 The Gantt chart is: Typically, higher average turnaround than SJF, but better response q should be large compared to context switch time q usually 10ms to 100ms, context switch < 10 usec 16 September 2018
Quantum vs Context Switch Process Time = 10 Quantum Context Switches 12 0 6 1 1 9 16 September 2018
Turnaround Time vs Quantum 16 September 2018