{"id":27974,"date":"2017-09-05T23:59:22","date_gmt":"2017-09-05T18:29:22","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27974"},"modified":"2017-09-05T23:59:22","modified_gmt":"2017-09-05T18:29:22","slug":"fcfs-scheduling-set-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/fcfs-scheduling-set-2\/","title":{"rendered":"FCFS Scheduling | Set 2"},"content":{"rendered":"<p>We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenario when processes have different arrival times are discussed. Given n processes with their burst times and arrival times, the task is to find average waiting time and average turn around time using FCFS scheduling algorithm.<br \/>\nFIFO simply queues processes in the order they arrive in the ready queue. Here, the process that comes first will be executed first and next process will start only after the previous gets fully executed.<\/p>\n<ol>\n<li>Completion Time: Time at which process completes its execution.<\/li>\n<li>Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time \u2013 Arrival Time<\/li>\n<li>Waiting Time(W.T): Time Difference between turn around time and burst time.<br \/>\nWaiting Time = Turn Around Time \u2013 Burst Time<\/li>\n<\/ol>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27998\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ff2_.png\" alt=\"FCFS Scheduling | Set 2\" width=\"1217\" height=\"341\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ff2_.png 1217w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ff2_-300x84.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ff2_-768x215.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ff2_-1024x287.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ff2_-990x277.png 990w\" sizes=\"(max-width: 1217px) 100vw, 1217px\" \/>[ad type=&#8221;banner&#8221;]\n<pre>\r\nProcess     Wait Time : Service Time - Arrival Time\r\n   P0 \t                   0 - 0   = 0\r\n   P1 \t                   5 - 1   = 4\r\n   P2 \t                   8 - 2   = 6\r\n   P3 \t                   16 - 3  = 13\r\n\r\nAverage Wait Time: (0 + 4 + 6 + 13) \/ 4 = 5.75\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Service Time :<\/strong> Service time means amount of time after which a process can start execution. It is summation of burst time of previous processes (Processes that came before)<\/p>\n<p><strong>Changes in code as compare to code of FCFS with same arrival time:<\/strong><br \/>\nTo find waiting time: Time taken by all processes before the current process to be started (i.e. burst time of all previous processes) \u2013 arrival time of current process<br \/>\n<strong>wait_time[i] = (bt[0] + bt[1] +\u2026\u2026 bt[i-1] ) \u2013 arrival_time[i]<\/strong><\/p>\n<p><b>Implementation:<\/b><\/p>\n<pre>1- Input the processes along with their burst time(bt)\r\n   and arrival time(at)\r\n2- Find waiting time for all other processes i.e. for\r\n   a given process  i:\r\n       wt[i] = (bt[0] + bt[1] +...... bt[i-1]) - at[i] \r\n3- Now find <strong>turn around time <\/strong>\r\n          = waiting_time + burst_time for all processes\r\n4- <strong>Average waiting time<\/strong> = \r\n                    total_waiting_time \/ no_of_processes\r\n5- <strong>Average turn around time<\/strong> = \r\n                 total_turn_around_time \/ no_of_processes<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program for implementation of FCFS<br\/>\/\/ scheduling with different arrival time<br\/>#include&lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Function to find the waiting time for all<br\/>\/\/ processes<br\/>void findWaitingTime(int processes[], int n, int bt[],<br\/>                                   int wt[], int at[])<br\/>{<br\/>    int service_time[n];<br\/>    service_time[0] = 0;<br\/>    wt[0] = 0;<br\/> <br\/>    \/\/ calculating waiting time<br\/>    for (int i = 1; i &lt; n ; i++)<br\/>    {<br\/>        \/\/ Add burst time of previous processes<br\/>        service_time[i] = service_time[i-1] + bt[i-1];<br\/> <br\/>        \/\/ Find waiting time for current process =<br\/>        \/\/ sum - at[i]<br\/>        wt[i] = service_time[i] - at[i];<br\/> <br\/>        \/\/ If waiting time for a process is in negative<br\/>        \/\/ that means it is already in the ready queue<br\/>        \/\/ before CPU becomes idle so its waiting time is 0<br\/>        if (wt[i] &lt; 0)<br\/>            wt[i] = 0;<br\/>    }<br\/>}<br\/> <br\/>\/\/ Function to calculate turn around time<br\/>void findTurnAroundTime(int processes[], int n, int bt[],<br\/>                                      int wt[], int tat[])<br\/>{<br\/>    \/\/ Calculating turnaround time by adding bt[i] + wt[i]<br\/>    for (int i = 0; i &lt; n ; i++)<br\/>        tat[i] = bt[i] + wt[i];<br\/>}<br\/> <br\/>\/\/ Function to calculate average waiting and turn-around<br\/>\/\/ times.<br\/>void findavgTime(int processes[], int n, int bt[], int at[])<br\/>{<br\/>    int wt[n], tat[n];<br\/> <br\/>    \/\/ Function to find waiting time of all processes<br\/>    findWaitingTime(processes, n, bt, wt, at);<br\/> <br\/>    \/\/ Function to find turn around time for all processes<br\/>    findTurnAroundTime(processes, n, bt, wt, tat);<br\/> <br\/>    \/\/ Display processes along with all details<br\/>    cout &lt;&lt; &quot;Processes &quot; &lt;&lt; &quot; Burst Time &quot; &lt;&lt; &quot; Arrival Time &quot;<br\/>         &lt;&lt; &quot; Waiting Time &quot; &lt;&lt; &quot; Turn-Around Time &quot;<br\/>         &lt;&lt; &quot; Completion Time \\n&quot;;<br\/>    int total_wt = 0, total_tat = 0;<br\/>    for (int i = 0 ; i &lt; n ; i++)<br\/>    {<br\/>        total_wt = total_wt + wt[i];<br\/>        total_tat = total_tat + tat[i];<br\/>        int compl_time = tat[i] + at[i];<br\/>        cout &lt;&lt; &quot; &quot; &lt;&lt; i+1 &lt;&lt; &quot;\\t\\t&quot; &lt;&lt; bt[i] &lt;&lt; &quot;\\t\\t&quot;<br\/>             &lt;&lt; at[i] &lt;&lt; &quot;\\t\\t&quot; &lt;&lt; wt[i] &lt;&lt; &quot;\\t\\t &quot;<br\/>             &lt;&lt; tat[i]  &lt;&lt;  &quot;\\t\\t &quot; &lt;&lt; compl_time &lt;&lt; endl;<br\/>    }<br\/> <br\/>    cout &lt;&lt; &quot;Average waiting time = &quot;<br\/>         &lt;&lt; (float)total_wt \/ (float)n;<br\/>    cout &lt;&lt; &quot;\\nAverage turn around time = &quot;<br\/>         &lt;&lt; (float)total_tat \/ (float)n;<br\/>}<br\/> <br\/>\/\/ Driver code<br\/>int main()<br\/>{<br\/>    \/\/ Process id&#039;s<br\/>    int processes[] = {1, 2, 3};<br\/>    int n = sizeof processes \/ sizeof processes[0];<br\/> <br\/>    \/\/ Burst time of all processes<br\/>    int burst_time[] = {5, 9, 6};<br\/> <br\/>    \/\/ Arrival time of all processes<br\/>    int arrival_time[] = {3, 3, 6};<br\/> <br\/>    findavgTime(processes, n, burst_time, arrival_time);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p><strong>[ad type=&#8221;banner&#8221;]<\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>Processes  Burst Time  Arrival Time  Waiting Time  Turn-Around Time  Completion Time \r\n 1\t\t5\t\t3\t\t0\t\t 5\t\t 8\r\n 2\t\t9\t\t3\t\t2\t\t 11\t\t 14\r\n 3\t\t6\t\t6\t\t8\t\t 14\t\t 20\r\nAverage waiting time = 3.33333\r\nAverage turn around time = 10<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>FCFS Scheduling | Set 2 &#8211; Operating System &#8211; We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenario<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71771,82275],"tags":[82462,82440,82471,82437,82473,82464,82477,82472],"class_list":["post-27974","post","type-post","status-publish","format-standard","hentry","category-cs-subjects","category-operating-systems","tag-cpu-scheduling-algorithms","tag-fcfs-scheduling-algorithm","tag-fcfs-scheduling-program-in-c","tag-preemptive-and-nonpreemptive-scheduling","tag-scheduling-algorithms-examples","tag-scheduling-algorithms-in-operating-system-with-examples-pdf","tag-sjf-scheduling","tag-sjf-scheduling-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27974"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27974\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}