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

2 Chapter 6: CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Real-Time CPU Scheduling Operating Systems Examples Algorithm Evaluation

3 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 To examine the scheduling algorithms of several operating systems

4 Basic Concepts Maximum CPU utilization obtained with multiprogramming
Process execution begins with CPU burst followed by I/O burst, which is followed by another CPU burst, then another I/O burst, and so on. Eventually, the final CPU burst ends with a system request to terminate execution

5 CPU Scheduler Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them nonpreemptive Scheduling Under nonpreemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. Dispatch latency – time it takes for the dispatcher to stop one process and start another running

6 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. Turnaround time is the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O. Waiting time – amount of time a process has been waiting in the ready queue. Waiting time is the sum of the periods spent waiting in the ready queue. Response time – amount of time it takes from when a request was submitted until the first response is produced

7 Scheduling Algorithm Optimization Criteria
Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

8 First- Come, First-Served (FCFS) Scheduling Algorithm
the process that requests the CPU first is allocated the CPU first. The implementation of the FCFS policy is easily managed with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. On the negative side, the average waiting time under the FCFS policy is often quite long.

9 First- Come, First-Served (FCFS) Scheduling Algorithm
Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: ( )/3 = 17

10 FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt chart for the schedule is: Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: ( )/3 = 3 Much better than previous case

11 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 is knowing the length of the next CPU request The SJF algorithm can be either preemptive or nonpreemptive. when a new process arrives at the ready queue while a previous process is still executing. The next CPU burst of the newly arrived process may be shorter than what is left of the currently executing process. A preemptive SJF algorithm will preempt the currently executing process, whereas a nonpreemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive version called shortest-remaining-time-first

12 Example of SJF (nonpreemptive )
ProcessArriva l Time Burst Time P P P P SJF scheduling chart Average waiting time = ( ) / 4 = 7

13 Example of Shortest-remaining-time-first (preemptive )
Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT Burst Time 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 msec

14 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) 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

15 Example of Priority Scheduling(Nonpreemptive)
ProcessA arri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = 8.2 msec

16 Example of Priority Scheduling(preemptive)
ProcessA arri Burst TimeT Priority Arrival TimeT P P P P P Priority scheduling Gantt Chart Average waiting time =

17 Round Robin (RR) The round-robin (RR) scheduling algorithm is designed especially for timesharing systems. Each process gets a small unit of CPU time (time quantum q). After this time has elapsed, the process is preempted and added to the end of the ready queue. Performance q large  FIFO q small  q must be large with respect to context switch, otherwise overhead is too high

18 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 usec

19 Time Quantum and Context Switch Time

20 Multilevel Queue Ready queue is partitioned into separate queues,
Each queue has its own scheduling algorithm: Scheduling must be done between the queues: Fixed priority scheduling; ( serve all from queue with highest priority then the second highest priority and so on). Possibility of starvation. Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; for example 80% to first queue in RR 20% to second queue in FCFS

21 Multilevel Queue Scheduling
Let’s look at an example of a multilevel queue scheduling algorithm with five queues, listed below in order of priority: Each queue has absolute priority over lower-priority queues. No process in the batch queue, for example, could run unless the queues for system processes, interactive processes, and interactive editing processes were all empty. If an interactive editing process entered the ready queue while a batch process was running, the batch process would be preempted.

22 Multilevel Feedback Queue
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, in contrast, allows a process to move between queues. A 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 method used to determine which queue a process will enter when that process needs service

23 Example of Multilevel Feedback Queue
Three queues: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS Scheduling A new job enters queue Q0 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 Q1 At Q1 job is again served FCFS and receives 16 additional milliseconds If it still does not complete, it is preempted and moved to queue Q2

24 Example of Multilevel Feedback Queue
For previous Example of Multilevel Feedback Queue consider the following processes: Multilevel Feedback Queue scheduling Gantt Chart Average waiting time =

25 Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available. Asymmetric multiprocessing – One approach to CPU scheduling in a multiprocessor system has all scheduling decisions, I/O processing, and other system activities handled by a single processor—the master server. The other processors execute only user code. It is simple because only one processor accesses the system data structures, reducing the need for data sharing. Symmetric multiprocessing (SMP) – A second approach to CPU scheduling in a multiprocessor system where each processor is self-scheduling. All processes may be in a common ready queue, or each processor may have its own private queue of ready processes. Regardless, scheduling proceeds by having the scheduler for each processor examine the ready queue and select a process to execute.

