370 likes | 460 Views
Chapter 6: CPU Scheduling. Basic Concepts Scheduling Criteria Scheduling Algorithms. Objectives. To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms
E N D
Chapter 6: CPU Scheduling • Basic Concepts • Scheduling Criteria • Scheduling Algorithms
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 • Scheduling is a fundamental operating system function. • Maximum CPU utilization obtained with multiprogramming. • The algorithm, the scheduler uses is called scheduling algorithm. • Several processes are kept in memory at one time. • CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait • CPU burst followed by I/O burst • CPU burst distribution is of main concern
CPU-Scheduling(with multiprograming) • In a single processor system, only one process can run at a time. Others must wait until CPU is free and can be rescheduled. • Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed (the records in the queues are generally PCBs of the pocesses). • The short term scheduler (or CPU scheduler- a part of OS) selects a process to get the processor (CPU) among the processes which are already in memory. (i.e. from the ready queue). • Processor scheduling algorithms try to answer the following crucial question. • Which process in the ready queue will get the processor? • In order to answer this, one should consider the relative importance of several performance criteria.
CPU Scheduler • CPU scheduler selects from 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. • 5.A new process joins the ready queue 4 2 5 1 3
Preemptive – Nonpreemptive Scheduling Interrupt: Scheduler picks another process Scheduler dispatch: Scheduler picks this process to execute 4 2 5 • In (1) and (4), a new process must be selected from the ready queue. • In (2), (3) and (5), previously running process or a new process may be selected. • Scheduling algorithms that act only in circumstances (1) and (4) are called nonpreemptive(or cooperative).Once CPU has been allocated to a process, that process keeps the CPU until it releases the CPU (either by termination or by requesting I/O). This scheduling method was used by Microsoft Windows 3.x. • Otherwise it is preemptive. Windows 95 and all subsequent versions of Windows operating systems have used preemptive scheduling. 1 3
Preemptive – Nonpreemptive Scheduling • A scheduling algorithm which acts on all circumstances is called preemptive. (i.e. such an algorithm can select a new process in circumstances 2, 3 and 5). • The CPU is allocated to the highest-priority process among all ready processes. The scheduler is called each time a process enters the ready state. • Advantages of non-preemptive scheduling algorithms: • They cannot lead the system to a race condition. • They are simple. • Disadvantage of non-preemptive scheduling algorithms: • They do not allow real multiprogramming. • Advantage of preemptive scheduling algorithms: • They allow real multiprogramming. • Disadvantages of preemptive scheduling algorithms: • They can lead the system to a race condition.
Dispatcher • Dispatcher module(invoked during every process switch) 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 • CPU utilization - keep the CPU as busy as possible. It is defined as: • (processor busy time) / (processor busy time + processor idle time) • Throughput – # of processes that complete their execution per time unit. • Turnaround time – The interval from the time of submission to the time of completion of a process. • Waiting time – amount of time a process has been waiting in the ready queue (waiting for I/O device is not counted) • Response time – amount of time it takes from when a request was submitted until the first response is produced, not output.
Performance Criteria • It is desirable that we, • Maximize CPU utilization • Maximize throughput • Minimize turnaround time • Minimize waiting time • Minimize response time
CPU Scheduling Algorithms First- Come, First-Served (FCFS) - Non preemptive Shortest-Job-First (SJF) - Non preemptive Shortest-Remaining-Time-First (SRTF) - Preemptive Priority Scheduling - Non preemptive, Preemptive Round Robin (RR) - Preemptive Priority – RR (Multilevel Queue Scheduling, Multilevel Feedback Queue Scheduling) Operating System Concepts
First-Come, First-Served (FCFS) Scheduling • This is the simplest one. In this algorithm the set of ready processes is managed as FIFO (first-in-first-out) Queue. The processes are serviced by the CPU until completion in the order of their entering in the FIFO queue. • Once allocated the CPU, each process keeps it until releasing either due to termination or requesting I/O. • Average waiting time under FCFS policy is often quite long. • FCFS scheduling algorithm is nonpreemptive.
Example: FCFS Scheduling P1 P2 P3 0 24 27 30 Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds (ms). ProcessCPU Burst Time (ms) P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: W(P1) = 0; W(P2) = 24ms; W(P3 )= 27ms Average waiting time: (0 + 24 + 27)/3 = 17ms CPU utilization = (30/(30+0))x100 = 100 % Gantt Chart is a bar chart that illustrates a particular schedule, including the start and finish times of each of the participating processes.
FCFS Scheduling (Cont.) P2 P3 P1 0 3 6 30 Suppose that the processes arrive in the order P2 , P3 , P1 . • The Gantt chart for the schedule is: • W(P1)= 6ms;W(P2) = 0; W(P3)= 3ms • Average waiting time: (6 + 0 + 3)/3 = 3ms • tat(P1)= 30ms;tat(P2) = 3ms;tat(P3)= 6ms • Much smaller wait time than previous case.
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 next CPU burst time. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie. • Two versions: • nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst. • preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, then preempt the current one. This scheme is known asShortest-Remaining-Time-First (SRTF). • SJF is optimal – gives minimum average waiting time for a given set of processes. • The main problem with the SJF algorithm is to know for each process the next CPU burst time!!! However some techniques exist to predict this next CPU burst time (could ask to the user). • Another problem is that starvation of long processes may occur.
Example of Non-Preemptive SJF P1 P3 P2 P4 0 7 8 12 16 ProcessArrival TimeCPU Burst Time(ms) P1 0 7 P2 2 4 P3 4 1 P4 5 4 • SJF (non-preemptive) • W(P1) = 0; W(P2) = 8-2 = 6ms; • W(P3 )= 7-4 = 3ms ; W(P4 )= 12-5 = 7ms • Average waiting time = (0 + 6 + 3 + 7)/4 = 4ms
Example of SJF ProcessArri Arrival Time l CPU Burst Time(ms) P10 6 P2 0 8 P30 7 P4 0 3 • SJF scheduling chart • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 ms.
Example of Preemptive SJF (SRTF) P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 ProcessArrival TimeCPU Burst Time(ms) P1 0 7 P2 2 4 P3 4 1 P4 5 4 • SJF (preemptive) • Process P1 is started at time 0, since it is the only process in the queue. Process P2 arrives at time 2. The remaining time for process P1 (5ms) is larger than the time required by process P2 (4ms), so process P1 is preempted, and process P2 is scheduled. • W(P1) = (0-0)+(11-2)=9ms; W(P2) = (2-2)+(5-4)=1ms; • W(P3 )= 4-4=0; W(P4 )= 7-5=2ms • Average waiting time = (9 + 1 + 0 +2)/4 = 3ms
Example of Shortest-remaining-time-first • Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarriArrival TimeTCPU Burst Time(ms) P10 8 P2 1 4 P32 9 P43 5 • Preemptive SJF Gantt Chart • Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 ms.
Round Robin (RR) (preemptive) • This scheduling algorithm is designed specially for time sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes. • 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 very large FIFO • q small q must be large with respect to context switch, otherwise overhead is too high.
Round Robin (continue) • The scheduler allocates the CPU to each process in the ready queue for a time interval of up to 1 time quantum in FIFO (circular) fashion. If the process still running at the end of the quantum; it will be preempted from the CPU. • A context switch will be executed, and the process will be put at the tail of the ready queue. Then the scheduler will select the next process in the ready queue. However, if the process has blocked or finished before the quantum has elapsed, the scheduler will then proceed with the next process in the ready queue.
Example: RR with Time Quantum = 20 P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Consider the following set of processes that arrive at time 0 in the order P1, P2, P3, P4, with the length of the CPU burst given in milliseconds. ProcessBurst Time(ms) P1 53 P2 17 P3 68 P4 24 • The Gantt chart is:
Example: RR with Quantum=10ms • Consider the following set of process that arrive at time 0 in the order A,B,C,D with the following given CPU burst time. Find the average waiting time with RR of quantum : 10 ms Process CPU burst time (ms) A 20 B 40 C 14 D 6 Gantt Chart: 0 10 20 30 36 46 56 60 70 80 W (A) = 36-10 =26 ms W (B) = (10-0) + (46-20) + (60-56)= 40 ms W (C) = (20-0) + (56-30) = 46 ms W (D) = 30-0 = 30 ms Average waiting time = (26+40+46+30) /4 = 142/4 = 35.5 ms
Example: RR with Quantum = 10ms • Consider the following set of process that arrive at time 0 in the order A,B,C,D with the following given CPU burst time. Find the average waiting time with RR of quantum = 10 ms and context switch time = 2ms Process CPU burst time I/O CPU burst time A 10 40 10 B 40 - - C 14 - - D 6 - - 0 10 12 22 24 34 36 42 44 54 56 60 62 72 74 84 86 96 idle A 0 10 50 Wait (A) = 62-50 = 12 ms Wait (B) = 12+22+20+2 = 56 ms Wait (C) = 24 + 22 = 46 ms Wait (D) = 36 ms Average waiting time = 150/4 = 37.5 ms
Example of RR with Time Quantum = 4 ProcessBurst 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 microsecond
Time Quantum and Context Switch Time Figure showing how smaller time quantum increases context switches.
Priority Scheduling • A priority number (integer) is associated with each process • The CPU is allocated to the process with the highest priority ((smallest or largest integer value may be defined as the highest priority) • Preemptive • Nonpreemptive • SJF is priority scheduling where priority is the inverse of predicted next CPU burst time • Problem Starvation– low priority processes may never execute • Solution Aging– as time progresses increase the priority of the process
Example - Priority Scheduling 5 highest 1 lowest Non-preemptive priority Algorithm: 0 60 160 310 610 620 W (A) = 60ms, W (B) = 610ms, W (C) = 310ms, W (D) = 0 ms, W (E) = 80ms Preemptive priority Algorithm (if a job come with high priority there is a switching): 0 60 80 230 310 610 620 W(A) = 210 ms, W(B) = 610 ms, W(C) = 310 ms, W(D)=0ms, Wait (E) = 0 ms
Example - Priority Scheduling ProcessA arri Burst TimeTPriority P1 10 3 P2 11 (highest) P32 4 P41 5 P5 5 2 • Priority scheduling Gantt Chart • Average waiting time = 8.2 msec
Multilevel Queue Scheduling • Another class of scheduling algorithms has been created for situations in which processes are easily classified into different groups(classes). • Among classes a priority scheduling algorithm is used, inside a class RR is used. • A ready queue is used for each class. • The following shows a system with five priority classes where RR is used in each class. As long as there are runnable processes in priority class 5, just run each one for one quantum (i.e. round robin fashion), and never bother with lower priority classes. If priority class 5 in empty , then run the class 4 processes in round robin fashion , and so on. If priorities are not adjusted from time to time, lower priority classes may all starve. • While a class 3 process is running if a highest queue process arrives, class 3 process would be preempted. • Time quantum value can be different for classes. • Each queue may have its own scheduling algorithm.
Multilevel Queue Scheduling (Priority 5) (Priority 1)
Multilevel Queue Scheduling Assume that Quantum=4 ms for Queue 1 and Quantum =3 ms for Queue 2. Assume that Queue 1 has higher priority when compared to Queue 2. 0 3 7 10 12 16 17 18 22 26 29 32 34 36 W (A) = 24ms W (B) = 0ms W (C) = 26ms W (D) = 0ms W (E) = 0ms W (F) = 10ms
Multilevel Feedback Queue Scheduling • Normally, when the multilevel queue scheduling algorithm is used, processes are permanently assigned to a queue when they enter the system. • The multilevel feedback queue scheduling algorithm allows a process to move between queues. • The scheduler first executes all processes in Queue 1. Only when Queue 1 is empty will it execute processes in Queue 2. Similarly, processes in Queue 3 will be executed only if Queues 1 and 2 are empty. A process that arrives for Queue 1 will preempt a process in Queue 2. • A process entering the ready queue is put in Queue 1. If it does not finish within one quantum, it is moved to the tail of Queue 2. If it does not complete there in one quantum, it is then preempted and put into Queue 3.
Multilevel Feedback Queue Scheduling Queue 1 Queue 2 The figure above shows an example for multilevel feedback queue where Queue 1 uses RR with Quantum = 8ms, Queue 2 uses RR with Quantum = 16 and Queue 3 uses FCFS.
Multilevel Feedback Queue Scheduling Assume Queue 1 with Quantum =4 ms and Queue 2 with Quantum = 7ms, both use Round Robin. Queue_1 : ABCDBCEED Queue_2 : AD CPU 0 4 8 10 14 17 21 24 25 26 30 34 41 I/O 0 8 10 13 24 26 32 34
Algorithm Evaluation • How to select CPU-scheduling algorithm for an OS? • Determine criteria, then evaluate algorithms • Takes a particular predetermined workload and defines the performance of each algorithm for that workload • Consider 5 processes arriving at time 0, in the order given, with the length of the CPU burst given in ms. • Consider the FCFS, SJF and RR(quantum = 10 ms) scheduling algorithms for this set of processes. Which algorithm would give the minimum average waiting time?
Algorithm Evaluation • For each algorithm, calculate minimum average waiting time • Simple and fast, but requires exact numbers for input, applies only to those inputs • FCFS is (0+10+39+42+49)/5=28ms: • Non-preemptive SJF is (10+32+0+3+20)/5=13ms: • RR is (0+32+20+23+40)/5=23ms: