2/15/2011 Scheduling Algorithms CPU Scheduling algorithms deal with the problem Scheduling Algorithms of deciding which process in ready queue should be allocated to CPU. Following are the commonly used scheduling algorithms: Gursharan Singh Tatla professorgstatla@gmail.com www.eazynotes.com 15-Feb-2011 www.eazynotes.com 15-Feb-2011 1 2 First-Come-First-Served Scheduling Algorithms Scheduling (FCFS) First-Come-First-Served (FCFS) In this scheduling, the process that requests the CPU first, is allocated the CPU first. Shortest Job First (SJF) Thus, the name First-Come-First-Served. Priority Scheduling The implementation of FCFS is easily managed Round-Robin Scheduling (RR) with a FIFO queue. Multi-Level Queue Scheduling (MLQ) Ready Queue Multi-Level Feedback Queue Scheduling (MFQ) D C B A CPU T ail of Head of Queue Queue www.eazynotes.com www.eazynotes.com 3 15-Feb-2011 4 15-Feb-2011 First-Come-First-Served Example of FCFS Scheduling Scheduling (FCFS) When a process enters the ready queue, its PCB Consider the following set of processes that arrive is linked to the tail of the queue. at time 0 with the length of the CPU burst time in milliseconds: When CPU is free, it is allocated to the process which is at the head of the queue. Process Burst Time (in milliseconds) P 1 24 FCFS scheduling algorithm is non-preemptive . P 2 3 Once the CPU is allocated to a process, that P 3 3 process keeps the CPU until it releases the CPU, either by terminating or by I/O request. www.eazynotes.com www.eazynotes.com 5 15-Feb-2011 6 15-Feb-2011 1
2/15/2011 Example of FCFS Scheduling Example of FCFS Scheduling Suppose that the processes arrive in the order: P 1 , P 2 , P 3 . P 1 24 Average Waiting Time = (Total Waiting Time) / The Gantt Chart for the schedule is: P 2 3 No. of Processes P 3 3 P 1 P 2 P 3 = (0 + 24 + 27) / 3 0 24 27 30 = 51 / 3 Waiting Time for P 1 = 0 milliseconds = 17 milliseconds Waiting Time for P 2 = 24 milliseconds Waiting Time for P 3 = 27 milliseconds www.eazynotes.com 15-Feb-2011 www.eazynotes.com 15-Feb-2011 7 8 Example of FCFS Scheduling Example of FCFS Scheduling Suppose that the processes arrive in the order: P 2 , P 3 , P 1 . Average Waiting Time = (Total Waiting Time) / The Gantt chart for the schedule is: No. of Processes = (0 + 3 + 6) / 3 P 2 P 3 P 1 = 9 / 3 0 3 6 30 Waiting Time for P 2 = 0 milliseconds = 3 milliseconds Waiting Time for P 3 = 3 milliseconds Thus, the average waiting time depends on the order in which the processes arrive. Waiting Time for P 1 = 6 milliseconds www.eazynotes.com www.eazynotes.com 9 15-Feb-2011 10 15-Feb-2011 Shortest Job First Scheduling Non-Preemptive SJF (SJF) In non-preemptive scheduling, CPU is assigned In SJF, the process with the least estimated execution to the process with least CPU burst time. time is selected from the ready queue for execution. The process keeps the CPU until it terminates. It associates with each process, the length of its next CPU burst. Advantage: When the CPU is available, it is assigned to the It gives minimum average waiting time for a given process that has the smallest next CPU burst. set of processes. If two processes have the same length of next CPU Disadvantage: burst, FCFS scheduling is used. It requires knowledge of how long a process will run SJF algorithm can be preemptive or non-preemptive. and this information is usually not available. www.eazynotes.com www.eazynotes.com 11 15-Feb-2011 12 15-Feb-2011 2
2/15/2011 Preemptive SJF Example of Non-Preemptive SJF In preemptive SJF, the process with the smallest Consider the following set of processes that arrive estimated run-time is executed first. at time 0 with the length of the CPU burst time in milliseconds: Any time a new process enters into ready queue, the scheduler compares the expected run-time of Process Burst Time (in milliseconds) this process with the currently running process. P 1 6 P 2 8 If the new process’s time is less, then the P 3 7 currently running process is preempted and the P 4 3 CPU is allocated to the new process. www.eazynotes.com 15-Feb-2011 www.eazynotes.com 15-Feb-2011 13 14 Example of Non-Preemptive SJF Example of Non-Preemptive SJF The Gantt Chart for the schedule is: P 1 6 P 2 8 Average Waiting Time = (Total Waiting Time) / P 1 P 4 P 3 P 2 P 3 7 No. of Processes P 4 3 0 3 9 16 24 = (0 + 3 + 9 + 16 ) / 4 Waiting Time for P 4 = 0 milliseconds = 28 / 4 Waiting Time for P 1 = 3 milliseconds = 7 milliseconds Waiting Time for P 3 = 9 milliseconds Waiting Time for P 2 = 16 milliseconds www.eazynotes.com www.eazynotes.com 15 15-Feb-2011 16 15-Feb-2011 Example of Preemptive SJF Example of Preemptive SJF Consider the following set of processes. These The Gantt Chart for the schedule is: processes arrived in the ready queue at the times P 1 P 2 P 4 P 1 P 3 given in the table: 5 0 26 1 10 17 Process Arrival Time Burst Time (in milliseconds) Waiting Time for P 1 = 10 – 1 – 0 = 9 P 1 0 8 P 2 1 4 Waiting Time for P 2 = 1 – 1 = 0 P 3 2 9 P AT BT P 4 3 5 Waiting Time for P 3 = 17 – 2 = 15 P 1 0 8 P 2 1 4 Waiting Time for P 4 = 5 – 3 = 2 P 3 2 9 P 4 3 5 www.eazynotes.com www.eazynotes.com 17 15-Feb-2011 18 15-Feb-2011 3
2/15/2011 Example of Preemptive SJF Explanation of the Example Process P 1 is started at time 0, as it is the only process in the queue. Average Waiting Time = (Total Waiting Time) / No. of Processes Process P 2 arrives at the time 1 and its burst time is 4 milliseconds. = (9 + 0 + 15 + 2) / 4 This burst time is less than the remaining time of = 26 / 4 process P 1 (7 milliseconds). = 6.5 milliseconds So, process P 1 is preempted and P 2 is scheduled. www.eazynotes.com 15-Feb-2011 www.eazynotes.com 15-Feb-2011 19 20 Explanation of the Example Explanation of the Example Process P 3 arrives at time 2. Its burst time is 9 After the termination of P 2 , the process with which is larger than remaining time of P 2 (3 shortest next CPU burst i.e. P 4 is scheduled. milliseconds). After P 4 , processes P 1 (7 milliseconds) and then So, P 2 is not preempted. P 3 (9 milliseconds) are scheduled. Process P 4 arrives at time 3. Its burst time is 5. Again it is larger than the remaining time of P 2 (2 milliseconds). So, P 2 is not preempted. www.eazynotes.com www.eazynotes.com 21 15-Feb-2011 22 15-Feb-2011 Priority Scheduling Priority Scheduling Priority scheduling can be preemptive or non- In priority scheduling, a priority is associated with preemptive. all processes. Preemptive Priority Scheduling: Processes are executed in sequence according In this, scheduler allocates the CPU to the new process to their priority. if the priority of new process is higher tan the priority of the running process. CPU is allocated to the process with highest Non-Preemptive Priority Scheduling: priority. The running process is not interrupted even if the new If priority of two or more processes are equal than process has a higher priority. FCFS is used to break the tie. In this case, the new process will be placed at the head of the ready queue. www.eazynotes.com www.eazynotes.com 23 15-Feb-2011 24 15-Feb-2011 4
2/15/2011 Priority Scheduling Priority Scheduling Problem: Solution: In certain situations, a low priority process can be Aging is a technique which gradually increases the blocked infinitely if high priority processes arrive in priority of processes that are victims of starvation. the ready queue frequently. For e.g.: Priority of process X is 10. This situation is known as Starvation . There are several processes with higher priority in the ready queue. Processes with higher priority are inserted into ready queue frequently. In this situation, process X will face starvation. www.eazynotes.com 15-Feb-2011 www.eazynotes.com 15-Feb-2011 25 26 Priority Scheduling Example of Priority Scheduling (Cont.): Consider the following set of processes that arrive at time 0 with the length of the CPU burst time in The operating system increases priority of a milliseconds. The priority of these processes is process by 1 in every 5 minutes. also given: Thus, the process X becomes a high priority Process Burst Time Priority process after some time. P 1 10 3 And it is selected for execution by the scheduler. P 2 1 1 P 3 2 4 P 4 1 5 P 5 5 2 www.eazynotes.com www.eazynotes.com 27 15-Feb-2011 28 15-Feb-2011 Example of Priority Scheduling Example of Priority Scheduling The Gantt Chart for the schedule is: P 2 P 5 P 1 P 3 P 4 Average Waiting Time = (Total Waiting Time) / 0 1 6 16 18 19 No. of Processes = (0 + 1 + 6 + 16 + 18) / 5 Waiting Time for P 2 = 0 P BT Pr P 1 10 3 = 41 / 5 Waiting Time for P 5 = 1 P 2 1 1 P 3 2 4 Waiting Time for P 1 = 6 = 8.2 milliseconds P 4 1 5 Waiting Time for P 3 = 16 P 5 5 2 Waiting Time for P 4 = 18 www.eazynotes.com www.eazynotes.com 29 15-Feb-2011 30 15-Feb-2011 5
Recommend
More recommend