Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5: CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms.

Similar presentations


Presentation on theme: "1 Chapter 5: CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms."— Presentation transcript:

1 1 Chapter 5: CPU Scheduling

2 2 Basic Concepts Scheduling Criteria Scheduling Algorithms

3 3 Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms

4 4 Basic Concepts Maximum CPU utilization obtained with multiprogramming Process execution consists of several cycles of CPU execution and I/O wait CPU executionI/O wait CPU executionI/O wait terminated time

5 5 Alternating Sequence of CPU and I/O Bursts

6 6 Histogram of CPU-burst Lengths

7 7 CPU Scheduler Selects a process from the processes in the ready queue, and allocates the CPU to that process 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 Scheduling under 2 and 3 is preemptive

8 8 Dispatcher Dispatcher is the module that gives control of the CPU to the process selected by the CPU scheduler (= short-term scheduler); this involves: switching context switching to user mode jumping to the proper location in the user program to continue that program Dispatch latency – time it takes for the dispatcher to stop one process and start another running

9 9 Scheduling Criteria  Quantities related to use of CPU or to process CPU utilization – degree of use (by applications) of the CPU, can range from 0 to 100 percent Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process: the interval from submission to completion 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)

10 10 Scheduling Algorithm Optimization Criteria All systems Fairness – giving each process a fair share of the CPU Balance – keeping all parts of the system busy Batch systems Maximize CPU utilization Max throughput Minimize turnaround time Min waiting time Interactive systems Min response time

11 11 First-Come, First-Served (FCFS) Scheduling Process Burst 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: (0 + 24 + 27)/3 = 17 P1P1 P2P2 P3P3 2427300

12 12 FCFS Scheduling (Cont) 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: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process P1P1 P3P3 P2P2 63300

13 13 Shortest-Job-First (SJF) Scheduling 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 with SJF scheduling is knowing the length of the next CPU burst

14 14 Example of SJF Process Burst Time P 1 6 P 2 8 P 3 7 P 4 3 Gantt chart Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 P4P4 P3P3 P1P1 3 16 0 9 P2P2 24

15 15 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

16 16 Prediction of the Length of the Next CPU Burst

17 17 Examples of Exponential Averaging Choose  = 0  n+1 =  n Recent history does not count Choose  = 1  n+1 = t n Only the actual last CPU burst counts If we expand the formula, we get:  n+1 =  t n + (1   )  t n  1 + … + (1   ) j  t n  j + … + (1   ) n+1  0 Since both  and (1   ) are less than or equal to 1, each successive term has less weight than its predecessor

18 18 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). Priority scheduling can be preemptive nonpreemptive SJF is a priority scheduling, where the predicted next CPU burst time determines the priority Problem  Starvation – low priority processes may never execute Solution  Aging – as time progresses increase the priority of the process

19 19 Round Robin (RR) Scheduling 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. Good for interactive systems If there are n processes in the ready queue and the time quantum is q, then no process waits more than (n  1)q time units (if the running process is not counted). head end n  1 PCBs Ready queue

20 20 Example of RR with Time Quantum = 4 Process Burst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: Typically, higher average turnaround than SJF, but better response time P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630

21 21 Time Quantum and Context Switch Time Performance q large  FIFO scheduling. How large is “large”? q small  q must be large with respect to context switch, otherwise overhead is too high

22 22 Turnaround Time Varies with the Time Quantum

23 23 Multilevel Queue Ready queue is partitioned into separate queues, e.g. foreground (interactive) queue background (batch) queue Each queue has its own scheduling algorithm foreground queue – RR background queue – FCFS Scheduling must be done between the queues. Some possibilities: Fixed-priority preemptive scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slicing – each queue gets a certain amount of CPU time which it can schedule among its processes; i.e., 80% to foreground in RR, 20% to background in FCFS

24 24 Scheduling between Queues Fixed-priority preemptive scheduling Each queue has “absolute” priority over lower-priority queues. For example, no process in the batch queue could run unless all queues above it were all empty. Time slice

25 25 Multilevel Feedback Queue Multilevel queue: processes are permanently assigned to a queue Multilevel feedback queue: 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

26 26 Example of Multilevel Feedback Queue Three queues: Q 0, using RR with time quantum 8 milliseconds Q 1, using RR time quantum 16 milliseconds Q 2, using 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 millisecs. If it still does not complete, it is preempted and moved to queue Q 2.

27 27 End of Chapter 5


Download ppt "1 Chapter 5: CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms."

Similar presentations


Ads by Google