{"id":63,"date":"2025-01-08T03:42:23","date_gmt":"2025-01-08T03:42:23","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=63"},"modified":"2025-12-06T07:02:15","modified_gmt":"2025-12-06T07:02:15","slug":"sjf-algorithm","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=63","title":{"rendered":"SJF (algorithm)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>Example of Non-Preemptive SJF<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Given Processes:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Process<\/th><th>Arrival Time<\/th><th>Burst Time<\/th><\/tr><\/thead><tbody><tr><td>P1<\/td><td>0<\/td><td>7<\/td><\/tr><tr><td>P2<\/td><td>2<\/td><td>4<\/td><\/tr><tr><td>P3<\/td><td>4<\/td><td>1<\/td><\/tr><tr><td>P4<\/td><td>5<\/td><td>4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Order of Execution<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>At time <code>t=0<\/code>, only <strong>P1<\/strong> is available, so it starts execution.<\/li>\n\n\n\n<li>At time <code>t=2<\/code>, <strong>P2<\/strong> arrives, but <strong>P1<\/strong> continues execution because this is non-preemptive SJF.<\/li>\n\n\n\n<li>At time <code>t=4<\/code>, <strong>P3<\/strong> arrives, and after <strong>P1<\/strong> completes, <strong>P3<\/strong> (shortest burst time) is executed.<\/li>\n\n\n\n<li>At time <code>t=5<\/code>, <strong>P4<\/strong> arrives, and <strong>P2<\/strong> is scheduled next after <strong>P3<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Gantt Chart:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Time<\/th><th>0 &#8211; 7<\/th><th>7 &#8211; 8<\/th><th>8 &#8211; 12<\/th><th>12 &#8211; 16<\/th><\/tr><\/thead><tbody><tr><td>P<\/td><td>P1<\/td><td>P3<\/td><td>P2<\/td><td>P4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Calculations<\/h4>\n\n\n\n<p><strong>Turnaround Time (TAT)<\/strong> = Completion Time &#8211; Arrival Time<br><strong>Waiting Time (WT)<\/strong> = Turnaround Time &#8211; Burst Time<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Process<\/th><th>Arrival Time<\/th><th>Burst Time<\/th><th>Completion Time<\/th><th>TAT<\/th><th>WT<\/th><\/tr><\/thead><tbody><tr><td>P1<\/td><td>0<\/td><td>7<\/td><td>7<\/td><td>7<\/td><td>0<\/td><\/tr><tr><td>P2<\/td><td>2<\/td><td>4<\/td><td>12<\/td><td>10<\/td><td>6<\/td><\/tr><tr><td>P3<\/td><td>4<\/td><td>1<\/td><td>8<\/td><td>4<\/td><td>3<\/td><\/tr><tr><td>P4<\/td><td>5<\/td><td>4<\/td><td>16<\/td><td>11<\/td><td>7<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Averages<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Average Turnaround Time (TAT)<\/strong> = 7+10+4+114=8\\frac{7 + 10 + 4 + 11}{4} = 847+10+4+11\u200b=8<\/li>\n\n\n\n<li><strong>Average Waiting Time (WT)<\/strong> = 0+6+3+74=4\\frac{0 + 6 + 3 + 7}{4} = 440+6+3+7\u200b=4<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Advantages of SJF<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Minimizes average waiting time.<\/li>\n\n\n\n<li>Optimal for systems where short tasks dominate.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Disadvantages of SJF<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Difficult to predict the exact burst time of a process.<\/li>\n\n\n\n<li>Starvation of longer processes if shorter ones keep arriving.<\/li>\n<\/ol>\n\n\n\n<p><strong>Example -2:<\/strong>Below is the full solution for <strong>SJF (Shortest Job First) Non-Preemptive<\/strong> scheduling for the given processes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Given Processes<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Process<\/th><th>Arrival Time<\/th><th>Burst Time<\/th><\/tr><\/thead><tbody><tr><td><strong>P1<\/strong><\/td><td>0<\/td><td>5<\/td><\/tr><tr><td><strong>P2<\/strong><\/td><td>2<\/td><td>3<\/td><\/tr><tr><td><strong>P3<\/strong><\/td><td>3<\/td><td>2<\/td><\/tr><tr><td><strong>P4<\/strong><\/td><td>1<\/td><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"> <strong>SJF (Non-Preemptive) Scheduling<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Sort by arrival, choose shortest burst among available processes<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Timeline Execution<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>At time = 0<\/strong><\/h3>\n\n\n\n<p>Available: P1<br>\u2192 Run <strong>P1<\/strong> (burst 5) until completion (non-preemptive)<\/p>\n\n\n\n<p><strong>P1 runs from 0 \u2192 5<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>At time = 5<\/strong><\/h3>\n\n\n\n<p>Now available: P2 (3), P3 (2), P4 (3)<br>Shortest = <strong>P3<\/strong><\/p>\n\n\n\n<p><strong>P3 runs from 5 \u2192 7<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>At time = 7<\/strong><\/h3>\n\n\n\n<p>Remaining: P2 (3), P4 (3)<br>Tie \u2192 choose the one that arrived first \u2192 <strong>P4<\/strong><\/p>\n\n\n\n<p><strong>P4 runs from 7 \u2192 10<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>At time = 10<\/strong><\/h3>\n\n\n\n<p>Last: P2 (3)<\/p>\n\n\n\n<p><strong>P2 runs from 10 \u2192 13<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"> <strong>Gantt Chart<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>|  P1  | P3 |  P4  |  P2  |\n0      5    7     10     13\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Completion, Turnaround, and Waiting Times<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Process<\/th><th>AT<\/th><th>BT<\/th><th>CT<\/th><th>TAT = CT-AT<\/th><th>WT = TAT-BT<\/th><\/tr><\/thead><tbody><tr><td><strong>P1<\/strong><\/td><td>0<\/td><td>5<\/td><td>5<\/td><td>5<\/td><td>0<\/td><\/tr><tr><td><strong>P3<\/strong><\/td><td>3<\/td><td>2<\/td><td>7<\/td><td>4<\/td><td>2<\/td><\/tr><tr><td><strong>P4<\/strong><\/td><td>1<\/td><td>3<\/td><td>10<\/td><td>9<\/td><td>6<\/td><\/tr><tr><td><strong>P2<\/strong><\/td><td>2<\/td><td>3<\/td><td>13<\/td><td>11<\/td><td>8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"> <strong>Average Times<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Average Turnaround Time<\/strong><\/h3>\n\n\n\n<p>5+4+9+11\/=4  29\/4 \u200b<\/p>\n\n\n\n<p>=7.25\u00a0ms<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Average Waiting Time<\/strong><\/h3>\n\n\n\n<p>0+2+6+8\/4=16\/4= 4 ms<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 <strong>Final Answer Summary<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Gantt Chart:<\/strong><\/h3>\n\n\n\n<p><code>0\u20135(P1), 5\u20137(P3), 7\u201310(P4), 10\u201313(P2)<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Average Waiting Time = 4 ms<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Average Turnaround Time = 7.25 ms<\/strong><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Example of Non-Preemptive SJF Given Processes: Process Arrival Time Burst Time P1 0 7 P2 2 4 P3 4 1 P4 5 4 Step 1: Order of Execution Gantt Chart: Time 0 &#8211; 7 7 &#8211; 8 8 &#8211; 12 12 &#8211; 16 P P1 P3 P2 P4 Step 2: Calculations Turnaround Time (TAT) = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63"}],"version-history":[{"count":2,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":353,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions\/353"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}