Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: CPU Scheduling

Similar presentations


Presentation on theme: "Chapter 6: CPU Scheduling"— Presentation transcript:

1 Chapter 6: CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms

2 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

3 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

4 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.

5 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 5 2 1 3

6 Preemptive – Nonpreemptive Scheduling
Interrupt: Scheduler picks another process Scheduler dispatch: Scheduler picks this process to execute 4 5 2 1 3 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.

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

8 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.

9 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.

10 Performance Criteria It is desirable that we, Maximize CPU utilization
Maximize throughput Minimize turnaround time Minimize waiting time Minimize response time

11 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

12 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.

13 Example: FCFS Scheduling
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds (ms). Process CPU 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: ( )/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. P1 P2 P3 24 27 30

14 FCFS Scheduling (Cont.)
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: ( )/3 = 3ms tat(P1)= 30ms; tat(P2) = 3ms; tat(P3) = 6ms Much smaller wait time than previous case. P1 P3 P2 6 3 30

15 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 as Shortest-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.

16 Example of Non-Preemptive SJF
Process Arrival Time CPU 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 = ( )/4 = 4ms P1 P3 P2 7 16 P4 8 12

17 Example of SJF ProcessArri Arrival Time l CPU Burst Time(ms) P1 0 6
SJF scheduling chart Average waiting time = ( ) / 4 = 7 ms.

18 Example of Preemptive SJF (SRTF)
Process Arrival Time CPU 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 = ( )/4 = 3ms P1 P3 P2 4 2 11 P4 5 7 16

19 Example of Shortest-remaining-time-first
Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT CPU Burst Time(ms) P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 ms.

20 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 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.

21 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.

22 Example: RR with Time Quantum = 20
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. Process Burst Time(ms) P1 53 P2 17 P3 68 P4 24 The Gantt chart is: P1 P2 P3 P4 20 37 57 77 97 117 121 134 154 162

23 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: W (A) = =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 = ( ) /4 = 142/4 = 35.5 ms A B C D

24 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 B C D A B C D idle A Wait (A) = = 12 ms Wait (B) = = 56 ms Wait (C) = = 46 ms Wait (D) = 36 ms Average waiting time = 150/4 = 37.5 ms

25 Example of RR with Time Quantum = 4
Process Burst 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

26 Time Quantum and Context Switch Time
Figure showing how smaller time quantum increases context switches.

27 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

28 Example - Priority Scheduling
Process Arrival time CPU burst time Priority A 100 3 B 10 1 C 300 D 60 5 E 80 150 4 5 highest 1 lowest Non-preemptive priority Algorithm: D A E C B 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): D A E C B W(A) = 210 ms, W(B) = 610 ms, W(C) = 310 ms, W(D)=0ms, Wait (E) = 0 ms

29 Example - Priority Scheduling
ProcessA arri Burst TimeT Priority P1 10 3 P (highest) P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = 8.2 msec

30 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.

31 Multilevel Queue Scheduling
(Priority 5) (Priority 1)

32 Multilevel Queue Scheduling
Process Arrival time CPU burst time (ms) Priority A 10 2 B 3 7 1 C 4 6 D 12 5 E 18 8 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. A B D C E W (A) = 24ms W (B) = 0ms W (C) = 26ms W (D) = 0ms W (E) = 0ms W (F) = 10ms

33 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.

34 Multilevel Feedback Queue Scheduling
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.

35 Multilevel Feedback Queue Scheduling
Process Arrival time CPU burst time I/O A 5 6 B 3 4 2 C D 7 E 14 Assume Queue 1 with Quantum =4 ms and Queue 2 with Quantum = 7ms, both use Round Robin. Queue_1 : ABCDBCEED Queue_2 : AD A B C D E idle CPU I/O idle B C E A D

36 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?

37 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 ( )/5=28ms: Non-preemptive SJF is ( )/5=13ms: RR is ( )/5=23ms:


Download ppt "Chapter 6: CPU Scheduling"

Similar presentations


Ads by Google