saurav karmakar. chapter 5: cpu scheduling  basic concepts  scheduling criteria  scheduling...

Download Saurav Karmakar. Chapter 5: CPU Scheduling  Basic Concepts  Scheduling Criteria  Scheduling Algorithms  Thread Scheduling  Multiple-Processor Scheduling

Post on 18-Jan-2018

216 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

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

TRANSCRIPT

Saurav Karmakar Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation 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 Basic Concepts Uniprocessor system one process may run at a time Objective of multiprogramming some process running at all times to maximize CPU utilization When one process has to wait, the operating system takes the CPU away from that process and gives the CPU to another process Almost all computer resources are scheduled before use CPU-I/O Burst Cycle Success of CPU scheduling follows : Process execution CPUI/O Burst Cycle Consists of a cycle of CPU execution and I/O wait Basically While a process waits for I/O, CPU sits idle if no multiprogramming Instead the OS can give CPU to another process CPU burst distribution Distribution for frequency vs duration of CPU bursts. Exponential or hyperexpoinential in nature Alternating Sequence of CPU And I/O Bursts Histogram of CPU-burst Times CPU Scheduler Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them : Short-term Scheduler The ready queue is not necessarily a FIFO queue 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 leaves us no choice But option 2 and 3 does. CPU Scheduler Nonpreemptive : once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either Scheduling under 1 and 4 is Nonpreemptive/Cooperative All other scheduling is Preemptive Preemptive Scheduling incurs some costs : Access to shared data Effects on the design of operating system kernel Effects of interrupts 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 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: ( )/3 = 17 P1P1 P2P2 P3P 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: ( )/3 = 3 Much better than previous case So Average waiting time vary substantially if the burst time for processes vary Generally quite long. This is non-preemptive in nature troublesome for time sharing system P1P1 P3P3 P2P 5: CPU-Scheduling17 Process Arrival Service Time Time P1P2P3P4 FCFS Average wait = ( (8-0) + (12-1) + (21-2) + (26-3) )/4 = 61/4 = FCFS Algorithm Residence Time at the CPU Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. Using these lengths to schedule the process with the shortest time If two processes have the same length next CPU burst, FCFS scheduling is used to break the tie. Scheduling depends on the length of the next CPU burst(lower the better) SJF is optimal gives minimum average waiting time for a given set of processes Difficulty knowing the length of the next CPU request Shortest-Job-First (SJF) Scheduling Example of SJF ProcessBurst Time P16 P16 P 2 8 P37 P37 P43 P43 SJF scheduling chart Average waiting time = ( ) / 4 = 7 By moving a short process before a long one, the waiting time of the short process decreases more than it increases the waiting time of the long process. Consequently, the average waiting time decreases. SJF scheduling is used frequently in long-term scheduling. P4P4 P3P3 P1P P2P2 24 Determining Length of Next CPU Burst How to implement it at short-term scheduler ? Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging 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 0 Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor Prediction of the Length of the Next CPU Burst 5: CPU-Scheduling26 Process Arrival Service Time Time P2P4P1P3 Preemptive Shortest Job First Average wait = ( (10-1) + (1-1) + (17-2) + (5-3) )/4 = 26/4 = 6.5 P1 1 Preemptive SJF Algorithm Shortest Remaining Time First Algorithm Priority Scheduling SJF is a special case of the general priority schedule algorithm Priority inverse of the next CPU Burst 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 Problem Starvation low priority processes may never execute Solution Aging as time progresses increase the priority of the process Priority Scheduling Average Waiting Time = ( )/5 = 8.2 What about fairness ? What about it? Strict fixed-priority scheduling between queues is unfair (run highest, then next, etc): long running jobs may never get CPU In Multics, shut down machine, found 10-year-old job Must give long-running jobs a fraction of the CPU even when there are shorter jobs to run Tradeoff: fairness gained by hurting avg response time! How to implement fairness? Could give each queue some fraction of the CPU What if one long-running job and 100 short-running ones? Like express lanes in a supermarketsometimes express lanes get so long, get better service by going into one of the other lines Fair Share Scheduling Problems with priority-based systems Priorities are absolute: no guarantees when multiple jobs with same priority No encapsulation and modularity Behavior of a system module is unpredictable: a function of absolute priorities assigned to tasks in other modules Solution: Fair-share scheduling Each job has a share: some measure of its relative importance denotes users share of system resources as a fraction of the total usage of those resources e.g., if user As share is twice that of user B then, in the long term, A will receive twice as many resources as B Traditional implementations keep track of per-process CPU utilization (a running average) reprioritize processes to ensure that everyone is getting their share are slow! Lottery Scheduling Yet another alternative here. Give each job some number of lottery tickets On each time slice, randomly pick a winning ticket On average, CPU time is proportional to number of tickets given to each job How to assign tickets? To approximate SRTF, short running jobs get more, long running jobs get fewer To avoid starvation, every job gets at least one ticket (everyone makes progress) Advantage over strict priority scheduling: behaves gracefully as load changes Adding or deleting a job affects all jobs proportionally, independent of how many tickets each job possesses Lottery Scheduling Example Assume short jobs get 10 tickets, long jobs get 1 ticket Round Robin (RR) Designed specially for time sharing system. Each process gets a small unit of CPU time (time quantum), usually milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. The ready queue generally is circular queue. Avg wait time is often long 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 q small q must be large with respect to context switch, otherwise overhead is too high ; processor sharing Example of RR with Time Quantum = 4 ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: Avg Waiting Time = ((10-4)+4+7)/3 =5.66 Typically, higher average turnaround than SJF, but no better response P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum 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; i.e., 80% to foreground in RR 20% to background in FCFS 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 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. Thread Scheduling : Contention S