cpu scheduling
play

CPU Scheduling Example Presented By: Dr. El-Sayed M. El-Alfy Note: - PowerPoint PPT Presentation

Recap: Process states and transitions Chapter 5: State diagram CPU Scheduling Example Presented By: Dr. El-Sayed M. El-Alfy Note: Most of the slides are compiled from the textbook and its complementary resources March 08 1 March 08


  1. Recap: Process states and transitions Chapter 5: � State diagram CPU Scheduling � Example Presented By: Dr. El-Sayed M. El-Alfy Note: Most of the slides are compiled from the textbook and its complementary resources March 08 1 March 08 2 Recap: Context Switching Objectives/Outline Objectives Outline Introduce CPU scheduling Basic Concepts � � Describe various CPU- Scheduling Criteria � � scheduling algorithms Scheduling Algorithms � Discuss evaluation criteria for � Multiple-Processor Scheduling � selecting a CPU-scheduling Real-Time Scheduling � algorithm for a particular Algorithm Evaluation � system 3 4 March 08 March 08

  2. Basic Concepts Basic Concepts (cont.) � Multiprogramming systems provide interleaved execution � Nature of Processes of several processes to give an illusion of many � Not all processes have an even mix of CPU and I/O usage simultaneously executing processes. � CPU-BOUND process � Computer can be a single-processor or multi-processor machine. � A number crunching program � OS must keep track of the state for each active process and may do a lot of computation make sure that the correct information is properly installed when and minimal I/O a process is given control of the CPU. � I/O-BOUND process � By switching CPU among processes can make the � A data processing job may do computer more productive (i.e. enhance CPU utilization) little computation and a lot of I/O � Required OS components: � Dispatcher Alternating Sequence of CPU And I/O Bursts � Scheduler March 08 5 March 08 6 Dispatcher CPU Scheduling Selects from among the processes in memory that are ready to � Dispatcher module gives control of the CPU to the process � execute, and allocates the CPU cycles to one of them selected by the short-term scheduler; this involves: Types of Schedulers � � Context switch – occurs when a process exchange is made Job scheduler (long-term scheduler) � between ready and run queues; OS must save the state of the � In a batch system, job scheduler decides as jobs arrive to the system which running process and restore the state of the ready process jobs to let into memory and in which order � Switching to user mode � Occurs less frequently � Jumping to the proper location in the user program to restart that CPU scheduler (short-term scheduler) � program � Decides which process to run next from those waiting in the ready queue � Short-term scheduling only deals with jobs that are currently resident in � Dispatch latency – time it takes for the dispatcher to stop memory one process and start another running � Occurs frequently Swapper (medium-term scheduler) � � Involves suspending or resuming processes by swapping (rolling) them out of or into memory (from disk) 7 8 March 08 March 08

  3. Scheduling Criteria CPU Scheduling CPU scheduling decisions may take place when a process: � Scheduling is an optimization task – it is performed in such a way � to achieve “ good performance ” of some criteria 1. Switches from running to waiting state 2. Switches from running to ready state There are many factors to consider: � 3. Switches from waiting to ready � CPU utilization: percentage of time the CPU is busy; keep the 4. Terminates CPU as busy as possible Scheduling under 1 and 4 is non-preemptive � � Throughput: number of jobs completed per time unit All other scheduling is preemptive � � Total service time (turnaround time): time from submission to Non-preemptive scheduler means: a process is never FORCED to give up completion of a job � control of the CPU. The process gives up control of the CPU only � Waiting Time: amount of time a job spends in the ready queue If it isn't using the CPU � � Response time: time until the system starts to respond to a If it is waiting for I/O � command If it is finished using the CPU � � much more useful in interactive systems Preemptive scheduling is forcing a process to give up control of the CPU � March 08 9 March 08 10 Additional Scheduling Criteria Optimization Criteria � There are also other factors to consider: � Max CPU utilization � Priority/Importance of work – hopefully more important work can � Max throughput be done first � Min turnaround time � Fairness – hopefully eventually everybody is served � Min waiting time � Implement policies to increase priority as we wait longer … (this is known as “ priority aging ” ) � Min response time � Deadlines – some processes may have hard or soft deadlines that must be met � In other words, we want to maximize CPU utilization � Overhead – e.g., data kept about execution activity, queue and throughput AND minimize turnaround, waiting, management, context switches and response times � Consistency and/or predictability may be a factor as well, especially in interactive systems 11 12 March 08 March 08

  4. Scheduling Algorithms First-Come, First-Served (FCFS) Scheduling Process Burst Time � First-Come, First-Served (FCFS) P 1 24 � Shortest Job First (SJF) P 2 3 � Shortest Remaining Time First (SRTF) P 3 3 � Priority � Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: � Round Robin (RR) � Multi-Level Queue (MLQ) P 1 P 2 P 3 � Multi-Level Feedback Queue (MLFQ) 0 24 27 30 � Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 � Average waiting time: (0 + 24 + 27)/3 = 17 March 08 13 March 08 14 FCFS Scheduling (Cont.) Convoy Effect pro c e ss se rvic e turnaro und waiting time t s time t t time t w � Suppose that the processes arrive in the order P 2 , P 3 , P 1 A 10 10 0 B 1 11 10 � The Gantt chart for the schedule is: pro c e ss C 3 14 11 D 4 18 14 AVE RAGE 13.25 8.75 P 2 P 3 P 1 A long CPU-bound job may A 10 hog the CPU and force shorter 0 3 6 30 (or I/O-bound) jobs to wait for B 1 � Waiting time for P 1 = 6 ; P 2 = 0 ; P 3 = 3 prolonged periods. This in turn may lead to a lengthy C 3 � Average waiting time: (6 + 0 + 3)/3 = 3 queue of ready jobs, and D 4 � Much better than previous case hence to the 'convoy effect' time 5 10 15 20 25 15 16 March 08 March 08

  5. FCFS Scheduling (Cont.) Shortest-Job-First (SJF) Scheduling � FCFS is: � Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the � Non-preemptive shortest time � Ready queue is a FIFO queue � Jobs arriving are placed at the end of ready queue � Two schemes: � First job in ready queue runs to completion of CPU burst � Nonpreemptive – once CPU is given to the process it cannot be preempted until the process completes its CPU burst � Advantages: simple, low overhead � Preemptive – if a new process arrives with CPU burst length less � Disadvantages: long waiting time, inappropriate for than the remaining time of current executing process, preempt. interactive systems, large fluctuations in average This scheme is know as the Shortest-Remaining-Time-First (SRTF) turnaround time are possible � SJF is optimal – gives minimum average waiting time for a given set of processes March 08 17 March 08 18 Example of Non-Preemptive SJF SRTF - Shortest Remaining Time First Process Arrival Time Burst Time � Preemptive version of SJF P 1 0.0 7 � Ready queue ordered on length of time till completion P 2 2.0 4 (shortest time to complete first, STCF ) P 3 4.0 1 � Arriving jobs inserted at proper position P 4 5.0 4 � shortest job � SJF (non-preemptive) � Runs to completion (i.e. CPU burst finishes) or � Runs until a job with a shorter remaining time arrives (i.e. P 1 P 3 P 2 P 4 placed in the ready queue) 0 3 7 8 12 16 � Average waiting time = (0 + 6 + 3 + 7)/4 = 4 19 20 March 08 March 08

  6. Example of Preemptive SJF (i.e., SRTF) Shortest-Job-First (SJF) Scheduling Process Arrival Time Burst Time � Ready queue treated as a priority queue based on P 1 0.0 7 smallest CPU-time requirement � Arriving jobs inserted at proper position in queue P 2 2.0 4 � Shortest job (1st in queue) runs to completion P 3 4.0 1 � In general, SJF is often used in long-term scheduling P 4 5.0 4 � Advantages: provably optimal w.r.t. average waiting time � Preemptive SJF (i.e., SRTF) � Disadvantages: P 1 P 2 P 3 P 2 P 4 P 1 � Starvation is possible! � Unimplementable at the level of short-term CPU scheduling. 11 16 0 � Can do it approximately: use exponential averaging to predict 2 4 5 7 length of next CPU burst = = > pick shortest predicted burst next! � Average waiting time = (9 + 1 + 0 + 2)/4 = 3 March 08 21 March 08 22 Determining Length of Next CPU Burst Prediction of the Length of the Next CPU Burst α = τ 0 = � Can only estimate the length 0 . 5 and 10 This figure is for � Can be done by using the length of previous CPU actual bursts, and using exponential averaging = th predicted 1. t actual lenght of n CPU burst n τ = + th 2. predicted value for the next (i.e., n 1 ) CPU burst + n 1 τ = th 3. predicted value for the n CPU burst n α ≤ α ≤ α = 4. , 0 1 0 . 5 ( ) τ = α + − α τ t 1 5. Define : + n 1 n n α = 0 implies making no use of recent history (τ n+ 1 = τ n ) α = 1 implies τ n+ 1 = t n (past prediction not used) α = 1/2 equally weighted Ex. Show that older bursts get less and less weight 23 24 March 08 March 08

Recommend


More recommend