{"id":28015,"date":"2017-09-06T00:15:10","date_gmt":"2017-09-05T18:45:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28015"},"modified":"2017-09-06T00:20:18","modified_gmt":"2017-09-05T18:50:18","slug":"program-priority-scheduling-set-1","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/program-priority-scheduling-set-1\/","title":{"rendered":"Program for Priority Scheduling | Set 1"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. Each process is assigned a priority. Process with the highest priority is to be executed first and so on.<br \/>\nProcesses with the same priority are executed on first come first served basis. Priority can be decided based on memory requirements, time requirements or any other resource requirement.<\/p>\n<p><strong>Implementation :<\/strong><\/p>\n<pre>1- First input the processes with their burst time \r\n   and priority.\r\n2- Sort the processes, burst time and priority\r\n   according to the priority.\r\n3- Now simply apply FCFS algorithm.<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-28040\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior.png\" alt=\"Program for Priority Scheduling | Set 1\" width=\"1170\" height=\"251\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior.png 1170w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior-300x64.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior-768x165.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior-1024x220.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior-990x212.png 990w\" sizes=\"(max-width: 1170px) 100vw, 1170px\" \/> <img decoding=\"async\" class=\"aligncenter size-full wp-image-28041\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior2.png\" alt=\"\" width=\"1300\" height=\"174\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior2.png 1300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior2-300x40.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior2-768x103.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior2-1024x137.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/prior2-990x133.png 990w\" sizes=\"(max-width: 1300px) 100vw, 1300px\" \/><\/p>\n<p>Note: A major problem with priority scheduling is indefinite blocking or starvation. A solution to the problem of indefinite blockage of the low-priority process is aging. Aging is a technique of gradually increasing the priority of processes that wait in the system for a long period of time.<\/p>\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<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>struct Process<br\/>{<br\/>    int pid;  \/\/ Process ID<br\/>    int bt;   \/\/ CPU Burst time required<br\/>    int priority; \/\/ Priority of this process<br\/>};<br\/> <br\/>\/\/ Function to sort the Process acc. to priority<br\/>bool comparison(Process a, Process b)<br\/>{<br\/>    return (a.priority &gt; b.priority);<br\/>}<br\/> <br\/>\/\/ Function to find the waiting time for all<br\/>\/\/ processes<br\/>void findWaitingTime(Process proc[], int n,<br\/>                     int wt[])<br\/>{<br\/>    \/\/ waiting time for first process is 0<br\/>    wt[0] = 0;<br\/> <br\/>    \/\/ calculating waiting time<br\/>    for (int  i = 1; i &lt; n ; i++ )<br\/>        wt[i] =  proc[i-1].bt + wt[i-1] ;<br\/>}<br\/> <br\/>\/\/ Function to calculate turn around time<br\/>void findTurnAroundTime( Process proc[], int n,<br\/>                         int wt[], int tat[])<br\/>{<br\/>    \/\/ calculating turnaround time by adding<br\/>    \/\/ bt[i] + wt[i]<br\/>    for (int  i = 0; i &lt; n ; i++)<br\/>        tat[i] = proc[i].bt + wt[i];<br\/>}<br\/> <br\/>\/\/Function to calculate average time<br\/>void findavgTime(Process proc[], int n)<br\/>{<br\/>    int wt[n], tat[n], total_wt = 0, total_tat = 0;<br\/> <br\/>    \/\/Function to find waiting time of all processes<br\/>    findWaitingTime(proc, n, wt);<br\/> <br\/>    \/\/Function to find turn around time for all processes<br\/>    findTurnAroundTime(proc, n, wt, tat);<br\/> <br\/>    \/\/Display processes along with all details<br\/>    cout &lt;&lt; &quot;\\nProcesses  &quot;&lt;&lt; &quot; Burst time  &quot;<br\/>         &lt;&lt; &quot; Waiting time  &quot; &lt;&lt; &quot; Turn around time\\n&quot;;<br\/> <br\/>    \/\/ Calculate total waiting time and total turn<br\/>    \/\/ around time<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\/>        cout &lt;&lt; &quot;   &quot; &lt;&lt; proc[i].pid &lt;&lt; &quot;\\t\\t&quot;<br\/>             &lt;&lt; proc[i].bt &lt;&lt; &quot;\\t    &quot; &lt;&lt; wt[i]<br\/>             &lt;&lt; &quot;\\t\\t  &quot; &lt;&lt; tat[i] &lt;&lt;endl;<br\/>    }<br\/> <br\/>    cout &lt;&lt; &quot;\\nAverage 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\/>void priorityScheduling(Process proc[], int n)<br\/>{<br\/>    \/\/ Sort processes by priority<br\/>    sort(proc, proc + n, comparison);<br\/> <br\/>    cout&lt;&lt; &quot;Order in which processes gets executed \\n&quot;;<br\/>    for (int  i = 0 ; i &lt;  n; i++)<br\/>        cout &lt;&lt; proc[i].pid &lt;&lt;&quot; &quot; ;<br\/> <br\/>    findavgTime(proc, n);<br\/>}<br\/> <br\/>\/\/ Driver code<br\/>int main()<br\/>{<br\/>    Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};<br\/>    int n = sizeof proc \/ sizeof proc[0];<br\/>    priorityScheduling(proc, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>[ad type=&#8221;banner&#8221;]<\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>Order in which processes gets executed \r\n1 3 2 \r\nProcesses  Burst time  Waiting time  Turn around time\r\n 1\t\t10\t 0\t\t 10\r\n 3\t\t8\t 10\t\t 18\r\n 2\t\t5\t 18\t\t 23\r\n\r\nAverage waiting time = 9.33333\r\nAverage turn around time = 17\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Program for Priority Scheduling | Set  1-  Operating System &#8211; Priority scheduling is a non-preemptive algorithm and one of the most common scheduling<\/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":[82523,82521,82522,82525,82520,82524,82469],"class_list":["post-28015","post","type-post","status-publish","format-standard","hentry","category-cs-subjects","category-operating-systems","tag-algorithm-for-priority-scheduling","tag-c-program-for-round-robin-scheduling","tag-non-preemptive-priority-scheduling-program-in-c-with-arrival-time","tag-preemptive-sjf-scheduling-program-in-c","tag-priority-scheduling-program-in-c-with-arrival-time","tag-priority-scheduling-program-in-c","tag-sjf-scheduling-program-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28015","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=28015"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28015\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}