CPU SCHEDULING This chapter is about how to
CPU SCHEDULING This chapter is about how to get a process attached to a processor. It centers around efficient algorithms that perform well. The design of a scheduler is concerned with making sure all users get their fair share of the resources.
Preemptive & Non-preemptive scheduling Preemptive scheduling: An interrupt causes currently running process to give up the CPU and be replaced by another process. • The four circumstances under which CPU scheduling takes place are 1. When a process switches from the running state to the waiting state (when I/O request occurs) 2. When a process switches from the running state to the ready state (when Interrupt occurs) 3. When a process switches from the waiting state to the ready state (after completion of I/O) 4. When a process Terminates When scheduling takes place under circumstances 1 & 4, its non-preemptive. Ie, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. Preemptive scheduling incurs cost. ( eg. two processes sharing data)
Scheduling Criteria Different criteria are, • CPU utilization - to keep CPU as busy as possible • Throughput- the number of processes completed per unit time is called throughput. Its magnitude may change depending upon the type of process. • Turnaround time- the interval from the time of submission of a process to the time of completion. • Waiting time- the sum of the period spent waiting in the ready queue. • Response time- it’s the amount of time it takes to start responding, but not the time that it takes to output that response Its desirable to Maximize CPU utilization and throughput, and to Minimize turn around time, waiting time, and response time
CPU SCHEDULING Scheduling Algorithms FIRST-COME, FIRST SERVED: ( FCFS) same as FIFO Simple, fair, but poor performance. Average queuing time may be long. Implemented with a FIFO queue It may cause Convoy effect FCFS algorithm is non-preemptive its not at all suitable for timesharing system.
Scheduling Algorithms CPU SCHEDULING EXAMPLE DATA: Process Arrival Time 1 2 3 4 Service Time 0 1 2 3 8 4 9 5 FCFS P 1 0 P 2 8 P 3 12 P 4 21 Average waiting time = ( (0) + (8) + (12) + (21) )/4 = 41/4 = 10. 25 26
CPU SCHEDULING Scheduling Algorithms SHORTEST JOB FIRST: Processor is assigned to a task that has the smallest next CPU burst time. If two processes have the same length next CPU burst, FCFS scheduling is used to break the tie. Optimal for minimizing queuing time, but difficult to implement. Tries to predict the process to schedule based on previous history. Here: Optimal for Long-term scheduling: but difficult to implement for shortterm scheduling. t( n+1 ) = w * t( n ) + ( 1 - w ) * T( n ) t(n+1) is time of next burst. t(n) is time of current burst. T(n) is average of all previous bursts. w is a weighting factor emphasizing current or previous bursts.
Scheduling Algorithms CPU SCHEDULING EXAMPLE DATA: Process Service Time P 1 P 2 P 3 P 4 6 8 7 3 Non Preemptive Shortest Job First P 4 0 P 1 3 P 3 9 P 2 16 Average wait = ( (0) + (3) + (9) + (16) )/4 = 28/4 = 7 24
• SJF algorithm may be either Preemptive or Non-Preemptive • Preemptive SJF is sometimes called Shortestremaining-time-first scheduling
Scheduling Algorithms CPU SCHEDULING EXAMPLE DATA: Process P 1 P 2 P 3 P 4 Arrival Time Service Time 0 1 2 3 8 4 9 5 Preemptive Shortest Job First P 1 0 P 2 1 P 4 5 P 1 10 P 3 17 Average waiting time= ( (10 -1) + (17 -2) + (5 -3) )/4 = 26/4 = 6. 5 26
CPU SCHEDULING Scheduling Algorithms PREEMPTIVE ALGORITHMS: Yank the CPU away from the currently executing process when a higher priority process is ready. Can be applied to both Shortest Job First or to Priority scheduling. Avoids "hogging" of the CPU On time sharing machines, this type of scheme is required because the CPU must be protected from a run-away low priority process. Give short jobs a higher priority – perceived response time is thus better. What are average queuing and residence times? Compare with FCFS.
CPU SCHEDULING Scheduling Algorithms PRIORITY BASED SCHEDULING: Assign each process a priority. Schedule highest priority first. All processes within same priority are FCFS. Priority may be determined by user or by some default mechanism. The system may determine the priority based on memory requirements, time limits, or other resource usage. Starvation (indefinite blocking) occurs if a low priority process never runs. Solution: build aging into a variable priority. May be either Preemptive or Non-preemptive
• EXAMPLE DATA: • • P 2 0 Process P 1 P 2 P 3 P 4 P 5 1 Burst Time 10 1 2 1 Priority 3 1 3 4 5 2 P 1 6 P 3 16 Average wait = ( (6) + (0) + (16) + (18)+ (1) )/5 = 41/5 = 8. 2 P 4 18 19
CPU SCHEDULING Scheduling Algorithms ROUND ROBIN Scheduling: Especially designed for Time-sharing system. Similar to FCFS scheduling, but preemption is added A small unit of time, called, a time quantum or time slice is defined The scheduler allocate the CPU to each process for a time interval of one time quantum. The Ready queue will be implemented as a Circular (FIFO) queue. CPU burst time processes may less or greater than one time quantum. RR scheduling algorithm is Preemptive. The performance of RR algorithm heavily depends on the size of the time slice. If time slice is very large, algorithm will become same as that of FCFS If it is very small (then, the RR approach will be called as Processor sharing) number of context switch will be increased
Scheduling Algorithms CPU SCHEDULING EXAMPLE DATA: Process Arrival 1 2 3 4 Service Time 0 1 2 3 Time 8 4 9 5 Round Robin, quantum = 4, no priority-based preemption
Scheduling Algorithms CPU SCHEDULING EXAMPLE DATA: Process Arrival Service Time 0 1 2 3 4 Note: Example violates rules for Time quantum size since most 8 processes don’t finish in one 4 quantum. 9 5 Round Robin, quantum = 4, no priority-based preemption P 1 0 P 2 4 P 3 8 P 4 12 P 1 16 P 3 20 Average wait = ( (12) + (3) + (15) + (17) )/4 = 47/4 = 11. 75 P 4 24 P 3 25 26
MULTI-LEVEL QUEUES: • Used in situations in which processes are easily classified into different groups (For eg: foreground and background processes) Each queue has its own scheduling algorithm. There must be scheduling between queues, implemented as a fixed-priority preemptive scheduling. Scheduling among queues may be implemented on the basis of time slice
Multilevel Feedback Queue Scheduling • • • In multilevel queue scheduling, processes do not move between queues So, in that case scheduling overhead is low, but it is inflexible In Multilevel feedback queue scheduling, processes are allowed to move between queues. Processes are separated with different CPU burst characteristics. I/O bound and interactive processes are in the higher-priority queues Aging can be provided by moving a process, that has been waiting for a long time, to a higher-priority queue An entering process is inserted into the top-level queue and processes in this queue are allocated a relatively small time-slice Time slices associated with queues increases as the level lowers If a process is blocked before using its entire time-slice, it is either moved to next higher-level queue or the top-level queue It is the most general scheme, but it is the most complex one.
Time-slice = 8 Time-slice = 16 FCFS Multilevel feedback queues
CPU SCHEDULING Scheduling Algorithms MULTIPLE PROCESSOR SCHEDULING: Different rules for homogeneous or heterogeneous processors. Load sharing in the distribution of work, such that all processors have an equal amount to do. Each processor can schedule from a common ready queue ( equal machines ) – Self-scheduling- OR can use a master slave arrangement. In the case Self-scheduling, steps to be taken to avoid the situation that two processors select the same process.
CPU SCHEDULING WRAPUP We’ve looked at a number of different scheduling algorithms. Which one works the best is application dependent. General purpose OS will use priority based, round robin, preemptive Real Time OS will use priority, no preemption.
- Slides: 20