Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture
CPU Scheduling (Core Definitions) Scheduling Objectives CPU I/O Burst Cycle Working of CPU Scheduler Preemptive Scheduling Non-Preemptive Scheduling Dispatcher Scheduling Criteria Scheduling Algorithm Optimization Criteria First-Come, First-Served (FCFS) Scheduling FCFS Scheduling (non-preemptive) Shortest-Job-First (SJF) Scheduling Determining Length of Next CPU Burst Example of SJF Non-preemptive SJF Example Priority Scheduling Non-Preemptive Priority Scheduling Prove It (SJF is Optimal in Non- Preemptive) Round Robin (RR) Multilevel Queue Scheduling Multilevel Feedback Queue Today Agenda
CPU Scheduling (Core Definitions) General rule — keep the CPU busy; an idle CPU is a wasted CPU Major source of CPU idleness: I/O (or waiting for it) CPU scheduling (short-term scheduling) is the act of selecting the next process for the CPU to “service” once the current process leaves the CPU idle Many algorithms for making this selection Implementation-wise, CPU scheduling manipulates the operating system’s various PCB queues The dispatcher is the software that performs the dirty work of passing the CPU to the next selected process
Scheduling Objectives Maximize CPU utilization Maximize utilization of other resource(Disk, printer) Maximize throughout = number of jobs completed per unit time. Minimize waiting time = total time a job spends waiting in the various queues for resource to become available. Minimize turnaround time = Waiting + Computation + I /O time Minimize response time (timesharing) = time from entry of a command until first output starts to appear. Fairness: All comparable jobs should be treated equally
CPU I/O Burst Cycle Almost all processes alternate between two states in a continuing cycle, as shown in Figure below : A CPU burst of performing calculations, and An I/O burst, waiting for data transfer in or out of the system.
Working of CPU Scheduler Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them CPU scheduling decisions may take place when a process: 1.Switches from running to waiting state (Ex. I/O Request) 2.Switches from running to ready state (Ex. Interrupts Occur) 3.Switches from waiting to ready (Ex. Completion of I/O ) 4.Terminates Scheduling under 1 and 4 is non-preemptive All other scheduling is preemptive
Preemptive Scheduling Preemptive: Preemptive algorithms are driven by the notion of prioritized computation. The process with the highest priority should always be the one currently using the processor. If a process is currently using the processor and a new process with a higher priority enters, the ready list, the process on the processor should be removed and returned to the ready list until it is once again the highest- priority process in the system
Non-Preemptive Scheduling Non-Preemptive: Non-preemptive algorithms are designed so that once a process enters the running state(is allowed a process), it is not removed from the processor until it has completed its service time. context_switch() is called only when the process terminates or blocks.
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
Scheduling Criteria
Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time
Scheduling Algorithms 1.First-Come, First-Served (FCFS) Scheduling 2.Shortest-Job-First (SJF) Scheduling 3.Priority Scheduling 4.Round robin Scheduling 5.Multilevel queue Scheduling 6.Multilevel feedback-queue Scheduling
First-Come, First-Served (FCFS) Scheduling ProcessBurst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: ( )/3 = 17 P1P1 P2P2 P3P
FCFS Scheduling (non-preemptive) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: ( )/3 = 3 Much better than previous case Convoy effect short process behind long process P1P1 P3P3 P2P
Convoy Effect
FCFS Scheduling (non-preemptive) Process Name Arrival timeBurst timeFinish timeWaiting time Process Process Process Average waiting time: ( )/3 = 3 It is not good scheduling algorithm for system where response time is important (i.e. real time or time sharing system)
Shortest-Job-First (SJF) Scheduling Pick the job that will take the least amount of time to complete 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 Two schemes of this algorithm Non-preemptive preemptive
Determining Length of Next CPU Burst Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging
Example of SJF ProcessBurst time P16 P28 P37 P43 P1P3P
Non-preemptive SJF Example ProcessArrival timeBurst time P107 P224 P342 P454 P1P3P2P Process Name Arrival timeBurst timeFinish timeWaiting time Process Process Process Process Gantt Chart Average waiting time: ( ) / 4 = 4.5 Waiting time for the processes in this example Note:- Waiting time= Finish Time-(Arrival time + Burst time)
Preemptive or non-preemptive Set Scheduling algorithm –Every 1 arrival time –100 Microsecond –New job come Shortest-remaining- time-first scheduling (SRTF)
Preemptive SJF Example ProcessArrival timeBurst time P107 P224 P341 P454 P1P2P3P2P4P Process Name Arrival timeBurst timeFinish timeWaiting time Process Process Process Process Gantt Chart Average waiting time: ( ) / 4 = 3 Waiting time for the processes in this example
Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer mean highest priority) Preemptive Non-preemptive 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 Priority Scheduling
Non-Preemptive Priority Scheduling ProcessBurst timePriority P1103 P211 P324 P415 P552 P2P5P1P3P Gantt Chart Average waiting time: ( ) / 5 = 8.2
Preemptive Priority Scheduling ProcessArrival timeBurst timePriority P10103 P2211 P3422 P4614 P5855 P1P2P1P3P1P4P Gantt Chart
Prove It (SJF is Optimal in Non- Preemptive) Logical Argument: Decrease in the wait times for short processes is much more than increase in the wait times for long processes P1P2P Gantt Chart P3P2P Change Order F=(0+5+8) / 3= 4.33 S=(0+2+5) / 3=2.33
Round Robin (RR) Each process gets a small unit of CPU time (time quantum), usually milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)* (q+t cs ) time units. If n=6 and q=5 then (6-1)*5= 25 Performance –q large FIFO or FCFS –q small q must be large with respect to context switch, otherwise overhead is too high
Example of RR with Time Quantum = 4 The Gantt chart is: Typically, higher average turnaround than SJF, but better response and it is preemptive scheduling P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P ProcessBurst time P124 P23 P33
Example of RR with Time Quantum = 20 The Gantt chart is: ProcessBurst time FSTF P P217 P P4244 P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P P3P3 P3P
Example of RR with Time Quantum = 20 Average waiting time: - 73 Average waiting time for SJF: - 38 Typically, higher average turnaround than SJF, but better response ProcessTurnaround time Waiting time P – 53 = 81 P23737 – 17 = 20 P – 68 = 94 P – 24 = 97
Time Quantum and Context Switch Time
Multilevel Queue Scheduling Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm –foreground – RR –background – FCFS
Multilevel Queue Scheduling
Scheduling must be done between the queues: –Fixed (Absolute) 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
Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way Multilevel-feedback-queue scheduler defined by the following parameters: –number of queues –scheduling algorithms for each queue –method used to determine when to upgrade a process –method used to determine when to demote a process –method used to determine which queue a process will enter when that process needs service
Example of Multilevel Feedback Queue Three queues: –Q 0 – RR with time quantum 8 milliseconds –Q 1 – RR time quantum 16 milliseconds –Q 2 – FCFS Scheduling –A new job enters queue Q 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 Q 1. –At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.
Multilevel Feedback Queue