Ch10: Job and CPU Scheduling Introduction Scheduling Levels Scheduling Objectives Scheduling Criteria Preemptive vs. Non-preemptive Scheduling Priorities Deadlock Scheduling Processor Scheduling FIFO RR SJF SRT Introduction What is schedule? A program of events or appointments expected in a given time. What is Scheduling: Setting an order and time for planned events. Processor scheduling: Is the problem of determining when processors should be assigned, and to which processes. Scheduling Levels High-level Scheduling/ job Scheduling/ Admission Scheduling: it determines which jobs shall be allowed to compete actively for the resources of the system. Intermediate-level Scheduling: It determines which processes shall be allowed to compete for the CPU. Ex: suspend & resume. Low-level Scheduling (Dispatching): It determines which ready processes will be assigned the CPU when it next becomes available, and actually assigns the CPU to this process. It dispatches the CPU to the process. Scheduling objectives A scheduling discipline should: Be fair. Maximize throughput. Maximize # of interactive users receiving acceptable response times. 55 Minimize overhead: Overhead is commonly viewed as wasted resource. Balance resource use. Avoid indefinite postponement. This is best accomplished by aging, as a process waits for a resource, its priority should grow. Enforce priorities. Give preference to processes holding key resources. Give better service to processes exhibiting desirable behavior. Ex: low paging rates. Degrade gracefully under heavy loads. Scheduling Criteria To realize the scheduling objectives, a scheduling mechanism should consider: CPU utilization: we want to keep the CPU busy as much as possible. Throughput: # of processes completed per unit of time. Turnaround time: the interval from the time of submission to the time of completion. Waiting time: the amount of time a process spends waiting in the ready queue. Response time: the time from submission of a request until the first response. Preemtive Vs. Non-preeptive Scheduling Preemptive Vs. Non-peermptive The CPU can be taken a way. Once a process has been given the CPU, the CPU can’t be taken a way from that process. 56 Priorities Static Vs. Dynamic Static priority: don’t change. Dynamic priority: responsive to change. Purchased Priorities A user with a rush job may be willing to pay a premium for a higher level of service. Deadline Scheduling Certain jobs are scheduled to be completed by a specific time or deadline. Processor Scheduling Algorithms First-In-First-Out Scheduling (FIFO) Processors are dispatched to their arrival time on the ready queue. Sometimes it is called First-Come-First-Served (FCFS) FIFO Characteristics Simple. Non-preemptive, once a process has the CPU, it runs to completion Fair in general, but short jobs must wait, important jobs also wait. More predictable, small variance in response time. Not useful in scheduling interactive users. Usually it is embedded in other schemes. Round Robin Scheduling (RR) Processes are dispatched FIFO, but are given a limited amount of CPU time slice (quantum). Time slice: A time interval during which a time-sharing system is processing one particular computer program. Also known as time quantum. If a process does not complete before its CPU time expires, the CPU is preempted and given to the next waiting process. 57 Ready list A C B CPU A Completion Preemption Quantum Size Should the quantum be large or small? When very large FIFO When very small Context switching overhead Consider the optimal value of the quantum that yields good response time. How? It should be large enough, so that the vast majority of interactive requests require less time than the duration of the quantum. Shortest-Job-First Scheduling (SJF) It is a non preemptive scheduling discipline in which the waiting job with the smallest estimated run-time-to-completion is run next. Reduces average waiting time over FIFO. Unpredictable, waiting time has a large variance. How to estimate how long a job will run? Users may mislead you. Shortest-Remaining-Time Scheduling (SRT) It is a preemptive counter part of SJF and is useful in time sharing. The process with the smallest estimated run-time-to-completion is run next, including new arrivals. its quantum before completion. 58 q= 8 q= 16 FCFS Ex: FCFS Process Arrival time P1 Burst time/ milliseconds(cpu execution time) 24 P2 3 0 P3 3 0 0 Gantt Chart p1 p2 0 24 p3 27 30 The average waiting time is (WT(P1) + WT(P2) + WT(P3))/3 = (0+24+27)/3 = 51/3 = 17 milliseconds Suppose the processes arrive in the order P2,P3,P1 p2 0 p3 3 p1 6 The average waiting time is now Ex: RR 30 (6+0+3)/3=3 milliseconds 59 q= 4 Process Burst time 24 P1 3 P2 3 P3 Gantt Chart P1 0 P2 4 P3 7 P1 10 P1 14 P1 18 P1 22 P1 26 30 The average waiting time is : (WT(P1) + WT(P2) + WT(P3))/3 = (6+4+7)/3 = 17/3 = 5.66 Ex: SJF Process Burst time Arrival time P1 6 0 P2 8 0 P3 7 0 P4 3 0 Gantt Chart P4 P1 P3 P2 0 3 9 16 24 The average waiting time is : (WT(P1) + WT(P2) + WT(P3) + WT(P4))/4 = (3+16+9+0)/4 =7 Ex: SRT 60 Process Arrival time Burst time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Gantt Chart P1 0 P2 1 P4 5 P1 10 The average waiting time is P1 P2 P3 P4 (10-1)+(1-1)+(17-2)+(5-3)/4 =26/4 =6.5 A non-preemptive SJF will result 7.75 Turn average turnaround time is P1 P2 P3 P4 (17+4+24+7)/4 = 13 Average response time is (0+0+15+2)/4 = 4.25 Priority Process Burst time Priority P1 10 3 P2 1 1 P3 2 3 P4 1 4 P5 5 2 Gantt Chart 61 P3 17 26 P2 P5 P1 0 1 6 The average waiting time is 8.2 milliseconds P3 16 P4 18 19 Disk Management Operation of Moving-Head Disk Storage. Why scheduling is necessary? Desirable Characteristics of scheduling polices. Basic Disk Scheduling Policies. Operation of Moving-Head Disk Storage Cylinder (Track) Read-Write head Sectors Spindle A platter or magnetic disk A disk access consists of three significant times: 1. Seek time: the time it takes to move the head to a specific cylinder. 2. Latency time (rotational time): the time it takes to rotate the disk from its current position to a position adjacent to the read-write head. 3. Transmission time: the time that needs the head to go over the data to read or write. Why scheduling is necessary? 62 The generated requests by processes in a multiprogramming system is much faster then the ability of the head to service them. Disk scheduling is the process of minimizing time spent seeking records. Desirable Characteristics of scheduling polices Throughput Mean response time Variance in response time (Predictability) Basic Disk Scheduling Policies FCFS no ordering on the queue. SSTF (Shortest-Seek-Time-First) Disk arm is positioned next at the request that minimizes arm movement. SCAN Operates like SSTF except that it chooses the request that results in the shortest seek distance in a preferred direction. N-stop Scan Disk arm sweeps back and forth as in scan, but all requests that arrive during a sweep in one direction are batched and reordered for optimum service during the return sweep. C-SCAN (Circular Scan) Disk arm moves unidirectional across the disk surface toward the inner track. When there are no more requests for service ahead of the arm, it jumps back to service the request nearest the outer track and proceeds inward again. Eshenbach scheme Disk arm movement is circular as in C-SCAN, but with several important exceptions. Every cylinder is serviced for exactly one full track of information whether or not there is a request for that cylinder. Requests are reordered for service within a cylinder to take advantage of rotational position. Example Consider for example, an ordered disk queue with requests involving tracks 98, 183, 37, 122, 14, 124, 65, and 67 start at track 53. FCFS Disk Scheduling 63 Queue= 98, 183, 37, 122, 14, 124, 65, 67 head starts at 53 0 14 37 53 65 67 98 122 124 183 199 122 124 183 199 SSTF Disk Scheduling Queue= 98, 183, 37, 122, 14, 124, 65, 67 head starts at 53 0 14 37 53 65 67 98 SCAN Disk Scheduling 64 Queue= 8, 183, 37, 122, 14, 124, 65, 67 head starts at 53 0 14 37 53 65 67 98 122 124 183 199 122 124 183 199 C-SCAN Disk Scheduling Queue= 8, 183, 37, 122, 14, 124, 65, 67 head starts at 53 0 14 37 53 65 67 98 C-LOOK Disk Scheduling 65 Queue= 8, 183, 37, 122, 14, 124, 65, 67 head starts at 53 0 14 37 53 65 67 98 122 124 183 199 Scheduling Criteria CPU utilization – keep the CPU as busy as possible Throughput – number of processes that complete their execution per time unit Turnaround time – the duration between job submission time and job completion time. 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 timesharing environment) Deadlines – cyclic RT-tasks must finish each cycle before its deadline Optimization Criteria Maximize CPU utilization Maximize throughput Minimize turnaround time Minimize waiting time Minimize response time 66