{"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>\u00a0<\/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=\u201dbanner\u201d]\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=\u201dbanner\u201d]\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20for%20implementation%20of%20FCFS%0A%2F%2F%20scheduling%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Astruct%20Process%0A%7B%0A%20%20%20%20int%20pid%3B%20%20%2F%2F%20Process%20ID%0A%20%20%20%20int%20bt%3B%20%20%20%2F%2F%20CPU%20Burst%20time%20required%0A%20%20%20%20int%20priority%3B%20%2F%2F%20Priority%20of%20this%20process%0A%7D%3B%0A%20%0A%2F%2F%20Function%20to%20sort%20the%20Process%20acc.%20to%20priority%0Abool%20comparison(Process%20a%2C%20Process%20b)%0A%7B%0A%20%20%20%20return%20(a.priority%20%3E%20b.priority)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20the%20waiting%20time%20for%20all%0A%2F%2F%20processes%0Avoid%20findWaitingTime(Process%20proc%5B%5D%2C%20int%20n%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20wt%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20waiting%20time%20for%20first%20process%20is%200%0A%20%20%20%20wt%5B0%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20calculating%20waiting%20time%0A%20%20%20%20for%20(int%20%20i%20%3D%201%3B%20i%20%3C%20n%20%3B%20i%2B%2B%20)%0A%20%20%20%20%20%20%20%20wt%5Bi%5D%20%3D%20%20proc%5Bi-1%5D.bt%20%2B%20wt%5Bi-1%5D%20%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20calculate%20turn%20around%20time%0Avoid%20findTurnAroundTime(%20Process%20proc%5B%5D%2C%20int%20n%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20wt%5B%5D%2C%20int%20tat%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20calculating%20turnaround%20time%20by%20adding%0A%20%20%20%20%2F%2F%20bt%5Bi%5D%20%2B%20wt%5Bi%5D%0A%20%20%20%20for%20(int%20%20i%20%3D%200%3B%20i%20%3C%20n%20%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20tat%5Bi%5D%20%3D%20proc%5Bi%5D.bt%20%2B%20wt%5Bi%5D%3B%0A%7D%0A%20%0A%2F%2FFunction%20to%20calculate%20average%20time%0Avoid%20findavgTime(Process%20proc%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20wt%5Bn%5D%2C%20tat%5Bn%5D%2C%20total_wt%20%3D%200%2C%20total_tat%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2FFunction%20to%20find%20waiting%20time%20of%20all%20processes%0A%20%20%20%20findWaitingTime(proc%2C%20n%2C%20wt)%3B%0A%20%0A%20%20%20%20%2F%2FFunction%20to%20find%20turn%20around%20time%20for%20all%20processes%0A%20%20%20%20findTurnAroundTime(proc%2C%20n%2C%20wt%2C%20tat)%3B%0A%20%0A%20%20%20%20%2F%2FDisplay%20processes%20along%20with%20all%20details%0A%20%20%20%20cout%20%3C%3C%20%22%5CnProcesses%20%20%22%3C%3C%20%22%20Burst%20time%20%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20%22%20Waiting%20time%20%20%22%20%3C%3C%20%22%20Turn%20around%20time%5Cn%22%3B%0A%20%0A%20%20%20%20%2F%2F%20Calculate%20total%20waiting%20time%20and%20total%20turn%0A%20%20%20%20%2F%2F%20around%20time%0A%20%20%20%20for%20(int%20%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20total_wt%20%3D%20total_wt%20%2B%20wt%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20total_tat%20%3D%20total_tat%20%2B%20tat%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22%20%20%20%22%20%3C%3C%20proc%5Bi%5D.pid%20%3C%3C%20%22%5Ct%5Ct%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%3C%20proc%5Bi%5D.bt%20%3C%3C%20%22%5Ct%20%20%20%20%22%20%3C%3C%20wt%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%3C%20%22%5Ct%5Ct%20%20%22%20%3C%3C%20tat%5Bi%5D%20%3C%3Cendl%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnAverage%20waiting%20time%20%3D%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20(float)total_wt%20%2F%20(float)n%3B%0A%20%20%20%20cout%20%3C%3C%20%22%5CnAverage%20turn%20around%20time%20%3D%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20(float)total_tat%20%2F%20(float)n%3B%0A%7D%0A%20%0Avoid%20priorityScheduling(Process%20proc%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Sort%20processes%20by%20priority%0A%20%20%20%20sort(proc%2C%20proc%20%2B%20n%2C%20comparison)%3B%0A%20%0A%20%20%20%20cout%3C%3C%20%22Order%20in%20which%20processes%20gets%20executed%20%5Cn%22%3B%0A%20%20%20%20for%20(int%20%20i%20%3D%200%20%3B%20i%20%3C%20%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20proc%5Bi%5D.pid%20%3C%3C%22%20%22%20%3B%0A%20%0A%20%20%20%20findavgTime(proc%2C%20n)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main()%0A%7B%0A%20%20%20%20Process%20proc%5B%5D%20%3D%20%7B%7B1%2C%2010%2C%202%7D%2C%20%7B2%2C%205%2C%200%7D%2C%20%7B3%2C%208%2C%201%7D%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof%20proc%20%2F%20sizeof%20proc%5B0%5D%3B%0A%20%20%20%20priorityScheduling(proc%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>[ad type=\u201dbanner\u201d]<\/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}]}}