Download presentation
Presentation is loading. Please wait.
Published byJoella Sharp Modified over 8 years ago
1
5.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 6: CPU Scheduling Overview: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Operating Systems Examples
2
5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Basic Concepts To Maximize CPU utilization, multiprogramming is used CPU–I/O Burst Cycle – Process execution consists of an alternating sequence of CPU executions and I/O waits CPU burst distribution
3
5.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Alternating Sequence of CPU And I/O Bursts of a Process
4
5.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Histogram of CPU-burst Times
5
5.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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 non-preemptive All other scheduling is preemptive
6
5.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Dispatcher Dispatcher module 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
7
5.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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 taken 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, not output (for time-sharing environment)
8
5.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Optimization Criteria A CPU scheduling algorithm should strive for Maximizing CPU utilization Maximizing throughput Minimizing turnaround time Minimizing waiting time Minimizing response time Making the users happy
9
5.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts First-Come, First-Served (FCFS) Scheduling ProcessBurst 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
10
5.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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
11
5.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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 CPU burst time. Two schemes: nonpreemptive – once CPU is 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 currently executing process, preempt. This scheme is known as the Shortest-Remaining-Time-First (SRTF). SJF is optimal – gives minimum average waiting time for a given set of processes. It is not possible to implement this algorithm even though it is optimal because there is no way of knowing the next CPU burst.
12
5.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts ProcessArrival TimeBurst Time P 1 0.07 P 2 2.04 P 3 4.01 P 4 5.04 SJF (non-preemptive) Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Example of Non-Preemptive SJF P1P1 P3P3 P2P2 73160 P4P4 812
13
5.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Example of Preemptive SJF ProcessArrival TimeBurst Time P 1 0.07 P 2 2.04 P 3 4.01 P 4 5.04 SJF (preemptive) Average waiting time = (9 + 1 + 0 +2)/4 = 3 P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16
14
5.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Determining Length of Next CPU Burst Can only estimate the length of the next CPU burst Can be done by using the length of previous CPU bursts, using exponential averaging
15
5.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Prediction of the Length of the Next CPU Burst (α =1/2)
16
5.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Examples of Exponential Averaging =0 n+1 = n Recent history does not count =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 t 0 Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor
17
5.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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). Under Linux, users can set priority to processes using the nice command. Preemptive Nonpreemptive SJF is a priority scheduling algorithm where priority is the predicted next CPU burst time Problem with priority scheduling: Starvation – low priority processes may never execute A Solution to this problem: Aging – as time progresses, increase the priority of the process
18
5.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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 How large q should be: q must be large with respect to context switch, otherwise overhead is too high
19
5.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Example of RR with Time Quantum = 20 ProcessBurst Time P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: Typically, higher average turnaround than SJF, but better response time P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162
20
5.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Time Quantum and Context Switch Time
21
5.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Turnaround Time Varies With The Time Quantum
22
5.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multilevel Queue Ready queue is partitioned into separate queues: foreground (interactive) background (batch) Each queue has its own scheduling algorithm foreground – RR background – FCFS Scheduling must be done between the queues Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; e.g., 80% to foreground in RR; 20% to background in FCFS
23
5.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multilevel Queue Scheduling
24
5.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multilevel Feedback Queue 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
25
5.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Example of Multilevel Feedback Queue Three queues: Q 0 – RR with time quantum 8 milliseconds Q 1 – RR time quantum 16 milliseconds Q 2 – 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 milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.
26
5.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multilevel Feedback Queues
27
5.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multiple-Processor Scheduling CPU scheduling is more complex when multiple CPUs are available Some approaches for multiprocessor scheduling Asymmetric multiprocessing: only one processor accesses the system data structures, reducing the need for data sharing; scheduling decisions are handled by this processor. Symmetric multiprocessing (SMP): Each processor is self scheduling. All processes may be in a common ready queue or each processor may have its own ready queue Each processor has its own scheduler. Since multiple processors are trying to access and modify common data structures (such as ready queue, etc), schedulers must be carefully programmed. Virtually all operating systems including Windows, Linux, and Mac OS X support SMP.
28
5.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Some issues related to SMP Processor affinity: When a process running on a processor is migrated to another processor, the contents of the cache memory for that processor must be invalidated and repopulated to the cache of the processor to which the process is migrated. This involves large overhead. To prevent this overhead, most SMP systems try to avoid migration of processes from one processor to another processor and instead keep a process running on the same processor. This is called processor affinity. Soft affinity: Operating system has a policy of attempting to keep a process running on the same processor without guaranteeing to do so. Linux implements soft affinity. Hard affinity: Some operating systems allow a process to specify the set of processors on which it may run. Linux provides support for hard affinity through the system call sched_setaffinity().
29
5.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Some issues related to SMP … Load balancing: This attempts to keep workload evenly distributed across all processors. This is especially needed in systems where each processor has its own queue which is the case in most contemporary Operating systems. Note that load balancing counteracts the benefits of processor affinity. So, this is not an easy problem to solve. Two approaches for load balancing: Push migration: a specific task periodically checks the load on each processor and balances the load by moving (or pushing) processes from overloaded processors to lightly loaded processors. Pull migration: Idle processor pulls a waiting task from a busy processor. Linux scheduler implements both Push and Pull migration. Multicore processors: Multiple processor cores are placed on a single physical chip. Each Core appears to the OS as a separate processor. SMP systems that use multicore processors are faster and consume less power than systems in which each processor is on a separate physical chip.
30
5.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Some issues related to SMP … Scheduling multicore processors: When a processor accesses memory, it waits for the data to become available for reasons such as cache miss – called memory stall. To solve the memory stall problem, many recent hardware designs have implemented multithreaded processor cores, in which two or more hardware threads are assigned to each core. This way the hardware can switch to another thread when a currently executing thread is waiting for memory. Coarse-grained multithreading: when a memory stall occurs during the execution of a thread on a processor, processor switches to another thread. This type of thread switching is high, because the instruction pipeline needs to be flushed before the other thread can start execution on the processor core. Fine-grained (or interleaved) multithreading: Thread switching occurs at the boundary of an instruction cycle. The architectural design includes logic for thread switching which results in further lowering switching overhead.
31
5.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Some issues related to SMP … Scheduling Multicore processors (contd.): So, multithreaded multicore scheduling requires scheduling at two different levels 1. at the Operating system level: For the OS may choose any scheduling algorithm such as the one we discussed earlier. 2. at the hardware level: this scheduling specifies how each core decides which hardware thread to run. The UltraSPARC T3 uses a simple round-robin algorithm to schedule eight hardware threads to each core. The Intel Itanium dual-core processor assigns two hardware threads for each core.
32
5.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Real-time CPU scheduling Soft Real-time systems: Provides no guarantees as to when a critical real-time process will be scheduled. It only guarantees that the process will be given preference over noncritical processes. Linux, Windows, Solaris, etc. support soft real-time scheduling. Hard Real-time systems: Guarantees that a critical task will be serviced by its deadline. Different scheduling algorithms exist for such systems. We will not talk about them here.
33
5.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Algorithm Evaluation Analytic evaluation Deterministic modeling – takes a particular predetermined workload and compares the performance of each algorithm for that workload. Simple and easy to do; but requires exact numbers for input which is hard to get for any given system. Queuing models: knowing the job arrival rates and service rates we can compute CPU utilization, average queue length, average waiting time, etc. This is a separate area of research, called queuing-network analysis Simulations Involves programming a model of the system. Results are of limited accuracy Implementation Implement the algorithm and study the performance It is the best way to study and compare the performance of different algorithms It is expensive and time consuming
34
5.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 5.15
35
5.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts CPU Scheduling in Solaris 2 Uses priority-based process scheduling It has six classes of scheduling, Time sharing, Interactive, real-time, system, Fair Share, Fixed Priority Within each of these classes, there are different priorities and different scheduling algorithms The default scheduling class for a process is time sharing. The scheduling policy for time-sharing class dynamically alters priorities and assigns time-slices of different length using multilevel feedback queue. There is an inverse relationship between priorities and time slices: the higher the priority, the smaller the time slice and the lower the priority, the larger the time slice Interactive processes typically have a higher priority; CPU bound processes have a lower priority. The scheduling policy is designed to give good response time for interactive processes and good throughput for CPU bound processes
36
5.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts CPU Scheduling in Solaris 2… Interactive class uses the same scheduling policy as the timesharing class but it gives windowing applications higher priority for better performance It uses the system class to run kernel processes such as scheduler, paging daemon, etc Priority of a system process, once established, never changes. User processes running in system mode do not belong to system class Scheduling policy for the system class does not time-slice. i.e., a process belonging to the system class runs until it blocks or is preempted by a higher priority process Processes in real-time class are given the highest priority to run among all classes
37
5.37 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts CPU Scheduling in Solaris 2… Each scheduling class includes a set of priorities The scheduler converts this class specific priorities into global priority and selects the process with the highest global priority. The selected process runs on the CPU until one of the following occurs: It blocks It uses its time-slice (if it is not a system process) It is preempted by a higher priority process
38
5.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduling in windows XP Windows XP uses a priority-based preemptive scheduling algorithm. It ensures that the highest priority process will always run A process selected by the scheduler (called dispatcher in windows) to run will run until It terminates Its time quantum ends or It calls a blocking system call, such as for I/O If a higher priority real-time process becomes ready while a lower priority process is running, the lower priority process will be preempted, thus giving a preferential treatment to real-time processes Scheduler uses a 32-level priority scheme to determine the order of process execution, and also has several classes of process
39
5.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduling in windows XP The scheduler uses a queue for each scheduling priority (i.e., 32 queues) and traverses the set of queues from highest to lowest until it finds a process to run. If no process is found, then it executes a special process called idle thread Priorities are divided into two classes The variable class Contains processes having priorities from 1 to 15 Priority of processes in this class can change This is further divided into high priority, above normal, normal, below normal, and idle priority classes The real-time class Contains processes with priorities ranging from 16 to 31 Priorities of processes in this class do not change.
40
5.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Windows XP Priorities Win32 API identifies several priority classes to which a process can belong. These include : Real-time, high, above normal, normal etc. A process within a given priority also has a relative priority. The values for these relative priorities include: time-critical, highest, above normal, below normal, etc.
41
5.41 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduling in windows XP Processes are typically members of normal priority class unless the parent process was of the idle priority class or another class was specified when the process was created When a process’s time quantum runs out The process is interrupted and its priority is lowered if it is in the variable priority class Lowering the priority helps in limiting CPU consumption of compute-bound processes When a variable-priority process is released from a wait operation, its priority is boosted The amount of boost depends on what the process was waiting for. For example, a process waiting for keyboard I/O will get a large boost whereas a process that was waiting for disc I/O will get a moderate boost. This strategy gives good response time for processes that are using windows and mouse (i.e., interactive processes), while compute-bound processes are permitted to use the CPU cycles in the background. (this strategy is used by other OSs as well) Windows distinguishes between foreground (i.e., processes selected on the screen) and background processes. When a process moves to the foreground, it increases the quantum by some factor – typically by 3.
42
5.42 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduling in Linux Linux provides two separate process-scheduling algorithms: one is designed for time-sharing processes for fair preemptive scheduling among multiple processes; the other, designed for real-time tasks A process may not be preempted while running in kernel mode even if a real-time process with a higher priority is ready For processes in the time-sharing class, Linux uses a prioritized credit-based algorithm. Each process (task) possesses a certain number of credits When a new process must be chosen, the process with most credit is selected Every time a timer interrupt occurs, the currently running process loses one credit. When the credit reaches 0, that process is suspended and another process is selected
43
5.43 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduling in Linux… If no runnable processes have any credit, then Linux performs a recrediting operation to every process in the system using the following rule: Credits = Credits/2 + priority Note that I/O bound processes that remain suspended can get multiple credits and hence will gain high priority; thus interactive processes will have rapid response time Using priority in calculating new credits allows the priority of a process to be fine-tuned. i.e., batch jobs can be given low priority and hence they will automatically receive fewer credits than the interactive users’ jobs.
44
5.44 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduling in Linux… Real-time scheduling: Linux implements two real-time scheduling classes, namely FCFS and RR In both cases each process has a priority in addition to its scheduling class. Scheduler always runs the process with highest priority Linux’s real-time scheduling is soft in the sense that it offers strict guarantees about priorities but the kernel does not offer any guarantees about how quickly a real-time process becomes runnable (Linux kernel code can be never preempted by user – mode code). For example, if an interrupt that wakes up a real- time process arrives while the kernel is already executing a system call on behalf of some other process, the real-time process has to just wait.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.