html5-img
1 / 131

Chapter 5, CPU Scheduling

Chapter 5, CPU Scheduling. 5 .1 Basic Concepts. Simple, non-preemptive scheduling means that a new process can be scheduled on the CPU only when the current job has begun waiting, for I/O, for example

kasen
Télécharger la présentation

Chapter 5, CPU Scheduling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5, CPU Scheduling

  2. 5.1 Basic Concepts • Simple, non-preemptive scheduling means that a new process can be scheduled on the CPU only when the current job has begun waiting, for I/O, for example • The goal of multi-programming is to maximize the utilization of the CPU as a system resource by having a process running on it at all times

  3. The CPU-I/O Burst Cycle • A CPU burst refers to the period of time when a given process occupies the CPU before making an I/O request or taking some other action which causes it to wait • CPU bursts are of varying length and can be plotted in a distribution by length • Overall system activity can also be plotted as a distribution of CPU and other activity bursts by processes

  4. The distribution of CPU burst lengths tends to be exponential or hyperexponential

  5. The CPU scheduler = the short term scheduler • (Under non-preemptive scheduling) when the processor becomes idle, a new process has to be picked and allocated the CPU • Note that the ready queue doesn’t have to be FIFO (although that is a simple, initial assumption) • It does tend to be some sort of linked data structure with a queuing discipline which implements the scheduling algorithm

  6. Preemptive scheduling • Scheduling decisions can be made at these points: • A process goes from the run state to the wait state (e.g., I/O wait, wait for a child process to terminate) • A process goes from the run state to the ready state (e.g., as the result of an interrupt) • A process goes from the wait state to the ready state (e.g., I/O completes) • A process terminates

  7. Scheduling has to occur at points 1 and 4. • If it only occurs then, this is non-preemptive or cooperative scheduling • If scheduling is also done at points 2 and 3, this is preemptive scheduling • Notice that much of the discussion in the previous chapters assumed preemptive scheduling—the use of interrupts, timers, etc., to trigger a context switch

  8. Historically, simple systems existed without timers, just like they existed without mode bits, for example • It is possible to write a simple, non-preemptive operating system for multi-programming without multi-tasking • Preemptive schedulers are more difficult to write and raise complex technical questions

  9. The problem with preemption comes from data sharing between processes • If two concurrent processes share data, this can lead to inconsistent data, lost update, etc. • Note that kernel data structures hold state for user processes. The user processes do not directly dictate what the kernel data structures contain, but by definition, the kernel loads the state of >1 user process

  10. This means that the kernel data structures themselves have the characteristic of data shared between processes • As a consequence, in order to be correctly implemented, preemptive scheduling has to prevent inconsistent state in the kernel data structures

  11. This can be explained concretely by considering the movement of PCB’s from one queue to another • If an interrupt occurs while one system process is moving a PCB, and the PCB has been removed form one queue, but not yet added to another, this is an error state

  12. Possible solutions to the problem • Only allow switching on I/O blocks. The idea is that interrupts will be queued rather than instantaneous (a queuing mechanism will be needed) • This means that processes will run to a point where they can be moved to an I/O queue and the next process will not be scheduled until that happens • This solves the problem of preemptive scheduling—by backing off to non-preemptive scheduling

  13. Other solutions to the problem: • Only allow switching after a system call runs to completion. In other words, make kernel processes uninterruptible. (This also assumes a queuing system for interrupts.) If the code that moves PCB’s around can’t be interrupted, inconsistent state can’t result. • Make certain code segments in the O/S uninterruptible. This is the same idea, but with finer granularity. It increases concurrency because interrupts can occur in parts of kernel code.

  14. Note that this issue is related to the problem of real time operating systems • If certain code blocks are not interruptible, you are not guaranteed a fixed, maximum response time to any particular system request or interrupt that you generate • You may have to wait an indeterminate amount of time while the uninterruptible code finishes processing • This violates the requirement for a hard real-time system

  15. The dispatcher = the module called by the short term scheduler which • Switches context • Switches to user mode • Jumps to the location in user code to run • Speed is desirable. Dispatch latency refers to time lost in the switching process

  16. Scheduling criteria • There are various algorithms for scheduling • There are also various criteria for evaluating them • Performance is always a trade-off • You can never maximize all of the criteria with one scheduling algorithm

  17. Criteria • CPU utilization. The higher, the better. 40%-90% is realistic • Throughput = processes completed / unit time • Turnaround time = total time for any single process to complete • Waiting time = total time spent waiting in O/S queues • Response time = time between submission and first visible sign of response to the request—important in interactive systems

  18. Depending on the criterion, you may want to: • Strive to attain an absolute maximum or minimum (utilization, throughput) • Minimize or maximize the average (turnaround, waiting) • Minimize or maximize the variance (for time-sharing, minimize the variance, for example)

  19. 5.3 Scheduling Algorithms • 5.3.1 First-Come, First-Served (FCFS) • 5.3.2 Shortest-Job-First (SJF) • 5.3.3 Priority • 5.3.4 Round Robin (RR) • 5.3.5 Multilevel Queue • 5.3.6 Multilevel Feedback Queue

  20. Reality involves a steady stream of many, many CPU bursts • Reality involves balancing a number of different performance criteria or measures • Examples of the different scheduling algorithms will be given below based on a very few processes and a limited number of bursts • The examples will be illustrated using Gantt charts • The scheduling algorithms will be evaluated and compared based on a simple measure of average waiting time

  21. FCFS Scheduling • The name, first-come, first-served, should be self-explanatory • This is an older, simpler scheduling algorithm • It is non-preemptive • It is not suitable for interactive time sharing • It can be implemented with a simple FIFO queue of PCB’s

  22. Consider the following scenario • Process Burst length • P1 24 ms. • P2 3 ms. • P3 3 ms.

  23. Avg. wait time = (0 + 24 + 27) / 3 =17 ms.

  24. Compare with a different arrival order: • P2, P3, P1

  25. Avg. wait time = (0 + 3 + 6) / 3 =3 ms.

  26. Additional comments on performance analysis • It is clear that average wait time varies greatly depending on the arrival order of processes and their varying burst lengths • As a consequence, it is also possible to conclude that for any given set of processes and burst lengths, arbitrary FCFS scheduling does not result in a minimal or optimal average wait time

  27. FCFS scheduling is subject to the convoy effect • There is the initial arrival order of process bursts • After that, the processes enter the ready queue after I/O waits, etc. • Let there be one CPU bound job (long CPU burst) • Let there be many I/O bound jobs (short CPU bursts)

  28. Scenario: • The CPU bound job holds the CPU • The other jobs finish their I/O waits and enter the ready queue • Each of the other jobs is scheduled, FCFS, and is quickly finished with the CPU due to an I/O request • The CPU bound job then takes the CPU again

  29. CPU utilization may be high (good) under this scheme • The CPU bound job is a hog • The I/O bound jobs spend a lot of their time waiting • Therefore, the average wait time will tend to be high • Recall that FCFS is not preemptive, so once the jobs have entered, scheduling only occurs when a job voluntarily enters a wait state due to an I/O request or some other condition

  30. SJF Scheduling • The name, shortest-job-first, is not quite self-explanatory • Various ideas involved deserve explanation • Recall that these thumbnail examples of scheduling are based on bursts, not the overall job time • For scheduling purposes, it is the length of the next burst that is important • There is no perfect way of predicting the length of the next burst

  31. Implementing SJF in reality involves devising formulas for predicting the next burst length based on past performance • SJF can be a non-preemptive algorithm. The assumption now is that all processes are available at time 0 for scheduling and the shortest is chosen • A more descriptive name for the algorithm is “shortest next CPU burst” scheduling

  32. SJF can also be implemented as a preemptive algorithm. The assumption is that jobs enter the ready queue at different times. If a job with a shorter burst enters the queue when a job with a longer burst is running, the shorter job preempts the longer one • Under the preemptive scenario a more descriptive name for the algorithm would be “shortest remaining time first” scheduling

  33. Non-preemptive Example • Consider the following scenario: • Process burst length • P1 6 ms. • P2 8 ms. • P3 7 ms. • P4 3 ms.

  34. SJF order: P4, P1, P3, P2average wait time = (0 + 3 + 9 + 16) / 4 =7 ms.

  35. SJF average wait time is lower than the average wait time for FCFS scheduling of the same processes: FCFS average wait time = (0 + 6 + 14 + 21) / 4 =10.25 ms.

  36. In theory, SJF is optimal for average wait time performance • Always doing the shortest burst first minimizes the aggregate wait time for all processes • This is only theoretical because burst length can’t be known • In a batch system user estimates might be used • In an interactive system user estimates make no sense

  37. Devising a formula for predicting burst time • The only basis for such a formula is past performance • What follows is the definition of an exponential average function for this purpose • Let tn = actual, observed length of nth CPU burst for a given process • Let Tn+1 = predicted value of next burst • Let a be given such that 0 <= a < 1 • Then define Tn+1 as follows: • Tn+1 = atn + (1 – a)Tn

  38. Explanation: • a is a weighting factor. How important is the most recent actual performance vs. performance before that • To get an idea of the function it serves, consider a = 0, a = ½, a = 1

  39. Tn appears in the formula. It is the previous prediction. • It includes real past performance because • Tn = atn-1 + (1 – a)Tn-1 • Ultimately this expansion depends on the initial predicted value, T0 • Some arbitrary constant can be used, a system average can be used, etc.

  40. Expanding the formula • This illustrates how come it is known as an exponential average • It gives a better feel for the role of the components in the formula • Tn+1 = atn + (1-a)(atn-1 + (1-a)(…at0 + (1-a)T0)…) • = atn + (1-a)atn-1 + (1-a)2atn-2 + … + (1-a)nat0 + (1-a)n+1T0

  41. The first term is: • atn • The general term is: • (1 – a)jatn-j • The last term is: • (1 – a)n+1T0

  42. In words • The most recent actual performance, tn, gets weight a • All previous performances, ti, are multiplied by a and by a factor of (1 – a)j, where the value of j is determined by how far back in time t occurred • Since (1 – a) < 1, as you go back in time, the weight of a given term on the current prediction is exponentially reduced

  43. The following graph illustrates the results of applying the formula with T0 = 10 and a = ½ • With a = ½, the exponential coefficients on the terms of the prediction are ½, (½)2, (½)3, … • Note that the formula tends to produce a lagging, not a leading indicator • In other words, as the actual values shift up or down, the prediction gradually approaches the new reality, whatever it might be

  44. Preemptive SJF • If a waiting job enters the ready queue with an estimated burst length shorter than the time remaining of the burst length of the currently running job, then the shorter job preempts the one on the CPU. • This can be called “shortest remaining time first” scheduling. • Unlike in the previous examples, the arrival time of a process now makes a difference

  45. Consider the following scenario: • Process arrival time burst length • P1 0 8 ms. • P2 1 4 ms. • P3 2 9 ms. • P4 3 5 ms.

  46. Preemptive SJF average wait time =(0 + 9 + 0 + 15 + 2) / 4 =6.5 ms.

  47. Walking through the example • P1 arrives at t = 0 and starts • P2 arrivess at t = 1 • P2’s burst length = 4 • P1’s remaining burst length = 8 – 1 = 7 • P2 preempts • P3 arrives at t = 2 • P3’s burst length burst length = 9 • P2’s remaining burst length = 4 – 1 = 3 • P1’s remaining burst length = 7 • No preemption

  48. P4 arrives at t = 3 • P4’s burst length = 5 • P3’s remaining burst length = 9 • P2’s remaining burst length = 3 – 1 = 2 • P1’s remaining burst length = 7 • No preemption • P2 runs to completion at t = 5 • P4 is scheduled. It runs to completion at t = 10 • P1 is rescheduled. It runs to completion at 17 • P3 is scheduled. It runs to completion at 26

More Related