{"id":54,"date":"2018-07-19T05:10:18","date_gmt":"2018-07-19T05:10:18","guid":{"rendered":"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=54"},"modified":"2018-08-07T05:00:25","modified_gmt":"2018-08-07T05:00:25","slug":"cpu-scheduling-i","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/chapter\/cpu-scheduling-i\/","title":{"rendered":"CPU Scheduling &#8211; I"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>7.1 Introduction\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">CPU scheduling is used in operating systems to make use of the processor time to the maximum and to provide multiple users with a feel that they are the sole users of the system. This module explains the basic concepts of CPU scheduling and the different criteria needed for evaluating CPU scheduling algorithms and the working of the First Come First Served (FCFS) and non-preemptive Shortest Job First (SJF) CPU scheduling algorithms.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>7.2 Basic Concepts\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In a multiprogramming system, many processes are kept in memory at a particular time. Any process, during execution may have to wait for the completion of some I\/O request. During this time, the CPU will remain idle. But, the objective of multiprogramming is to have some program running always and hence maximize CPU utilization. Hence, when a process waits for I\/O, the operating system takes away t he CPU from the process and assigns the CPU to another process. But, of the many processes residing in the memory, which is the next process to which the CPU is assigned? This selection is done by the CPU scheduler.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The success of CPU scheduling depends on the following property of processes: Process execution comprises a cycle of CPU execution (CPU burst) and I\/O wait (I\/O burst). Any process alternates between these two states. Process execution begins with a CPU burst. This CPU burst is followed by an I\/O burst, then a CPU burst, then an I\/O burst and so on. Figure 7.1 shows an example of how a process alternates between CPU and I\/O bursts, during its execution.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider the followi ng program in C to find the sum of two numbers:<\/p>\r\n<p style=\"text-align: justify\">int main(void)<\/p>\r\n<p style=\"text-align: justify\">{<\/p>\r\n<p style=\"text-align: justify\">int a,b, sum;<\/p>\r\n<p style=\"text-align: justify\">\/*I\/O burst*\/<\/p>\r\n<p style=\"text-align: justify\">printf(\u201cEnter the two numbers to be added\\n\u201d);<\/p>\r\n<p style=\"text-align: justify\">scanf(\u201c%d %d\u201d,&amp;a,&amp;b);<\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n\/*CPU burst *\/\r\n\r\nsum = a+b;\r\n\r\n\/*I\/O burst*\/\r\n\r\nprintf(\u201c The sum of %d and %d is %d \\n\u201d, a,b,sum);\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">It can be seen that the first two statements are I\/O bound, the third statement is CPU-bound and the next statement is I\/O bound. Thus any program will \u00a0have alternating CPU-bound and I\/O bound i nstructions.<\/p>\r\n<img class=\" wp-image-68 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence.png\" alt=\"\" width=\"512\" height=\"388\" \/>\r\n<p style=\"text-align: center\">Fig. 7.1 Alternating sequence of CPU and I\/O bursts (Source: [1])<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">If a program has few and very long CPU bursts , it is a CPU-bound program. An I\/O-bound program has many very short CPU bursts . The duration of CPU bursts vary from process to process and from computer to computer. Still, the CPU bursts tend to have a frequency curve similar to that shown i n Figure 7.2. It is seen from Figure 7.2 that very short CPU bursts are more frequent than long CPU bursts. Whenever a CPU burst ends, there is an I\/O burst. When there is an I\/O burst, the process has to wait for I\/O to get completed and is moved to the waiting state. Now, the CPU scheduler has to run to select the next process that should be assigned the CPU. Since the CPU bursts are frequent, the CPU scheduler has to run frequently.<\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n<img class=\" wp-image-67 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram.png\" alt=\"\" width=\"470\" height=\"373\" \/>\r\n<p style=\"text-align: center\">Fig. 7.2 Histogram of CPU-burst times (Source: [1])<\/p>\r\n&nbsp;\r\n\r\n<strong>7.3 <\/strong><strong>C<\/strong><strong>P<\/strong><strong>U Scheduler<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Whenever the CPU becomes idle, the operating system should select one of the processes in the ready queue and that process is assigned the CPU. This selection is done by the CPU scheduler. The CPU scheduler is also called the s hort-term scheduler. The CPU scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them. The ready queue from which the processes are selected need not be a first i n, first out queue, always. The ready queue may even be a priority queue, a tree or an unordered linked list.<\/p>\r\n&nbsp;\r\n\r\nFigure 7.3 shows the change i n states during the lifetime of a process. CPU scheduling decisions may take place when a process:\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Switches from running to waiting state (I\/O, wait system call)\r\n\r\n2.\u00a0 Switches from running to ready state (timer i nterrupt)\r\n\r\n3.\u00a0 Switches from waiti ng to ready (completion of I\/O)\r\n\r\n4.\u00a0 Termi nates\r\n\r\n<\/div>\r\n<img class=\" wp-image-66 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state.png\" alt=\"\" width=\"521\" height=\"395\" \/>\r\n<div style=\"text-align: justify\">\r\n<p style=\"text-align: center\">Fig. 7.3 Process state transition diagram<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">When the process switches from the running state to the waiting state or when the process terminates, the process relinquishes the CPU on its own. It is not forced to relinquish the CPU. When scheduling takes place in these circumstances, it is called non-preemptive CPU scheduling. When the process moves from the waiting state to the ready state or when the process moves from the running state to the ready state, the CPU is forcibly removed from the process. The scheduling that happens in these circumstances is called preemptive scheduling.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.4\u00a0 Dispatcher\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Only the selection of which process will use the CPU next is done by the CPU scheduler. The dispatcher module is the one that gives control of the CPU to the process selected by the short-term scheduler. The dispatcher takes care of the following:<\/p>\r\n&nbsp;\r\n\r\n1. Switching context\r\n\r\n2. Switching to user mode\r\n<p style=\"text-align: justify\">3. Jumping to the proper location in the user program to restart that program Thus, it takes some \u00a0time for the dispatcher to stop one process and start another process. This time is called dispatch latency. The dispatch latency should be as less as possible because the CPU does not do any useful work during this period.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.5 Scheduling Criteria\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Many CPU scheduling algorithms have been proposed. To select the best CPU scheduling algorithm, various criteria can be considered. The following are some of the criteria that are used to analyze the performance of any CPU scheduling algorithm:<\/p>\r\n&nbsp;\r\n\r\n1. CPU utilization \u2013 Keep the CPU as busy as possible. Hence, CPU utilization should be maximum.\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">2. Throughput \u2013 Number of processes that complete their execution per time unit.\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">Throughput should be maximum.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">3. Turnaround time \u2013 amount of time to execute a particular process. This should be as minimum as possible.<\/span><\/p>\r\n<span style=\"text-align: initial;font-size: 1em\">4. Waiting time \u2013 amount of time a process has been waiting in the ready queue.\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">This should be as minimum as possible.<\/span>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">5. Response time \u2013 amount of time it takes from when a request was submitted\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">until the first response is produced, not output (for time-sharing environment). Response time should be as minimum as possible.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>7.6 Scheduling algorithms\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this section, we will learn two CPU scheduling algorithms, First Come First Served (FCFS) scheduling and Shortest Job First (SJF) scheduling algorithms.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.6.1 First-Come, First-Served (FCFS) Scheduling\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In FCFS, the process that arrived first and requests the CPU first is assigned the CPU first. In FCFS, the ready queue can be maintained as a FIFO queue. When processes arrive they are added to the tail of the queue. When the CPU becomes free, the process in the head of the queue is taken for execution. This is a non-preemptive algorithm. Once a process is assigned the CPU, the process continues to use the CPU till its CPU burst time is completed. When its CPU burst gets over, the process by itself relinquishes the CPU. FCFS is explained with an example given below:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider three processes <em>P<\/em>1<em>, P<\/em>2 and <em>P<\/em>3 with CPU-burst times 24 milliseconds (ms), 3 ms and 3 ms respectively. All three processes arrive at the same time i n the order <em>P<\/em>1, <em>P<\/em>2, <em>P<\/em>3.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Gantt chart that shows the order i n which processes are assigned the CPU is shown below. Since process <em>P<\/em>1 is the first process in the queue, <em>P<\/em>1\u00a0 is assigned the CPU first. <em>P<\/em>1 has a CPU-burst length of 24 ms. <em>P<\/em>1 uses the CPU for 24 ms and then relinquishes the CPU as shown in Figure 7.4.<\/p>\r\n<img class=\" wp-image-65 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1.png\" alt=\"\" width=\"335\" height=\"76\" \/>\r\n<p style=\"text-align: center\">Fig. 7.4 Process <em>P<\/em>1 using the CPU<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next process i n the queue is <em>P<\/em>2. <em>P<\/em>2 is scheduled next. <em>P<\/em>2 has a CPU-burst time of 3 ms and uses the CPU till 27 ms as shown in Figure 7.5.<\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n<img class=\"wp-image-64 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2.png\" alt=\"\" width=\"350\" height=\"86\" \/>\r\n<p style=\"text-align: center\">Fig. 7.5 Process <em>P<\/em>2 using the CPU<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">After <em>P<\/em>2, <em>P<\/em>3 is the only process in the queue and it is assigned the CPU. <em>P<\/em>3 uses the CPU for a time duration of 3 ms and then relinquishes the CPU as shown i n Figure 7.6.<\/p>\r\n<img class=\"wp-image-63 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3.png\" alt=\"\" width=\"374\" height=\"84\" \/>\r\n<p style=\"text-align: center\">Fig. 7.4 Process <em>P<\/em>3 using the CPU<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The waiting time for each process and hence the average waiting time can be calculated. Since all the three processes arrive at the same time, the arrival time of the processes is taken as 0. Process <em>P<\/em>1 arrived at time 0 and was assigned the CPU immediately. Hence, the waiting time for <em>P<\/em>1 = 0. Process <em>P<\/em>2 arrived at time 0 and was assigned the CPU at time 24. Hence, the waiting time for <em>P<\/em>2 = 24. Process P3 arrived at time 0 and was assigned the CPU at time 27. Hence, the waiting time for <em>P<\/em>3 = 27. Hence, the average waiting time is (0 + 24 + 27) \/ 3 = 17.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Similarly, the turnaround time for each process and hence the average turnaround time can be calculated. Process <em>P<\/em>1 arrived at time 0 and completed its CPU burst at time 24. Hence, the turnaround time for <em>P<\/em>1 = 24. Process <em>P<\/em>2 arrived at time 0 and completed its CPU burst at time 27. Hence, the turnaround time for <em>P<\/em>2 = 27. Process <em>P<\/em>3 arrived at time 0 and completed its CPU burst at time 30. Hence, the turnaround time for <em>P<\/em>3 = 30.The average turnaround time is (24 + 27 + 30) \/ 3 = 27.<\/p>\r\n&nbsp;\r\n\r\nSuppose if the three processes arrive in the order <em>P<\/em>2, <em>P<\/em>3, <em>P<\/em>1. The Gantt chart for the schedule is as shown in Figure 7.5.\r\n\r\n<img class=\"wp-image-62 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with.png\" alt=\"\" width=\"389\" height=\"71\" \/>\r\n<p style=\"text-align: center\">Fig. 7.5 Gantt chart with the order of the processes changed<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The waiting time of the processes can be calculated as follows: The arrival time for all the processes is the same and is taken as 0. Therefore, the waiting time for <em>P<\/em>1 <em>= <\/em>6<em>, P<\/em>2 = 0 and <em>P<\/em>3 <em>= <\/em>3. The average waiting time is (6 + 0 + 3)\/3 = 3. The turnaround time for <em>P<\/em>1 <em>= <\/em>30<em>, P<\/em>2 = 3 and <em>P<\/em>3 <em>= <\/em>6. Therefore, the average turnaround time is (3 + 6 + 30)\/3 = 13. It is seen that the average waiting time and the average turnaround time have improved when the order of the processes is changed. In the earlier case, process <em>P<\/em>1 had a long\u00a0<span style=\"font-size: 1em;text-align: initial\">CPU burst and the other processes had to wait for <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 to complete its CPU burst. This increased the average waiting time and the average turnaround time.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>7.6.2\u00a0 Shortest-Job-First (SJF) Scheduling\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The SJF CPU scheduling algorithm associates with each process the length of its next CPU burst. These lengths are used to schedule the process with the shortest time. When the CPU becomes available, the process with the shortest next CPU burst is assigned the CPU. There are two schemes i n SJF. One is non-preemptive in which once CPU is assigned to a process, it cannot be preempted until it completes its CPU burst. The other is preemptive in which if a new process arrives with CPU burst length less than the remaining time of the currently executing process, the currently executing process is preempted. This scheme is known as the Shortest-Remaining-Time-First (SRTF). In this module, we will learn how scheduling is done i n non-preemptive SJF.<\/p>\r\n&nbsp;\r\n\r\nConsider four processes <em>P<\/em>1<em>, P<\/em>2<em>, P<\/em>3\u00a0 and <em>P<\/em>4<em>. <\/em>The arri val times and the CPU-burst times of the four processes are gi ven below:\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Process<\/strong><\/td>\r\n<td><strong>Arriva l Time<\/strong><\/td>\r\n<td><strong>Burst Time<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>1<\/td>\r\n<td>0.0<\/td>\r\n<td>7<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>2<\/td>\r\n<td>2.0<\/td>\r\n<td>4<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>3<\/td>\r\n<td>4.0<\/td>\r\n<td>1<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>4<\/td>\r\n<td>5.0<\/td>\r\n<td>4<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">At time 0.0, <em>P<\/em>1 is the only process that has arrived. Hence , <em>P<\/em>1 is assigned the CPU first. Since it is non-preemptive SJF scheduling, <em>P<\/em>1 continues to use the CPU till its CPU-burst gets over as shown in Figure 7.6<\/p>\r\n<img class=\"wp-image-61 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU.png\" alt=\"\" width=\"406\" height=\"84\" \/>\r\n<p style=\"text-align: center\">Fig. 7.6 Process <em>P<\/em>1 using the CPU<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">After <em>P<\/em>1 finishes its CPU-burst, the processes remaining i n the ready queue are <em>P<\/em>2, <em>P<\/em>3 and <em>P<\/em>4. The processes <em>P<\/em>2, <em>P<\/em>3\u00a0 and <em>P<\/em>4 have now arrived. Of these three processes, <em>P<\/em>3 has the shortest CPU burst. Hence, <em>P<\/em>3 is assigned the CPU. <em>P<\/em>3 makes use of the CPU for a time duration that is equal to its CPU burst (1 ms) and then relinquishes the CPU as in Figure 7.7.<\/p>\r\n<img class=\"wp-image-60 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU.png\" alt=\"\" width=\"409\" height=\"91\" \/>\r\n<p style=\"text-align: center\">Fig. 7.7 Process <em>P<\/em>3usingtheCPU<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 and <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">4 are the remaining processes. Both the processes have the same CPU burst time. Any one of these can be assigned the CPU. So, the tie is now broken using FCFS. Of the two processes, <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 arrived earlier. Hence, <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 is assigned the CPU and <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 executes till its CPU burst is completed as i n Figure 7.8.<\/span><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n<img class=\"wp-image-59 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU.png\" alt=\"\" width=\"404\" height=\"81\" \/>\r\n<p style=\"text-align: center\">Fig. 7.8 <em>P<\/em>2 using the CPU<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">After <em>P<\/em>2 finishes execution, the only process waiting is <em>P<\/em>4. <em>P<\/em>4 is now assigned the CPU. After <em>P<\/em>4 runs for its CPU-burst time, it relinquishes the CPU as shown i n Figure 7.9.<\/p>\r\n<img class=\"wp-image-58 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU.png\" alt=\"\" width=\"417\" height=\"85\" \/>\r\n<p style=\"text-align: center\">Fig. 7.9 <em>P<\/em>4 using the CPU<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The average waiting time and the average turnaround time can be calculated as follows. The waiting time for process <em>P<\/em>1 is 0. <em>P<\/em>1 arrived at time 0 and was assigned the CPU immediately. <em>P<\/em>1 did not wait. <em>P<\/em>2 arrived at time 2.0 and was assigned the CPU at time 8.0. Hence, the waiting time for process <em>P<\/em>2 is 8 - 2 = 6. <em>P<\/em>3 arrived at time 4.0 and was assigned the CPU at time 7.0. Hence, the waiting time for process <em>P<\/em>2 is 7 - 4 = 3. Process <em>P<\/em>4 arrived at time 5.0 and was assigned the CPU at time 12.0. Hence the waiting time of process <em>P<\/em>2 is 12 - 5 = 7. Hence, the average waiting time = (0 + (8 - 2) + (7 - 4) + (12 - 5)) \/ 4 = 4.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><em>P<\/em>1 arrived at time 0 and completed execution at 7. The turnaround time of process <em>P<\/em>1 is 7. <em>P<\/em>2 arrived at time 2.0 and completed execution at time 12.0. Hence, the turnaround time for process <em>P<\/em>2 is 12 - 2 = 10. <em>P<\/em>3 arrived at time 4.0 and completed execution at time 8.0. Hence , the waiting time for process <em>P<\/em>2 is 8 - 4 = 4. Process <em>P<\/em>4 arrived at time 5.0 and completed execution at time 16.0. Hence , the turnaround time of process <em>P<\/em>2 is 16 - 5 = 11. Hence the a verage waiting time = (0 + (8 - 2) + (7 - 4) + (12 - 5)) \/ 4 = 4. Hence, the average turnaround time = ((7 - 0) + (12 - 2) + (8 - 4) + (16 - 5)) \/\u00a0 4 = 32 \/ 4 =8.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.7\u00a0 Summary\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module, we learnt what CPU scheduling is. Multiprogramming maximizes CPU utilization. The CPU scheduler selects the next process that should be assigned the \u00a0CPU. \u00a0Many CPU scheduling algorithms \u00a0have \u00a0been proposed. CPU scheduling\u00a0<span style=\"text-align: justify;font-size: 1em\">algorithms can be preemptive or non-preemptive. In this module, we learnt how First Come First Served (FCFS) and non-preemptive Shortest Job First (SJF) CPU scheduling algorithms work. We saw that SJF has less waiting time and less turnaround time when compared to FCFS.<\/span><\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify\">Abraham Silberschatz, \u00a0Peter \u00a0B. \u00a0Galvin, \u00a0Greg \u00a0Gagne, \u00a0\u201cOperating \u00a0System Concepts\u201d, Sixth Edition, John Wiley &amp; Sons Inc., 2003.<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>7.1 Introduction\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">CPU scheduling is used in operating systems to make use of the processor time to the maximum and to provide multiple users with a feel that they are the sole users of the system. This module explains the basic concepts of CPU scheduling and the different criteria needed for evaluating CPU scheduling algorithms and the working of the First Come First Served (FCFS) and non-preemptive Shortest Job First (SJF) CPU scheduling algorithms.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>7.2 Basic Concepts\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In a multiprogramming system, many processes are kept in memory at a particular time. Any process, during execution may have to wait for the completion of some I\/O request. During this time, the CPU will remain idle. But, the objective of multiprogramming is to have some program running always and hence maximize CPU utilization. Hence, when a process waits for I\/O, the operating system takes away t he CPU from the process and assigns the CPU to another process. But, of the many processes residing in the memory, which is the next process to which the CPU is assigned? This selection is done by the CPU scheduler.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The success of CPU scheduling depends on the following property of processes: Process execution comprises a cycle of CPU execution (CPU burst) and I\/O wait (I\/O burst). Any process alternates between these two states. Process execution begins with a CPU burst. This CPU burst is followed by an I\/O burst, then a CPU burst, then an I\/O burst and so on. Figure 7.1 shows an example of how a process alternates between CPU and I\/O bursts, during its execution.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider the followi ng program in C to find the sum of two numbers:<\/p>\n<p style=\"text-align: justify\">int main(void)<\/p>\n<p style=\"text-align: justify\">{<\/p>\n<p style=\"text-align: justify\">int a,b, sum;<\/p>\n<p style=\"text-align: justify\">\/*I\/O burst*\/<\/p>\n<p style=\"text-align: justify\">printf(\u201cEnter the two numbers to be added\\n\u201d);<\/p>\n<p style=\"text-align: justify\">scanf(\u201c%d %d\u201d,&amp;a,&amp;b);<\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p>\/*CPU burst *\/<\/p>\n<p>sum = a+b;<\/p>\n<p>\/*I\/O burst*\/<\/p>\n<p>printf(\u201c The sum of %d and %d is %d \\n\u201d, a,b,sum);<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">It can be seen that the first two statements are I\/O bound, the third statement is CPU-bound and the next statement is I\/O bound. Thus any program will \u00a0have alternating CPU-bound and I\/O bound i nstructions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-68 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence.png\" alt=\"\" width=\"512\" height=\"388\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence.png 835w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence-300x227.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence-768x582.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence-225x171.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Alternating-sequence-350x265.png 350w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.1 Alternating sequence of CPU and I\/O bursts (Source: [1])<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If a program has few and very long CPU bursts , it is a CPU-bound program. An I\/O-bound program has many very short CPU bursts . The duration of CPU bursts vary from process to process and from computer to computer. Still, the CPU bursts tend to have a frequency curve similar to that shown i n Figure 7.2. It is seen from Figure 7.2 that very short CPU bursts are more frequent than long CPU bursts. Whenever a CPU burst ends, there is an I\/O burst. When there is an I\/O burst, the process has to wait for I\/O to get completed and is moved to the waiting state. Now, the CPU scheduler has to run to select the next process that should be assigned the CPU. Since the CPU bursts are frequent, the CPU scheduler has to run frequently.<\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-67 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram.png\" alt=\"\" width=\"470\" height=\"373\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram.png 817w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram-300x238.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram-768x609.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram-225x178.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Histogram-350x278.png 350w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.2 Histogram of CPU-burst times (Source: [1])<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.3 <\/strong><strong>C<\/strong><strong>P<\/strong><strong>U Scheduler<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Whenever the CPU becomes idle, the operating system should select one of the processes in the ready queue and that process is assigned the CPU. This selection is done by the CPU scheduler. The CPU scheduler is also called the s hort-term scheduler. The CPU scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them. The ready queue from which the processes are selected need not be a first i n, first out queue, always. The ready queue may even be a priority queue, a tree or an unordered linked list.<\/p>\n<p>&nbsp;<\/p>\n<p>Figure 7.3 shows the change i n states during the lifetime of a process. CPU scheduling decisions may take place when a process:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Switches from running to waiting state (I\/O, wait system call)<\/p>\n<p>2.\u00a0 Switches from running to ready state (timer i nterrupt)<\/p>\n<p>3.\u00a0 Switches from waiti ng to ready (completion of I\/O)<\/p>\n<p>4.\u00a0 Termi nates<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-66 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state.png\" alt=\"\" width=\"521\" height=\"395\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state.png 817w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state-300x227.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state-768x582.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state-225x170.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-state-350x265.png 350w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/p>\n<div style=\"text-align: justify\">\n<p style=\"text-align: center\">Fig. 7.3 Process state transition diagram<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When the process switches from the running state to the waiting state or when the process terminates, the process relinquishes the CPU on its own. It is not forced to relinquish the CPU. When scheduling takes place in these circumstances, it is called non-preemptive CPU scheduling. When the process moves from the waiting state to the ready state or when the process moves from the running state to the ready state, the CPU is forcibly removed from the process. The scheduling that happens in these circumstances is called preemptive scheduling.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.4\u00a0 Dispatcher\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Only the selection of which process will use the CPU next is done by the CPU scheduler. The dispatcher module is the one that gives control of the CPU to the process selected by the short-term scheduler. The dispatcher takes care of the following:<\/p>\n<p>&nbsp;<\/p>\n<p>1. Switching context<\/p>\n<p>2. Switching to user mode<\/p>\n<p style=\"text-align: justify\">3. Jumping to the proper location in the user program to restart that program Thus, it takes some \u00a0time for the dispatcher to stop one process and start another process. This time is called dispatch latency. The dispatch latency should be as less as possible because the CPU does not do any useful work during this period.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.5 Scheduling Criteria\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Many CPU scheduling algorithms have been proposed. To select the best CPU scheduling algorithm, various criteria can be considered. The following are some of the criteria that are used to analyze the performance of any CPU scheduling algorithm:<\/p>\n<p>&nbsp;<\/p>\n<p>1. CPU utilization \u2013 Keep the CPU as busy as possible. Hence, CPU utilization should be maximum.<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">2. Throughput \u2013 Number of processes that complete their execution per time unit.\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">Throughput should be maximum.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">3. Turnaround time \u2013 amount of time to execute a particular process. This should be as minimum as possible.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">4. Waiting time \u2013 amount of time a process has been waiting in the ready queue.\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">This should be as minimum as possible.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">5. Response time \u2013 amount of time it takes from when a request was submitted\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">until the first response is produced, not output (for time-sharing environment). Response time should be as minimum as possible.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>7.6 Scheduling algorithms\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this section, we will learn two CPU scheduling algorithms, First Come First Served (FCFS) scheduling and Shortest Job First (SJF) scheduling algorithms.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.6.1 First-Come, First-Served (FCFS) Scheduling\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In FCFS, the process that arrived first and requests the CPU first is assigned the CPU first. In FCFS, the ready queue can be maintained as a FIFO queue. When processes arrive they are added to the tail of the queue. When the CPU becomes free, the process in the head of the queue is taken for execution. This is a non-preemptive algorithm. Once a process is assigned the CPU, the process continues to use the CPU till its CPU burst time is completed. When its CPU burst gets over, the process by itself relinquishes the CPU. FCFS is explained with an example given below:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider three processes <em>P<\/em>1<em>, P<\/em>2 and <em>P<\/em>3 with CPU-burst times 24 milliseconds (ms), 3 ms and 3 ms respectively. All three processes arrive at the same time i n the order <em>P<\/em>1, <em>P<\/em>2, <em>P<\/em>3.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Gantt chart that shows the order i n which processes are assigned the CPU is shown below. Since process <em>P<\/em>1 is the first process in the queue, <em>P<\/em>1\u00a0 is assigned the CPU first. <em>P<\/em>1 has a CPU-burst length of 24 ms. <em>P<\/em>1 uses the CPU for 24 ms and then relinquishes the CPU as shown in Figure 7.4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-65 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1.png\" alt=\"\" width=\"335\" height=\"76\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1.png 775w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-300x68.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-768x174.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-65x15.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-225x51.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-350x79.png 350w\" sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.4 Process <em>P<\/em>1 using the CPU<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next process i n the queue is <em>P<\/em>2. <em>P<\/em>2 is scheduled next. <em>P<\/em>2 has a CPU-burst time of 3 ms and uses the CPU till 27 ms as shown in Figure 7.5.<\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-64 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2.png\" alt=\"\" width=\"350\" height=\"86\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2.png 841w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2-300x73.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2-768x188.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2-65x16.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2-225x55.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P2-350x86.png 350w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.5 Process <em>P<\/em>2 using the CPU<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After <em>P<\/em>2, <em>P<\/em>3 is the only process in the queue and it is assigned the CPU. <em>P<\/em>3 uses the CPU for a time duration of 3 ms and then relinquishes the CPU as shown i n Figure 7.6.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-63 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3.png\" alt=\"\" width=\"374\" height=\"84\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3.png 836w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-300x67.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-768x173.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-65x15.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-225x51.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-350x79.png 350w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.4 Process <em>P<\/em>3 using the CPU<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The waiting time for each process and hence the average waiting time can be calculated. Since all the three processes arrive at the same time, the arrival time of the processes is taken as 0. Process <em>P<\/em>1 arrived at time 0 and was assigned the CPU immediately. Hence, the waiting time for <em>P<\/em>1 = 0. Process <em>P<\/em>2 arrived at time 0 and was assigned the CPU at time 24. Hence, the waiting time for <em>P<\/em>2 = 24. Process P3 arrived at time 0 and was assigned the CPU at time 27. Hence, the waiting time for <em>P<\/em>3 = 27. Hence, the average waiting time is (0 + 24 + 27) \/ 3 = 17.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Similarly, the turnaround time for each process and hence the average turnaround time can be calculated. Process <em>P<\/em>1 arrived at time 0 and completed its CPU burst at time 24. Hence, the turnaround time for <em>P<\/em>1 = 24. Process <em>P<\/em>2 arrived at time 0 and completed its CPU burst at time 27. Hence, the turnaround time for <em>P<\/em>2 = 27. Process <em>P<\/em>3 arrived at time 0 and completed its CPU burst at time 30. Hence, the turnaround time for <em>P<\/em>3 = 30.The average turnaround time is (24 + 27 + 30) \/ 3 = 27.<\/p>\n<p>&nbsp;<\/p>\n<p>Suppose if the three processes arrive in the order <em>P<\/em>2, <em>P<\/em>3, <em>P<\/em>1. The Gantt chart for the schedule is as shown in Figure 7.5.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-62 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with.png\" alt=\"\" width=\"389\" height=\"71\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with.png 751w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with-300x55.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with-65x12.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with-225x41.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Gantt-chart-with-350x64.png 350w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.5 Gantt chart with the order of the processes changed<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The waiting time of the processes can be calculated as follows: The arrival time for all the processes is the same and is taken as 0. Therefore, the waiting time for <em>P<\/em>1 <em>= <\/em>6<em>, P<\/em>2 = 0 and <em>P<\/em>3 <em>= <\/em>3. The average waiting time is (6 + 0 + 3)\/3 = 3. The turnaround time for <em>P<\/em>1 <em>= <\/em>30<em>, P<\/em>2 = 3 and <em>P<\/em>3 <em>= <\/em>6. Therefore, the average turnaround time is (3 + 6 + 30)\/3 = 13. It is seen that the average waiting time and the average turnaround time have improved when the order of the processes is changed. In the earlier case, process <em>P<\/em>1 had a long\u00a0<span style=\"font-size: 1em;text-align: initial\">CPU burst and the other processes had to wait for <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 to complete its CPU burst. This increased the average waiting time and the average turnaround time.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>7.6.2\u00a0 Shortest-Job-First (SJF) Scheduling\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The SJF CPU scheduling algorithm associates with each process the length of its next CPU burst. These lengths are used to schedule the process with the shortest time. When the CPU becomes available, the process with the shortest next CPU burst is assigned the CPU. There are two schemes i n SJF. One is non-preemptive in which once CPU is assigned to a process, it cannot be preempted until it completes its CPU burst. The other is preemptive in which if a new process arrives with CPU burst length less than the remaining time of the currently executing process, the currently executing process is preempted. This scheme is known as the Shortest-Remaining-Time-First (SRTF). In this module, we will learn how scheduling is done i n non-preemptive SJF.<\/p>\n<p>&nbsp;<\/p>\n<p>Consider four processes <em>P<\/em>1<em>, P<\/em>2<em>, P<\/em>3\u00a0 and <em>P<\/em>4<em>. <\/em>The arri val times and the CPU-burst times of the four processes are gi ven below:<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Process<\/strong><\/td>\n<td><strong>Arriva l Time<\/strong><\/td>\n<td><strong>Burst Time<\/strong><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>1<\/td>\n<td>0.0<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>2<\/td>\n<td>2.0<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>3<\/td>\n<td>4.0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>4<\/td>\n<td>5.0<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">At time 0.0, <em>P<\/em>1 is the only process that has arrived. Hence , <em>P<\/em>1 is assigned the CPU first. Since it is non-preemptive SJF scheduling, <em>P<\/em>1 continues to use the CPU till its CPU-burst gets over as shown in Figure 7.6<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-61 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU.png\" alt=\"\" width=\"406\" height=\"84\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU.png 802w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU-300x62.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU-768x159.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU-225x47.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P1-using-the-CPU-350x72.png 350w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.6 Process <em>P<\/em>1 using the CPU<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After <em>P<\/em>1 finishes its CPU-burst, the processes remaining i n the ready queue are <em>P<\/em>2, <em>P<\/em>3 and <em>P<\/em>4. The processes <em>P<\/em>2, <em>P<\/em>3\u00a0 and <em>P<\/em>4 have now arrived. Of these three processes, <em>P<\/em>3 has the shortest CPU burst. Hence, <em>P<\/em>3 is assigned the CPU. <em>P<\/em>3 makes use of the CPU for a time duration that is equal to its CPU burst (1 ms) and then relinquishes the CPU as in Figure 7.7.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-60 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU.png\" alt=\"\" width=\"409\" height=\"91\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU.png 794w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU-300x67.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU-768x171.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU-65x14.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU-225x50.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Process-P3-using-the-CPU-350x78.png 350w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.7 Process <em>P<\/em>3usingtheCPU<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 and <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">4 are the remaining processes. Both the processes have the same CPU burst time. Any one of these can be assigned the CPU. So, the tie is now broken using FCFS. Of the two processes, <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 arrived earlier. Hence, <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 is assigned the CPU and <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">2 executes till its CPU burst is completed as i n Figure 7.8.<\/span><\/p>\n<div style=\"text-align: justify\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-59 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU.png\" alt=\"\" width=\"404\" height=\"81\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU.png 827w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU-300x60.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU-768x154.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU-225x45.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P2-using-the-CPU-350x70.png 350w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.8 <em>P<\/em>2 using the CPU<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After <em>P<\/em>2 finishes execution, the only process waiting is <em>P<\/em>4. <em>P<\/em>4 is now assigned the CPU. After <em>P<\/em>4 runs for its CPU-burst time, it relinquishes the CPU as shown i n Figure 7.9.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-58 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU.png\" alt=\"\" width=\"417\" height=\"85\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU.png 815w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU-300x61.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU-768x156.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU-225x46.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/P4-using-the-CPU-350x71.png 350w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 7.9 <em>P<\/em>4 using the CPU<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The average waiting time and the average turnaround time can be calculated as follows. The waiting time for process <em>P<\/em>1 is 0. <em>P<\/em>1 arrived at time 0 and was assigned the CPU immediately. <em>P<\/em>1 did not wait. <em>P<\/em>2 arrived at time 2.0 and was assigned the CPU at time 8.0. Hence, the waiting time for process <em>P<\/em>2 is 8 &#8211; 2 = 6. <em>P<\/em>3 arrived at time 4.0 and was assigned the CPU at time 7.0. Hence, the waiting time for process <em>P<\/em>2 is 7 &#8211; 4 = 3. Process <em>P<\/em>4 arrived at time 5.0 and was assigned the CPU at time 12.0. Hence the waiting time of process <em>P<\/em>2 is 12 &#8211; 5 = 7. Hence, the average waiting time = (0 + (8 &#8211; 2) + (7 &#8211; 4) + (12 &#8211; 5)) \/ 4 = 4.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><em>P<\/em>1 arrived at time 0 and completed execution at 7. The turnaround time of process <em>P<\/em>1 is 7. <em>P<\/em>2 arrived at time 2.0 and completed execution at time 12.0. Hence, the turnaround time for process <em>P<\/em>2 is 12 &#8211; 2 = 10. <em>P<\/em>3 arrived at time 4.0 and completed execution at time 8.0. Hence , the waiting time for process <em>P<\/em>2 is 8 &#8211; 4 = 4. Process <em>P<\/em>4 arrived at time 5.0 and completed execution at time 16.0. Hence , the turnaround time of process <em>P<\/em>2 is 16 &#8211; 5 = 11. Hence the a verage waiting time = (0 + (8 &#8211; 2) + (7 &#8211; 4) + (12 &#8211; 5)) \/ 4 = 4. Hence, the average turnaround time = ((7 &#8211; 0) + (12 &#8211; 2) + (8 &#8211; 4) + (16 &#8211; 5)) \/\u00a0 4 = 32 \/ 4 =8.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.7\u00a0 Summary\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module, we learnt what CPU scheduling is. Multiprogramming maximizes CPU utilization. The CPU scheduler selects the next process that should be assigned the \u00a0CPU. \u00a0Many CPU scheduling algorithms \u00a0have \u00a0been proposed. CPU scheduling\u00a0<span style=\"text-align: justify;font-size: 1em\">algorithms can be preemptive or non-preemptive. In this module, we learnt how First Come First Served (FCFS) and non-preemptive Shortest Job First (SJF) CPU scheduling algorithms work. We saw that SJF has less waiting time and less turnaround time when compared to FCFS.<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify\">Abraham Silberschatz, \u00a0Peter \u00a0B. \u00a0Galvin, \u00a0Greg \u00a0Gagne, \u00a0\u201cOperating \u00a0System Concepts\u201d, Sixth Edition, John Wiley &amp; Sons Inc., 2003.<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":4,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-mary-anitha-rajam"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-54","chapter","type-chapter","status-publish","hentry","contributor-dr-mary-anitha-rajam"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/54\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/54\/revisions\/407"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/54\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapter-type?post=54"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/contributor?post=54"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/license?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}