CPU Scheduling Basic Concepts Chapter 5: CPU Scheduling • • • • • • • Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation Objectives • To introduce CPU scheduling, which is the basis for multiprogrammed operating systems • To describe various CPU-scheduling algorithms • To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system Basic Concepts • Maximum CPU utilization obtained with multiprogramming • CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait • CPU burst distribution Histogram of CPU-burst Times CPU-I/O Burst Cycle • Process execution consists of a cycle of CPU execution and I/O wait • Processes alternate between these two states • Each scheduling decision is about which job to give to the CPU for use by its next CPU burst • With time slicing, job may be forced to give up CPU before finishing current CPU burst 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 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates • Scheduling under 1 and 4 is nonpreemptive • All other scheduling is preemptive Scheduling • Managing resource (e.g., CPU) • n processes – A schedule is a listing – Number of possible schedules if no interdependencies? • Environment – Number of processors involved – Whether preemption is allowed (priorities or time slicing) – Whether a process once active can become blocked (due to I/O, etc…) Scheduling • First the easy case – One processor, no preemption, no blocking – n/1 static scheduling problem • n processes • Know arrival times, processing times Scheduling • Given the following information about process Pi – Arrival Time ai – Execution Time ei – Due Time di • We can obtain – Completion (finish) Time fi – Turn-around Time ti = (fi – ai) – Waiting Time wi = (ti – ei) – Lateness Li = (fi – di) – Tardiness Ti = max(0, Li) Example Process pi P1 P2 P3 P4 P5 P6 15 25 5 20 25 30 P4 ti wi P1 65 50 P2 120 95 P3 50 45 P4 20 0 P5 45 20 P6 95 65 0 P5 20 P3 45 50 P1 P6 65 P2 95 120 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 • 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 How good is a schedule? Performance Measures • • • • • Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time First-Come, First-Served (FCFS) Scheduling • First-Come, First-Served (FCFS) – Also “First In, First Out” (FIFO) or “Run until done” • In early systems, FCFS meant one program scheduled until done (including I/O) • Now, means keep CPU until process blocks First-Come, First-Served (FCFS) Scheduling Process pi P1 P2 P3 24 3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 . Draw the Gantt Chart for the schedule and compute the average waiting time: 3 P2 0 P3 3 P1 6 30 First-Come, First-Served (FCFS) Scheduling Process pi P1 P2 P3 24 3 3 • Suppose that the processes arrive in the order: P2, P3, P1 . Draw the Gantt Chart for the schedule and compute the average waiting time: 17 P1 0 P2 24 P3 27 30 First-Come, First-Served (FCFS) Scheduling • There is a convoy effect as all the other processes wait for the one big process to get off the CPU – Average waiting time for FCFS is often quite long – What about average turnaround time? • Pros and cons – Simple (+) – Short jobs get stuck behind long ones (-) • Analogy: getting milk at a supermarket and waiting to pay, always stuck behind carts full of items. Upside: get to read about space aliens! Example of (Shortest Job First) SJF Process Arrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 • SJF scheduling chart P4 0 P3 P1 3 9 P2 16 24 • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 SJF Scheduling example Process pi P1 P2 P3 P4 P5 P6 15 25 5 20 18 30 • Sometimes called Shortest Processing Time (SPT) First • Draw the Gantt Chart for a SPT schedule and compute the average waiting time and average turnaround time (assume all processes arrive at time 0) Round Robin (RR) • Each process gets a small unit of CPU time (time quantum), 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 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 time units. • Performance – q large FIFO – q small q must be large with respect to context switch, otherwise overhead is too high