Download presentation
Presentation is loading. Please wait.
Published byMarilynn Sherman Modified over 9 years ago
1
Silberschatz, Galvin and Gagne 2002 5.1 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation
2
Silberschatz, Galvin and Gagne 2002 5.2 Operating System Concepts Basic Concepts In a uniprocessor system, only one process may run at a time (other processes must wait until CPU is free) The objective of multiprogramming is to maximize CPU utilization (CPU sits idle frequently without m.p.) Several processes are kept in memory at one time. When one process has to wait (I/O operation), the operating system takes the CPU away form that process and gives the CPU to another process. Scheduling is a fundamental OS function. All computer resources are scheduled before use. CPU is scheduled before use too.
3
Silberschatz, Galvin and Gagne 2002 5.3 Operating System Concepts CPU–I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait. Processes alternate between two states. Process execution begins with a CPU burst, then I/O wait, then CPU burst, then…… The last CPU burst will end with a system request to terminate execution.
4
Silberschatz, Galvin and Gagne 2002 5.4 Operating System Concepts Histogram of CPU-burst Times CPU burst distribution can help us select an appropriate CPU-scheduling algorithm Many short CPU bursts, but a few long CPU bursts
5
Silberschatz, Galvin and Gagne 2002 5.5 Operating System Concepts CPU Scheduler Whenever the CPU becomes idle, OS must select from among the processes in memory that are ready to execute (in the ready queue), and allocates the CPU to one of them. The selection process is carried out by the short-term scheduler (CPU scheduler). The ready queue is not necessary a first-in, first-out queue. It can be implemented with a FIFO, priority queue or a tree. We will consider various scheduling algorithms in the followings.
6
Silberschatz, Galvin and Gagne 2002 5.6 Operating System Concepts Diagram of Process State 1 1 4 2 3
7
Silberschatz, Galvin and Gagne 2002 5.7 Operating System Concepts Nonpreemptive Scheduling CPU scheduling decisions may take place when a process: 1.Switches from the running state to the waiting state. 2.Switches from the running state to the ready state. 3.Switches from the waiting state to the ready state. 4.Terminates. Scheduling only under 1 and 4 is “nonpreemptive scheduling”. (because the CPU must be reallocated to other process definitely under circumstances 1 and 4) Under nonpreemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it release the CPU either by terminating or by switching to the waiting state.
8
Silberschatz, Galvin and Gagne 2002 5.8 Operating System Concepts Preemptive Scheduling All other scheduling is called “preemptive scheduling”. (If a process just entering the ready state has higher priority than the running process, it might take over the CPU) Preemptive scheduling needs cost. If two processes share data, inconsistency happens with preemptive scheduling. Dispatcher is the module giving control of the CPU to the process selected by the short-term scheduler. Its 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
Silberschatz, Galvin and Gagne 2002 5.9 Operating System Concepts Scheduling Criteria CPU utilization – keep the CPU as busy as possible (ex:70%) Throughput – number of processes completed per time unit Turnaround time – amount of time to execute a particular process (the interval from the time of submission of a process to the time of completion) Waiting time – sum of time that a process spends waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response (not output) is produced (Turnaround time is generally limited by the speed of the output device)
10
Silberschatz, Galvin and Gagne 2002 5.10 Operating System Concepts Optimization Criteria Maximize CPU utilization Maximize throughput Minimize turnaround time Minimize waiting time Minimize response time In most cases, we optimize the average measure. But in some cases, we want to optimize the minimum or maximum values, rather than average. To guarantee that all users get good (real-time) service, we may want to minimize the maximum response time not minimize the average response time.
11
Silberschatz, Galvin and Gagne 2002 5.11 Operating System Concepts 1. First-Come, First-Served (FCFS) Scheduling ProcessBurst Time P 1 24 P 2 3 P 3 3 P1P1 P2P2 P3P3 2427300 FCFS is like an FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When CPU is free, it is allocated to the process at the head of the queue. The average waiting time of FCFS is often quite long. Three processes arrive at time 0 If processes arrive in the order: P 1, P 2, P 3, the Gantt chart is : Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17
12
Silberschatz, Galvin and Gagne 2002 5.12 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 Obviously, the average waiting time of FCFS varies greatly The FCFS scheduling algorithm is Nonpreemptive. P1P1 P3P3 P2P2 63300
13
Silberschatz, Galvin and Gagne 2002 5.13 Operating System Concepts Convoy Effect Convoy effect -- all the other processes wait for the one big process to get off the CPU The effect results in lower CPU and device utilization than might be possible if the shorted processes were allowed to go first. The FCFS is particularly troublesome for time-sharing system, where each user needs to get a share of the CPU at regular intervals.
14
Silberschatz, Galvin and Gagne 2002 5.14 Operating System Concepts 2. Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. CPU is assigned to the process that has the smallest next CPU burst. Note that : the shortest next CPU burst not the shortest CPU bursts (sum of all its CPU bursts) SJF is provably optimal – give minimum average waiting time for a given set of processes by moving a short process before a long one. The difficulty with the SJF algorithm is knowing the length of the next CPU burst.
15
Silberschatz, Galvin and Gagne 2002 5.15 Operating System Concepts ProcessBurst Time P 1 6 P 2 8 P 3 7 P 4 3 Average waiting time = (3 + 16 + 9 +0 )/4 = 7 milliseconds Average waiting time is (0+6+14+21)/4=10.25 for FCFS scheduling Shortest-Job-First (SJF) Scheduling P4P4 P1P1 P2P2 6 3 160 P3P3 9 12 24
16
Silberschatz, Galvin and Gagne 2002 5.16 Operating System Concepts When a new process arrives at the ready queue while a previous process is executing. The new process may have a shorter next CPU burst than what is left of the currently executing process. A preemptive SJF algorithm will preempt the currently executing process (Shortest-Remaining-Time-First scheduling). A nonpreemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive and Nonpreemptive SJF
17
Silberschatz, Galvin and Gagne 2002 5.17 Operating System Concepts Example #1 of SJF ProcessArrival TimeBurst Time P 1 0.08 P 2 1.04 P 3 2.09 P 4 3.05 SJF (preemptive) Average waiting time = (9+ 0 + 15 +2)/4 = 6.5 SJF (nonpreemptive) (0+7+15+9)/4=7.75 5 1 10 P1P1 P4P4 P2P2 17 0 P3P3 P1P1 26 8 12 P1P1 P2P2 17 0 P3P3 P4P4 26
18
Silberschatz, Galvin and Gagne 2002 5.18 Operating System Concepts 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 SJF (non-preemptive) (0 + 6 + 3 + 7)/4 =4 Example #2 of SJF P1P1 P3P3 P2P2 7160 P4P4 812 4257 P1P1 P3P3 P2P2 11 0 P4P4 P2P2 P1P1 16
19
Silberschatz, Galvin and Gagne 2002 5.19 Operating System Concepts Determining Length of Next CPU Burst The real difficulty with the SJF algorithm is knowing the length of the next CPU burst. For long-term scheduling in a batch system, we can use as the length of the process time limit that a user specifies when he submit the job (shortest-job first-served). So SJF scheduling is used frequently in long-term (job) scheduling. SJF is not easily for short-term (CPU) scheduling. No way to know the length of the next CPU burst. The only solution is to predict the next CPU burst by assuming that the next CPU burst will be similar in length to the previous ones. Exponential average
20
Silberschatz, Galvin and Gagne 2002 5.20 Operating System Concepts 3. Priority Scheduling A priority number (integer) is associated with each process. Here, the smallest integer represents highest priority. CPU is allocated to the process with the highest priority Preemptive nonpreemptive SJF is a special case of priority scheduling where the predicted next CPU burst time is used to decide priority. Problem: lower priority processes may never execute (starvation or indefinite blocking). Solution: Aging – as time progresses increase the priority of the process.
21
Silberschatz, Galvin and Gagne 2002 5.21 Operating System Concepts Example of Priority Scheduling Process Burst Time Priority P 1 10 3 P 2 1 1 P 3 2 4 P 4 1 5 P 5 5 2 Priority Scheduling Average waiting time = (6 + 0 + 16 + 18 + 1)/5 = 8.2 P2P2 P5P5 1 0 P3P3 P4P4 P1P1 16 6 18 19
22
Silberschatz, Galvin and Gagne 2002 5.22 Operating System Concepts 4. Round Robin (RR) Scheduling RR is designed specially for time-sharing system. It is similar to FCFS, 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. Keep the ready queue as a FIFO queue. New process are added to the end of ready queue, and every time the process in the head of ready queue is picked by CPU scheduler. 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 a time. No process waits more than (n-1)q time units.
23
Silberschatz, Galvin and Gagne 2002 5.23 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 (longer average waiting time) than SJF, but better response. P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162
24
Silberschatz, Galvin and Gagne 2002 5.24 Operating System Concepts Time Quantum and Context Switch Time Performance q large FCFS q small processor sharing; each user feels he owns a processor; many context switches (higher overhead). Tradeoff between q and response time is very important. => 80% of the CPU bursts should be shorter than q
25
Silberschatz, Galvin and Gagne 2002 5.25 Operating System Concepts Turnaround Time Varies With The Time Quantum Quantum=7, average turnaround time (6+9+10+17)/4=10.5
26
Silberschatz, Galvin and Gagne 2002 5.26 Operating System Concepts 5. Multilevel Queue Scheduling Ready queue is partitioned into separate queues: foreground (interactive) & background (batch) Each queue has its own scheduling algorithm for different response-time requirement foreground – RR background – FCFS Scheduling must be also done between the queues. Fixed priority scheduling; (e.g., the foreground queue may have absolute priority over background queue). Possibility of starvation (the example in next page). Time slice – each queue gets a certain amount of CPU time, which it can schedule among its processes; (e.g., 80% for foreground with RR, 20% for background with FCFS)
27
Silberschatz, Galvin and Gagne 2002 5.27 Operating System Concepts Multilevel Queue Scheduling Each queue has absolute priority over lower-priority queues (lower-priority processes might be preempted anytime).
28
Silberschatz, Galvin and Gagne 2002 5.28 Operating System Concepts 6. 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
29
Silberschatz, Galvin and Gagne 2002 5.29 Operating System Concepts Example of Multilevel Feedback Queue Three queues: Q 0 – time quantum 8 milliseconds Q 1 – time quantum 16 milliseconds Q 2 – FCFS Scheduling Processes in Q 2 will be executed only if Q 0 and Q 1 are empty. Processes arrive for higher-priority queue will preempt a running process of lower-priority queue. 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 the tail of queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds (Note: Q 0 is empty now). If it still does not complete, it is preempted and moved to queue Q 2.
30
Silberschatz, Galvin and Gagne 2002 5.30 Operating System Concepts Example of Multilevel Feedback Queue The multilevel feedback queue is the most general CPU- scheduling algorithm. It is also the most complex one.
31
Silberschatz, Galvin and Gagne 2002 5.31 Operating System Concepts Multiple-Processor Scheduling CPU scheduling is more complex when multiple CPUs are available (load sharing is very important). There is no best solution as in single CPU system. More details can be found in references. Homogeneous processors within a multiprocessor. Homogeneous system: any available processor can be used to run any processes in the queue Heterogeneous system: only programs compiled for a given processor’s instruction set could be run on that processor. (Chapter 14 through 16)
32
Silberschatz, Galvin and Gagne 2002 5.32 Operating System Concepts Multiple-Processor Scheduling Each processor is self-scheduling (must deal with data sharing problem carefully, symmetric multiprocessing). A processor is assigned as scheduler for other processors (master-slave structure--only one processor accesses the system data structures, asymmetric multiprocessing). XP, Solaris, Linux and Mac use SMP. Because of the high cost of invalidating and re-populating 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 (named as process affinity).
33
Silberschatz, Galvin and Gagne 2002 5.33 Operating System Concepts Load Balancing Provide a separate queue for each processor Problem: one processor could be idle (with empty queue) while another processor was very busy Push migration-- a specific task periodically checks the load on each processor and distributes load by moving (pushing) processes from overloaded to idle processors. Pull migration– an idle processor pulls a waiting task from a busy processor. Load balancing counteracts the benefits of processor affinity.
34
Silberschatz, Galvin and Gagne 2002 5.34 Operating System Concepts Real-Time Scheduling Hard real-time systems – required to complete a critical task within a guaranteed amount of time. A process is submitted along with a statement of the amount of time in which it needs; The scheduler can either admit the process (guarantee on time) or reject it. --- Resource reservation (here, the scheduler must know exactly how long each type of operating-system function takes to perform) Without secondary storage or virtual memory because of unavoidable and unforeseeable variation in access time. A special OS runs on dedicated hardware (lack the full functionality of modern computer and OS).
35
Silberschatz, Galvin and Gagne 2002 5.35 Operating System Concepts Soft real-time computing – requires that critical processes has higher priority. It is at least possible to achieve. The system must have priority scheduling and real-time processes must have the highest priority. The priority of real- time process must not degrade over time (without aging) The dispatch latency must be small. Many OSs are designed to wait either for a system call to complete or for an I/O block to take place before doing a context switch. The dispatch latency can be long since some system calls are very complex (preemption is necessary) a. Insert preemption point in system call b. make the entire kernel preemptible (use CS in Chap 6) Real-Time Scheduling
36
Silberschatz, Galvin and Gagne 2002 5.36 Operating System Concepts Thread Scheduling User-level threads vs. kernel-level threads: User-level threads are managed by a thread library, and the kernel is unaware of them. Use-level threads are mapped to an associated kernel-level thread (or a lightweight process, LWP) to run on a CPU. The thread library schedules user-level threads to an LWP (local scheduling or process-contention scope, PCS)-- performed by software library. The kernel schedules kernel-level thread to a CPU (system global scheduling or system-contention scope, SCS)-- performed by OS.
37
Silberschatz, Galvin and Gagne 2002 5.37 Operating System Concepts Solaris 2 Threads
38
Silberschatz, Galvin and Gagne 2002 5.38 Operating System Concepts Scheduling Algorithm Evaluation Deterministic modeling Calculate the performance of algorithm for real workload Queueing Models Calculate the performance of algorithm based on queueing models Simulations Program a model of computer system to generate the proper inputs. Implementation Put OS on a real system to see how it works. No best solution
39
Silberschatz, Galvin and Gagne 2002 5.39 Operating System Concepts Deterministic Modeling Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload. It is simple and fast, because it gives exact numbers, allowing the algorithms to be compared. The main uses of deterministic modeling are in describing scheduling algorithms and providing examples. Deterministic modeling is too specific, and requires too much exact knowledge, to be useful.
40
Silberschatz, Galvin and Gagne 2002 5.40 Operating System Concepts Queueing Models There is no static set of processes to use for deterministic modeling The distribution of CPU and I/O bursts may be measured and then approximated or simply estimated Little’s formula: n = λ * W n is the average queue length W is the average waiting time in the queue λ is the average arrival rate for new processes in the queue
41
Silberschatz, Galvin and Gagne 2002 5.41 Operating System Concepts Simulations Involving programming a model of the computer system Use a random-number generator, which is programmed to generate processes, CPU-burst times, arrivals, departures, and so on, according to probability distributions The frequency distribution indicates only how many of each event occur Trace tape – monitor the real system, recording the sequence of actual events expensive
42
Silberschatz, Galvin and Gagne 2002 5.42 Operating System Concepts Implementation The only completely accurate way to evaluate a scheduling algorithm is to put the actual algorithm in the real system for evaluation under real operating conditions Difficulty: Expensive (coding the algorithm and modifying the operating system & the reaction of the users to a constantly changing operating system) The environment in which the algorithm is used will change
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.