26 Issues Concerning SMP Systems
Processor affinity – Because of the high cost of invalidating and repopulating caches, most SMP systems try to avoid migration of processes from one processor to another and instead attempt to keep a process running on the same processor. This is known as Processor affinity Load Balancing Load balancing attempts to keep the workload evenly distributed across all processors in an SMP system. load balancing is typically necessary only on systems where each processor has its own private queue

27 Real-Time CPU Scheduling
Soft real-time systems –provide no guarantee as to when a critical real-time process will be scheduled. They guarantee only that the process will be given preference over noncritical processes. Hard real-time systems – have stricter requirements. A task must be serviced by its deadline; service after the deadline has expired is the same as no service at all. In this section, we explore several issues related to process scheduling in both soft and hard real-time operating systems.

28 Algorithm Evaluation How to select CPU-scheduling algorithm for an OS?
Determine criteria, then evaluate algorithms. criteria are often defined in terms of: CPU utilization response time or throughput.

29 Deterministic modeling
Deterministic modeling This method takes a particular predetermined workload and defines the performance of each algorithm for that workload. For example, assume that we have the workload shown below. All five processes arrive at time 0, in the order given, with the length of the CPU burst given in milliseconds: Consider the FCFS, SJF, and RR (quantum = 10 milliseconds) scheduling algorithms for this set of processes. Which algorithm would give the minimum average waiting time?

30 Deterministic Evaluation
For the FCFS algorithm, we would execute the processes as The waiting time is 0 milliseconds for process P1, 10 milliseconds for process P2, 39 milliseconds for process P3, 42 milliseconds for process P4, and 49 milliseconds for process P5. Thus, the average waiting time is ( )/5 = 28 milliseconds. With nonpreemptive SJF scheduling, we execute the processes as: The waiting time is 10 milliseconds for process P1, 32 milliseconds for process P2, 0 milliseconds for process P3, 3 milliseconds for process P4, and 20 milliseconds for process P5. Thus, the average waiting time is ( )/5 = 13 milliseconds.

31 Deterministic Evaluation
With the RR algorithm, we execute the processes as: The waiting time is 0 milliseconds for process P1, 32 milliseconds for process P2, 20 milliseconds for process P3, 23 milliseconds for process P4, and 40 milliseconds for process P5. Thus, the average waiting time is ( )/5 = 23 milliseconds. We can see that, in this case, the average waiting time obtained with the SJF policy is less than half that obtained with FCFS scheduling; the RR algorithm gives us an intermediate value. Deterministic modeling is simple and fast. It gives us exact numbers, allowing us to compare the algorithms. However, it requires exact numbers for input, and its answers apply only to those cases.

32 Queueing Models The arrival of processes, and CPU and I/O bursts approximated or simply estimated The computer system is described as a network of servers. Each server has a queue of waiting processes. The CPU is a server with its ready queue, as is the I/O system with its device queues. Knowing arrival rates and service rates, we can compute utilization, average queue length, average wait time, and so on. This area of study is called queueing-network analysis.

33 Little’s Formula n = average queue length
W = average waiting time in queue λ = average arrival rate into queue Little’s law – in steady state, processes leaving queue must equal processes arriving, thus: n = λ x W Valid for any scheduling algorithm and arrival distribution For example, if on average 7 processes arrive per second (λ = 7 ) , and normally 14 processes in queue (n = 14) , then average wait time per process = 2 seconds (W = 2 )

34 Simulations To get a more accurate evaluation of scheduling algorithms, we can use simulations. Running simulations involves programming a model of the computer system. The simulator has a variable representing a clock. As this variable’s value is increased, the simulator modifies the system state to reflect the activities of the devices, the processes, and the scheduler. An accurate way to generate input data for simulator We create a trace tape by monitoring the real system and recording the sequence of actual events (Figure 6.25). We then use this sequence to drive the simulation. Trace tapes provide an excellent way to compare two algorithms on exactly the same set of real inputs. This method can produce accurate results for its inputs.

35 Evaluation of CPU Schedulers by Simulation

36 Implementation Even simulations have limited accuracy.
The only completely accurate way to evaluate a scheduling algorithm is to code it up, put it in the operating system, and see how it works. High cost,(coding the algorithm and modifying the operating system to support it) Environments vary(the environment in which the algorithm is used will change as a result of the performance of the scheduler)

37 End of Chapter 6


Download ppt "Chapter 6: CPU Scheduling"

Similar presentations


Ads by Google