1 / 21

Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling. Efficiency Goal: Some (useful) process running at all times if such a process exists Maximum of one process running (uniprocessor) CPU–I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait CPU burst distribution is not uniform

orli-nixon
Download Presentation

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 • Efficiency Goal: Some (useful) process running at all times if such a process exists • Maximum of one process running (uniprocessor) • CPU–I/O Burst Cycle • Process execution consists of a cycle of CPU execution and I/O wait • CPU burst distribution is not uniform • Approaches: • Batch: Reduce I/O / setup cost • Multiprogramming: Context switch on I/O • Time-sharing (multi-tasking): Context switch on I/O or expiration of quantum • Maximum CPU utilization obtained with multiprogramming • Why? CEG 433/633 - Operating Systems I

  2. Histogram of CPU-burst Times CEG 433/633 - Operating Systems I

  3. Context Switch • Assumption of Finite Progress: Processes move forward in execution in a sequential fashion, a temporal halt does not effect the correctness of the execution and thus allowed. Execution rate is positive, but unknown • A context switch (and thus a CPU scheduling decision) takes place on: 1. Process state changes from running to waiting 2. Process state changes from running to terminated 3. Process state changes from waiting to ready 4. Process state changes from running to ready • Non-preemptive: a process keeps the CPU until it releases it through its own actions (initiate I/O or termination) (1 and 2, above) • MS-Windows is non-preemptive • Preemptive: “outside” events may cause context switch(3 and 4) • subject to race-conditions CEG 433/633 - Operating Systems I

  4. Scheduler and Dispatcher • Almost all computer resources are allocated (scheduled) before use. The CPU is one of the primary resources. • CPU Scheduler: selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them • The Dispatcher carries out the scheduler decision • 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 CEG 433/633 - Operating Systems I

  5. Scheduling Criteria • CPU utilization – keep the CPU as busy as possible (0-100%) • Throughput – # of processes that complete their execution per time unit • Turnaround time – amount of time to execute a particular process (from entering the system to leaving the system) • Waiting time – amount of time a process waits in the ready queue • Note: This is the metric the scheduling algorithm really affects - it can not directly reduce wait time (I/O) or execution time (on CPU) • Response time – amount of time it takes from when a request was submitted until the first I/O is produced. Very useful for interactive processes (such as a shell) where I/O speed is not relevant • Optimize for overall (average) performance - but to be fair we want to minimize additional metrics (such as maximum turnaround time, etc.) CEG 433/633 - Operating Systems I

  6. P1 P2 P3 0 24 27 30 First-Come, First-Served (FCFS) Scheduling • Example: ProcessBurst 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: • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 • Average turn-around time (24+27+30)/3 = 27 CEG 433/633 - Operating Systems I

  7. P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) • Suppose that the processes arrive in the order, P2 , P3 , P. The Gantt chart for the schedule is • Waiting time for P1 = 6;P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Average turn-around time: (30+ 3+ 6)/3 = 13 • Much better than previous case • FCFS is non-preemptive, no “decisions”, performance is poor (average turn-around time tends to be large) • Convoy effect: All of the short process (I/O-bound) are slowed behind the long processes (CPU-bound). I/O sits idle when, for the small price of a preemption and short CPU-burst, it could be doing useful work CEG 433/633 - Operating Systems I

  8. Shortest-Job-First (SJR) Scheduling • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. • Provably optimal (gives minimum average waiting time for a given set of processes) but requires historic knowledge/hints (req. time) or an oracle (req. magic) • Example: predict next CPU burst length as an exponential average of its history • Two schemes: • 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, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF). CEG 433/633 - Operating Systems I

  9. P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (non-preemptive) • Average waiting time = (0 + 6 + 3 + 7)/4 = 4 • Average turn-around time = ((7-0)+(8-4)+(12-2)+(16-5))/4 = 8 CEG 433/633 - Operating Systems I

  10. P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 Example of Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (preemptive): Assume no effective overhead for switch • Average waiting time = (9 + 1 + 0 +2)/4 = 3 • Average turn-around time = (16 + 5 + 1 + 6)/4 = 7 CEG 433/633 - Operating Systems I

  11. Determining Length of Next CPU Burst • Can only estimate the length. • Can be done by using the length of previous CPU bursts, using exponential averaging. CEG 433/633 - Operating Systems I

  12. Examples of Exponential Averaging •  =0 • n+1 = n • Recent history does not count. •  =1 • n+1 = tn • Only the actual last CPU burst counts. • If we expand the formula, we get: n+1 =  tn+(1 - )  tn -1 + … +(1 -  )j  tn -1 + … +(1 -  )n=1 tn 0 • Since both  and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor. CEG 433/633 - Operating Systems I

  13. Priority Scheduling • A priority number (integer) is associated with each process (smallest integer  highest priority). • The CPU is allocated to the process with the highest priority • Ties are usually handled FCFS • Preemptive or Non-preemptive • SJF is a priority scheduling algorithm • priority is the predicted next CPU burst time • Problem: Starvation – low priority processes may never execute • Solution: Aging – as time progresses increase the priority of the process • Note: Priority is often based on process-type. Interactive or I/O-bound processes have a higher priority that CPU-bound processes CEG 433/633 - Operating Systems I

  14. 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. • FCFS but with a switch every quantum • “Circular” Queue • Performance depend on quantum • Turnaround time and overhead Vary with Time Quantum • q  FCFS • q  0  context switch overhead 100% of CPU time • Non optimal (SJF is better) but response time is less/better CEG 433/633 - Operating Systems I

  15. P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example: RR with Time Quantum = 20 ProcessBurst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is: CEG 433/633 - Operating Systems I

  16. Multilevel Queue • Ready queue is partitioned into separately scheduled queues:foreground (interactive) - RRbackground (batch) - FCFS (or priority, or preemptive SJF) • Scheduling must be done between the queues. • Fixed priority scheduling; i.e., serve all from high priority queue before considering next priority. Possibility of starvation. • Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% interactive, 20% batch CEG 433/633 - Operating Systems I

  17. Multilevel Feedback Queue • The standard multilevel queue algorithm does not allow a process to change queues • Using feedback/history, a process can be moved 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 CEG 433/633 - Operating Systems I

  18. Example of Multilevel Feedback Queue • Three queues: • Q0 – time quantum 8 milliseconds • Q1 – time quantum 16 milliseconds • Q2 – FCFS • Scheduling • A new job enters queue Q0which is servedFCFS. 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. • Most general scheduling algorithm CEG 433/633 - Operating Systems I

  19. CPU Scheduling in UNIX • Three classes of processes: Time Sharing, Interactive, Real Time • Processes are given small CPU time slices that reduce to round-robin for CPU-bound jobs • Designed to benefit interactive processes • Each process has a scheduling priority (large # is low priority) • Set locally, check with priocntl -l (-60 to 60 is common) • Processes with priority < 0 are doing I/O or OS tasks • such processes can not be killed by signals! • Ordinary user processes have priority > 0 • “nice” can be used to set priority • The more CPU time a process accumulates the more positive its number (negative feedback) • Gives short-term scheduler some control over “job mix” • Aging prevents starvation CEG 433/633 - Operating Systems I

  20. Multiple-Processor Scheduling • CPU scheduling more complex when multiple CPUs are available • If processors are identical (homogeneous), then we can simply focus on load-sharing • Use a common ready queue • Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing • Symmetric multiprocessing - any CPU can access system data structures (such as the ready queue) • More efficient • More reliable if race conditions are avoided CEG 433/633 - Operating Systems I

  21. Algorithm Evaluation • Deterministic modeling – • Define performance for a synthetic workload • Queuing models - Queuing Network Analysis (Theoretic) • Simulation - Random interarrival, burst length, etc. • Implementation - Experimentation in real conditions • Look for major problems and solutions... • Livelock (starvation) • Solution: Aging • Priority Inversion - What if a high priority process needs to access resources held by a low priority process? Does the high priority process must wait on the low-priority process? • Solution: Priority Inheritance - Upgrade the lower priority process until required resource is released • Is this a good solution? CEG 433/633 - Operating Systems I

More Related