<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anna</title>
    <description>The latest articles on DEV Community by Anna (@iamdigitalanna).</description>
    <link>https://dev.to/iamdigitalanna</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F769637%2F1d7f6cf0-90a3-4dbb-8c94-a78d876532dc.jpg</url>
      <title>DEV Community: Anna</title>
      <link>https://dev.to/iamdigitalanna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamdigitalanna"/>
    <language>en</language>
    <item>
      <title>Scheduling Algorithms in Operating Systems - Part 5 - Highest Response Ratio next</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Tue, 07 Dec 2021 12:41:37 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-5-highest-response-ratio-next-5fh1</link>
      <guid>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-5-highest-response-ratio-next-5fh1</guid>
      <description>&lt;h4&gt;
  
  
  You can skip the intro if you have been through the &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a"&gt;&lt;strong&gt;Part - 1&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-2-shortest-job-first-sjf-4if3"&gt;&lt;strong&gt;Part - 2&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-3-round-robin-algorithm-9j9"&gt;&lt;strong&gt;Part - 3&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-4-priority-scheduling-algorithm-558b"&gt;&lt;strong&gt;Part - 4&lt;/strong&gt;&lt;/a&gt; of the series
&lt;/h4&gt;

&lt;p&gt;Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.&lt;/p&gt;

&lt;p&gt;For this purpose, there are many scheduling algorithms like the &lt;strong&gt;First Come First Serve&lt;/strong&gt; (FCFS) algorithm, &lt;strong&gt;Shortest Job First&lt;/strong&gt; (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrival Time&lt;/strong&gt;: The time at which the process arrives in the processor for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Waiting Time&lt;/strong&gt;: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Waiting time = Completion Time- Arrival Time- Service Time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Service Time/ Burst time&lt;/strong&gt;: The time required by the process to complete its execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turnaround Time&lt;/strong&gt;: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
&amp;gt;  &lt;em&gt;Turnaround Time = Service Time + Waiting Time&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let us have a look at the Highest Response Ratio Next algorithm :&lt;/p&gt;

&lt;h2&gt;
  
  
  Highest Response Ratio Next Algorithm:
&lt;/h2&gt;

&lt;p&gt;In this, for execution, we select the process that has the highest response ratio.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Response Ratio =( Waiting Time + Service Time ) / Service time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let us understand this using an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c-WfOogv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AyceYPNq8fQj_i1zl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c-WfOogv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AyceYPNq8fQj_i1zl.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, process A is the only process to enter the processor and hence it starts executing. Meanwhile, B enters the queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7g2r2q9T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A1cF-aLlWyAsWFSHI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7g2r2q9T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A1cF-aLlWyAsWFSHI.png" alt="" width="880" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, since B is the only process to enter the queue, it starts executing next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0wKt_xeO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AtYsErcG9-Lqc_oWj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0wKt_xeO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AtYsErcG9-Lqc_oWj.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now comes the trickier part.&lt;/p&gt;

&lt;p&gt;There are three processes — C, D, E in the queue. How do we decide which one to select out of these?&lt;/p&gt;

&lt;p&gt;It's very easy! We calculate the response ratio for each of them using the formula mentioned earlier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JxQdsOUD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dp35o2lm9b47pooe6bq6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JxQdsOUD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dp35o2lm9b47pooe6bq6.png" alt="" width="685" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, C has the highest response ratio. Hence it will be executed next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RbvwPUdK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A0zo47dku3wO0N5xE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RbvwPUdK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A0zo47dku3wO0N5xE.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have to choose between process D and E. Let us calculate the response ratio for these processes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uHlyA4ZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzj5r28ahuazjdbx3ra8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uHlyA4ZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzj5r28ahuazjdbx3ra8.png" alt="" width="685" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since E has the highest response ratio, it is executed next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oo8_SW0g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AlmMG-70fn2JCWrtt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oo8_SW0g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AlmMG-70fn2JCWrtt.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the only process left is D. Hence, it will be executed finally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CrGejYrY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A9WXSq-QwLJGS3BbD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CrGejYrY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A9WXSq-QwLJGS3BbD.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us now calculate the average waiting time and turnaround time for this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2qhajf_g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AdVw_Tb4WBCpdFe8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2qhajf_g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AdVw_Tb4WBCpdFe8b.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how processes are scheduled using the Highest Response Ratio algorithm.&lt;/p&gt;

&lt;p&gt;Here, the disadvantage is that extra calculation is required. Also, the service time has to be known and the waiting time has to be tracked.&lt;/p&gt;

&lt;p&gt;Hope this helps you!&lt;/p&gt;

&lt;p&gt;Have a nice day 😊!!&lt;/p&gt;

&lt;p&gt;PS : This was the last part of the scheduling algorithms series. Hope all your questions were answered. Still if there's anything you need to ask or add to what I've posted please let me know in the comments.&lt;br&gt;
Thanks!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Scheduling Algorithms in Operating Systems - Part 4 - Priority Scheduling Algorithm</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Tue, 07 Dec 2021 12:29:43 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-4-priority-scheduling-algorithm-558b</link>
      <guid>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-4-priority-scheduling-algorithm-558b</guid>
      <description>&lt;h4&gt;
  
  
  You can skip the intro if you have been through the &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a"&gt;&lt;strong&gt;Part - 1&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-2-shortest-job-first-sjf-4if3"&gt;&lt;strong&gt;Part - 2&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-3-round-robin-algorithm-9j9"&gt;&lt;strong&gt;Part - 3&lt;/strong&gt;&lt;/a&gt; of the series
&lt;/h4&gt;

&lt;p&gt;Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.&lt;/p&gt;

&lt;p&gt;For this purpose, there are many scheduling algorithms like the &lt;strong&gt;First Come First Serve&lt;/strong&gt; (FCFS) algorithm, &lt;strong&gt;Shortest Job First&lt;/strong&gt; (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrival Time&lt;/strong&gt;: The time at which the process arrives in the processor for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Waiting Time&lt;/strong&gt;: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Waiting time = Completion Time- Arrival Time- Service Time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Service Time/ Burst time&lt;/strong&gt;: The time required by the process to complete its execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turnaround Time&lt;/strong&gt;: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
&amp;gt;  &lt;em&gt;Turnaround Time = Service Time + Waiting Time&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let us have a look at the Priority Scheduling algorithm :&lt;/p&gt;

&lt;h2&gt;
  
  
  Priority Scheduling Algorithm:
&lt;/h2&gt;

&lt;p&gt;In this algorithm, every process has a specific priority and is executed according to the given priority.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Non-Preemptive Priority Scheduling Algorithm:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this algorithm, the process of highest priority is completely executed first and then the process with the next highest priority is executed.&lt;/p&gt;

&lt;p&gt;Let's take an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JJEV91Vc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AZ4KmHicp52OpPNnF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JJEV91Vc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AZ4KmHicp52OpPNnF.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we have 5 processes A, B,C, D,E&lt;/p&gt;

&lt;p&gt;Processes A and B enter the processor first. Out of those two processes, process B has a higher priority and hence, process B will be executed first.&lt;/p&gt;

&lt;p&gt;Meanwhile, the other processes are added to the waiting queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tSX6QlZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AWJHklQMTnjHGLOIl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tSX6QlZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AWJHklQMTnjHGLOIl.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, after B completes its execution, the waiting queue is checked. Process D is the process in the waiting queue with the highest priority. Hence, it is executed next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mft0a8q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A4obnRfg4OjchHTUp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mft0a8q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A4obnRfg4OjchHTUp.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, now from the queue, process E has the highest priority and is therefore executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--junu3ABR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2ApNiwuuxW0c4zuW71.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--junu3ABR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2ApNiwuuxW0c4zuW71.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, considering the priority, process C is executed….&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wWHswNYe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2810/0%2A5WC3E8h2uqcSCve6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wWHswNYe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2810/0%2A5WC3E8h2uqcSCve6.png" alt="" width="880" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;followed by the process A.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ivtH-FSO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2810/0%2Aneo2qI9EiRXfgtHn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ivtH-FSO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2810/0%2Aneo2qI9EiRXfgtHn.png" alt="" width="880" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us calculate the average waiting time and turnaround time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r-pXdXeY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2886/0%2ArXptcfjgAH8bTqnu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r-pXdXeY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2886/0%2ArXptcfjgAH8bTqnu.png" alt="" width="880" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;p&gt;The processes of higher priority get preference and are made to execute earlier.&lt;/p&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;p&gt;The processes with lower priority may suffer starvation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Preemptive Scheduling Algorithm:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this algorithm, if a process with a priority higher than that of the process being executed enters, the processor is preempted and that process starts executing.&lt;/p&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DdQ9LtxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AOjk0VZELOhuNG4zo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DdQ9LtxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AOjk0VZELOhuNG4zo.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here at 0ms processes A and B enter the system. B being the process with the highest priority starts executing first….&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m3cZmfaX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AQfEOhxNKiC3d4G1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m3cZmfaX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AQfEOhxNKiC3d4G1j.png" alt="" width="880" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;..but as process D enters the processor since D has a higher priority, it starts executing next and B is added to the queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uFWrrcQP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A6aJWWIwh0f09Y62I.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uFWrrcQP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2A6aJWWIwh0f09Y62I.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now after D completes executing, (D gets completely executed as no other process with higher priority enters in between) B being the process with the highest priority starts executing next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yAkrp8O1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2ArsTStM2SvsZ2RlE7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yAkrp8O1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2ArsTStM2SvsZ2RlE7.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, the process E starts executing&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pwrh8GSx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AerHuzfX9XK7ap6zh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pwrh8GSx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AerHuzfX9XK7ap6zh.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, process C starts executing&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qIUrRkBN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AxL5iloZYCQurrkel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qIUrRkBN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2AxL5iloZYCQurrkel.png" alt="" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, process A starts executing&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zg3ZvbWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2734/0%2AjUjEsCl_6dkEm_6C.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zg3ZvbWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2734/0%2AjUjEsCl_6dkEm_6C.png" alt="" width="880" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us calculate the average waiting time and turnaround time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n6D_RLg7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AjBD8Cpx3d22t5WjJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n6D_RLg7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AjBD8Cpx3d22t5WjJ.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The advantage of this algorithm is that the processes with higher priority do not have to wait, hence they are not starved.&lt;/p&gt;

&lt;p&gt;In the next post, let us have a look at the Highest Response Ratio Next Algorithm.&lt;/p&gt;

&lt;p&gt;Hope this helps you!&lt;/p&gt;

&lt;p&gt;Have a nice day 😊!!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Scheduling Algorithms in Operating Systems - Part 3 - Round Robin Algorithm</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Tue, 07 Dec 2021 12:22:46 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-3-round-robin-algorithm-9j9</link>
      <guid>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-3-round-robin-algorithm-9j9</guid>
      <description>&lt;h4&gt;
  
  
  You can skip the intro if you have been through the &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a"&gt;&lt;strong&gt;Part - 1&lt;/strong&gt;&lt;/a&gt; or &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-2-shortest-job-first-sjf-4if3"&gt;&lt;strong&gt;Part - 2&lt;/strong&gt;&lt;/a&gt; of the series
&lt;/h4&gt;

&lt;p&gt;Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.&lt;/p&gt;

&lt;p&gt;For this purpose, there are many scheduling algorithms like the &lt;strong&gt;First Come First Serve&lt;/strong&gt; (FCFS) algorithm, &lt;strong&gt;Shortest Job First&lt;/strong&gt; (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrival Time&lt;/strong&gt;: The time at which the process arrives in the processor for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Waiting Time&lt;/strong&gt;: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Waiting time = Completion Time- Arrival Time- Service Time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Service Time/ Burst time&lt;/strong&gt;: The time required by the process to complete its execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turnaround Time&lt;/strong&gt;: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
&amp;gt;  &lt;em&gt;Turnaround Time = Service Time + Waiting Time&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let us have a look at the Round Robin algorithm :&lt;/p&gt;

&lt;h2&gt;
  
  
  Round robin algorithm :
&lt;/h2&gt;

&lt;p&gt;In this method, every process is executed for a specific interval or time quantum and after the interval is over the next process to arrive starts executing for the same amount of time.&lt;/p&gt;

&lt;p&gt;Let's take an example.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Consider a time quantum of 4ms&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GOCHGnl---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2ATLP0e6-hqum6yAuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GOCHGnl---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2ATLP0e6-hqum6yAuo.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A being the first process to arrive starts executing first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WUG44EtI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AYvnx8bIp6mGEafyS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WUG44EtI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AYvnx8bIp6mGEafyS.png" alt="" width="880" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though the time quantum is 4ms, A needs 3ms, 1ms less than the time quantum. Hence, it gets completely executed 1ms earlier.&lt;/p&gt;

&lt;p&gt;Now since B is the next process, it starts executing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ffJIskV9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A38DkdbnyloXa-1RT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ffJIskV9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A38DkdbnyloXa-1RT.png" alt="" width="880" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It executes for 4ms and then the execution stops. The next process i.e. C starts executing and B is put back into the waiting queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8PbszoHn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2Al5l3uK9qt6S2C4sg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8PbszoHn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2Al5l3uK9qt6S2C4sg.png" alt="" width="880" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C process executes for 4ms and executes completely. Meanwhile, process E enters the queue.&lt;/p&gt;

&lt;p&gt;Next, process D starts executing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8kE4KW30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A18iH4wpAzmcpOJIp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8kE4KW30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A18iH4wpAzmcpOJIp.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Process D executes for 4ms whereas its required time was 5ms. Hence, it is added back into the queue and the next process to be executed is process B which will execute for 2ms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NJ4RKuhJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2460/0%2AFG3SiMEQQnCZd6Dq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NJ4RKuhJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2460/0%2AFG3SiMEQQnCZd6Dq.png" alt="" width="880" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, process E will be executed for 2ms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gKaSX1O---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2460/0%2AMrFoH8BhtY4Sqb15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gKaSX1O---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2460/0%2AMrFoH8BhtY4Sqb15.png" alt="" width="880" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the only process left in the queue is process D. Hence, it will get executed next for 1ms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VGHc68HC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2460/0%2Ag03QxMVdJSLZ2QvU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VGHc68HC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2460/0%2Ag03QxMVdJSLZ2QvU.png" alt="" width="880" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this way, all processes are executed using the round-robin method.&lt;/p&gt;

&lt;p&gt;Now let's have a look at the average waiting time and turnaround time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--icmED7E6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AGGCyuTyn0Pd99IUw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--icmED7E6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AGGCyuTyn0Pd99IUw.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advantages :&lt;/p&gt;

&lt;p&gt;An effective approach in time sharing or transaction processing time.&lt;/p&gt;

&lt;p&gt;Disadvantages :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Relative treatment of processor bound and I/O bound processes can cause poor performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inefficient use of I/O devices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increase in variance of response time.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the next post, let us have a look at the Priority Scheduling algorithm.&lt;/p&gt;

&lt;p&gt;Hope this helps you!&lt;/p&gt;

&lt;p&gt;Have a nice day 😊!!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Scheduling Algorithms in Operating Systems - Part 2 - Shortest Job First (SJF)</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Tue, 07 Dec 2021 12:16:38 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-2-shortest-job-first-sjf-4if3</link>
      <guid>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-2-shortest-job-first-sjf-4if3</guid>
      <description>&lt;h3&gt;
  
  
  You can skip the intro if you've gone through the &lt;a href="https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a"&gt;&lt;strong&gt;Part - 1&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.&lt;/p&gt;

&lt;p&gt;For this purpose, there are many scheduling algorithms like the &lt;strong&gt;First Come First Serve&lt;/strong&gt; (FCFS) algorithm, &lt;strong&gt;Shortest Job First&lt;/strong&gt; (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrival Time&lt;/strong&gt;: The time at which the process arrives in the processor for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Waiting Time&lt;/strong&gt;: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Waiting time = Completion Time- Arrival Time- Service Time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Service Time/ Burst time&lt;/strong&gt;: The time required by the process to complete its execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turnaround Time&lt;/strong&gt;: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
&amp;gt;  &lt;em&gt;Turnaround Time = Service Time + Waiting Time&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Shortest Job First/ Shortest Process Next :
&lt;/h2&gt;

&lt;p&gt;In this algorithm, the processes that require the least time for execution are executed first.&lt;/p&gt;

&lt;p&gt;Let us first consider&lt;/p&gt;

&lt;h3&gt;
  
  
  Non-Preemptive Shortest Job First Algorithm :
&lt;/h3&gt;

&lt;p&gt;Let's consider this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--32fYk1tM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AB8BtbMPGolaXgeBA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--32fYk1tM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AB8BtbMPGolaXgeBA.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Process A is the first process to enter the processor. So it starts executing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---gBdV5gc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A223JsCF8oc909CuX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---gBdV5gc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A223JsCF8oc909CuX.png" alt="" width="880" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;B is the only process in the queue now, so, it starts executing. As and when the processes arrive, they are added to the waiting queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QXZZ2_08--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A6Y7gj1maQtGCxkrq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QXZZ2_08--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A6Y7gj1maQtGCxkrq.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, in the waiting queue, as we can see process E is the process that will require the least time for execution. Hence, process E starts executing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bKGPz2ff--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AD_SymeeGlIcqhsMZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bKGPz2ff--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AD_SymeeGlIcqhsMZ.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C being the process requiring the least time starts executing next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sBc_yDwD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2Av1P_5giYzyA3E-4X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sBc_yDwD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2Av1P_5giYzyA3E-4X.png" alt="" width="880" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, D starts executing&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ozg9hKDg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2Apo0nXg8M7InraeAn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ozg9hKDg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2510/0%2Apo0nXg8M7InraeAn.png" alt="" width="880" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how the SJF algorithm works.&lt;/p&gt;

&lt;p&gt;Now, let's calculate the waiting and turnaround time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dhzICAgP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2ALinlW7BFaqgEP3xl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dhzICAgP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2ALinlW7BFaqgEP3xl.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advantages :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;No bias for longer processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Improved response time.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Disadvantages :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Increased variability hence reduced predictability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The processing time of the process needs to be known.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Possibility of starvation for longer processes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This waiting and turnaround time can be further improved using the below method:&lt;/p&gt;

&lt;h3&gt;
  
  
  Preemptive Shortest Job First Algorithm (Shortest remaining time first):
&lt;/h3&gt;

&lt;p&gt;In this method, while a process is executing, if a process requiring lesser service time enters, that process is executed first.&lt;/p&gt;

&lt;p&gt;Consider the example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--olW_JK4C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2A8ztnrpqFcXPasdYJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--olW_JK4C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2A8ztnrpqFcXPasdYJ.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A being the process to arrive first starts executing first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z2lycPP4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2ANklvC2bAZq9ZzF9Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z2lycPP4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2ANklvC2bAZq9ZzF9Q.png" alt="" width="880" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, process B arrives at 2ms. Already executing process i.e. process A requires 1ms to complete and B needs 6ms which is greater. Hence, A continues execution.&lt;/p&gt;

&lt;p&gt;Then B starts executing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KRHRZOZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2ARBgQJucUiz_yYOcZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KRHRZOZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2ARBgQJucUiz_yYOcZ.png" alt="" width="880" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But at 4ms, C enters the queue. B requires 5ms to complete its execution whereas C requires 4ms. Hence, C starts executing and B is added to the queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QgzOp_57--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A7mVzZD9kXmi6KDTj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QgzOp_57--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2A7mVzZD9kXmi6KDTj.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While C was executing, the D process entered the queue but since the time required for D to execute was greater than that of process C, processes were not switched.&lt;/p&gt;

&lt;p&gt;Now all the processes are either executed or are in the queue. So now the process requiring minimum time for execution will be executed first. i.e. process E.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fgcUubE6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AXfK_we3E5Bxxp7rr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fgcUubE6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AXfK_we3E5Bxxp7rr.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the queue has processes B and D both requiring 5ms. So any process can be executed next.&lt;/p&gt;

&lt;p&gt;B entered the queue first hence, we will execute B first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qkWfPC1t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2508/0%2AS7vUxWbv-SrRGyfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qkWfPC1t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2508/0%2AS7vUxWbv-SrRGyfl.png" alt="" width="880" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, D is the only process left. Hence, it will get executed next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0QfwIon7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2508/0%2AC3zpGcksSMlXFF4T.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0QfwIon7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2508/0%2AC3zpGcksSMlXFF4T.png" alt="" width="880" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so let us now calculate the average waiting and turnaround time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YVhWQlFz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2Ak7QcHgKYrjb3DVj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YVhWQlFz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2Ak7QcHgKYrjb3DVj6.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advantages :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Improved turnaround time and waiting time than preemptive SJF.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No bias towards longer processes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Disadvantages :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The scheduler must have an estimate of processing time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Risk of starvation of longer processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Elapsed service time must be recorded which increases overhead.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the next part, we'll see the Round Robin Algorithm.&lt;/p&gt;

&lt;p&gt;Hope this helps you!&lt;/p&gt;

&lt;p&gt;Have a nice day!!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>IPv4 Classful Addressing </title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Mon, 06 Dec 2021 22:48:26 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/ipv4-classful-addressing-482n</link>
      <guid>https://dev.to/iamdigitalanna/ipv4-classful-addressing-482n</guid>
      <description>&lt;p&gt;Internet protocol is a set of rules for the transfer of data/communication on the internet.IPv4 is the 4th version of the Internet Protocol.&lt;/p&gt;

&lt;p&gt;The IPv4 uses &lt;strong&gt;32-bit addresses&lt;/strong&gt; and has 4.3 billion addresses. It uses the dotted-decimal representation, for e.g: 192.168.0.2.&lt;/p&gt;

&lt;p&gt;The IPv4 address format is: *&lt;em&gt;8bit.8bit.8bit.8bit *&lt;/em&gt;(adds up to 32bit)&lt;/p&gt;

&lt;p&gt;Now, there are two types of IPv4 addresses — &lt;strong&gt;&lt;em&gt;Classful and Classless&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We will see the &lt;strong&gt;&lt;em&gt;classful addressing&lt;/em&gt;&lt;/strong&gt; method here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classful Addressing:
&lt;/h2&gt;

&lt;p&gt;Basically, there are 5 classes of addresses — A, B, C, D, E.&lt;/p&gt;

&lt;p&gt;Let us now have a look at each of them one by one:&lt;/p&gt;

&lt;h3&gt;
  
  
  Class A:-
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--haJrGDEv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A3ecaXVgQyf9ETZlQWnlf7Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--haJrGDEv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A3ecaXVgQyf9ETZlQWnlf7Q.png" alt="" width="640" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Class A, as you can see above, the 32-bit address, which is divided into four sections of 8 bits each, out of those, the &lt;strong&gt;leading 8 bits&lt;/strong&gt; are used to represent the &lt;strong&gt;network&lt;/strong&gt; and the &lt;strong&gt;trailing 24 bits&lt;/strong&gt; are used to represent the network &lt;strong&gt;host&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example: 125.16.32.64 is a class A address&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, the 1st bit of a class A address is always zero.&lt;/p&gt;

&lt;p&gt;Since 1 bit is fixed out of the 32 bits, there are 31 bits left to represent the address. So, in total, there can be 2³¹ addresses in class A and 2⁷ network ID.&lt;/p&gt;

&lt;p&gt;The class A addresses constitute a total of 50% of the possible addresses.&lt;/p&gt;

&lt;p&gt;And the range for the network address is 0 to 127 (How? → If we put all zeroes in the first octet i.e. 00000000 = 0 and if we consider the last address i.e. put all ones except for the 1st bit which is 0 by default, we get 01111111 = 127).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The first and the last address i.e 0.0.0.0 and 127.255.255.255 are never used since the former is the host address/identifier and the latter is the broadcast address.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Due to this, even though 128 network addresses are available, only 126 out of them can be used.&lt;/p&gt;

&lt;p&gt;The default subnet mask for Class A is *&lt;em&gt;255.0.0.0 *&lt;/em&gt;(i.e. to find the network ID, we just need to logically AND our address with this subnet mask…How interesting :-) ).&lt;/p&gt;

&lt;h3&gt;
  
  
  Class B:-
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wPhN4CqW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADV-T-tyG6q2-N3xwGandYw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wPhN4CqW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADV-T-tyG6q2-N3xwGandYw.png" alt="" width="639" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Class B, the &lt;strong&gt;leading 16 bits&lt;/strong&gt; are used to represent the &lt;strong&gt;network&lt;/strong&gt; and the &lt;strong&gt;trailing 16 bits&lt;/strong&gt; are used to represent the network &lt;strong&gt;host&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example: 136.192.168.64 is a class B address&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, the 1st two bits in the first octet of a class B address are always 10.&lt;/p&gt;

&lt;p&gt;Since 2 bits are fixed out of the 32 bits, there are 30 bits left to represent the address. So, in total, there can be 2³⁰ addresses in class B and 2¹⁴ network ID.&lt;/p&gt;

&lt;p&gt;The class B addresses constitute a total of 25% of the possible addresses.&lt;/p&gt;

&lt;p&gt;And the range for the network address is 128 to 191 (How? → If we put all zeroes except for the 1st 2 bits which are 10 by default, in the first octet, we get 10000000 = 128, and if we consider the last address i.e. put all ones we get 10111111 = 191).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The first and the last address i.e 128.0.0.0 and 191.255.255.255 are never used since the former is the host address/identifier and the latter is the broadcast address.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Due to this, even though 2¹⁴ network addresses are available, only 2¹⁴-2 out of them can be used.&lt;/p&gt;

&lt;p&gt;The default subnet mask for class B is &lt;strong&gt;255.255.0.0.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Class C:-
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JRGGrG0I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A0OvwCRiNLY-M4wVkADAcfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JRGGrG0I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A0OvwCRiNLY-M4wVkADAcfg.png" alt="" width="653" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Class C, the &lt;strong&gt;leading 24 bits&lt;/strong&gt; are used to represent the &lt;strong&gt;network&lt;/strong&gt; and the &lt;strong&gt;trailing 8 bits&lt;/strong&gt; are used to represent the network &lt;strong&gt;host&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example: 193.201.198.23 is a class C address&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, the 1st three bits in the first octet of a class C address are always 110.&lt;/p&gt;

&lt;p&gt;Since 3 bits are fixed out of the 32 bits, there are 29 bits left to represent the address. So, in total, there can be 2²⁹ addresses in class C and 2²¹ network ID.&lt;/p&gt;

&lt;p&gt;The class C addresses constitute a total of 12.5% of the possible addresses.&lt;/p&gt;

&lt;p&gt;And the range for the network address is 192 to 223 (How? → If we put all zeroes except for the 1st 3 bits which are 110 by default, in the first octet, we get 11000000 = 192, and if we consider the last address i.e. put all ones we get 11011111 = 223).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The first and the last address i.e 192.0.0.0 and 223.255.255.255 are never used .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Due to this, even though 2²⁹ network addresses are available, only 2²⁹-2 out of them can be used.&lt;/p&gt;

&lt;p&gt;The default subnet mask for class C is &lt;strong&gt;255.255.255.0.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Class D:-
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8XtlRfBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AH5-g50aDAMMTZJIBeSk71g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8XtlRfBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AH5-g50aDAMMTZJIBeSk71g.png" alt="" width="629" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Class D, there are no hosts or networks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example: 225.108.162.1 is a class D address&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, the 1st four bits in the first octet of a class D address are always 1110.&lt;/p&gt;

&lt;p&gt;Since 4 bits are fixed out of the 32 bits, there are 28 bits left to represent the address. So, in total, there can be 2²⁸ addresses in class D.&lt;/p&gt;

&lt;p&gt;The class D addresses constitute a total of 6.25% of the possible addresses.&lt;/p&gt;

&lt;p&gt;And the range for the network address is 224 to 239 (How? → If we put all zeroes except for the 1st 4 bits which are 1110 by default, in the first octet, we get 111000000 = 224, and if we consider the last address i.e. put all ones we get 11101111 = 239).&lt;/p&gt;

&lt;p&gt;These addresses are reserved for multicasting group email/broadcast.&lt;/p&gt;

&lt;h3&gt;
  
  
  Class E:-
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8XtlRfBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AH5-g50aDAMMTZJIBeSk71g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8XtlRfBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AH5-g50aDAMMTZJIBeSk71g.png" alt="" width="629" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Class E addresses are very similar to class D addresses.&lt;/p&gt;

&lt;p&gt;The 1st four bits in the first octet of a Class E address are always 1111.&lt;/p&gt;

&lt;p&gt;Since 4 bits are fixed out of the 32 bits, there are 28 bits left to represent the address. So, in total, there can be 2²⁸ addresses in class E.&lt;/p&gt;

&lt;p&gt;The class E addresses also constitute a total of 6.25% of the possible addresses.&lt;/p&gt;

&lt;p&gt;And the range for the network address is 240 to 255 (How? → If we put all zeroes except for the 1st 4 bits which are 1111 by default, in the first octet, we get 111100000 = 240 and if we consider the last address i.e. put all ones we get 11111111 = 255).&lt;/p&gt;

&lt;p&gt;These addresses are reserved for military purposes.&lt;/p&gt;

&lt;p&gt;To summarize,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1ebATSQA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3166/1%2AhTonC4CR0obuYQNPcrtaUw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1ebATSQA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3166/1%2AhTonC4CR0obuYQNPcrtaUw.png" alt="" width="880" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, that was all about classful addressing in IPv4.&lt;/p&gt;

&lt;p&gt;Feel free to leave a feedback in the comments.&lt;/p&gt;

&lt;p&gt;Have a nice day 😊&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Scheduling Algorithms in Operating Systems - Part 1 - First Come First Serve (FCFS)</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Mon, 06 Dec 2021 22:37:38 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a</link>
      <guid>https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a</guid>
      <description>&lt;p&gt;Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.&lt;/p&gt;

&lt;p&gt;For this purpose, there are many scheduling algorithms like the &lt;strong&gt;First Come First Serve&lt;/strong&gt; (FCFS) algorithm, &lt;strong&gt;Shortest Job First&lt;/strong&gt; (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrival Time&lt;/strong&gt;: The time at which the process arrives in the processor for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Waiting Time&lt;/strong&gt;: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Waiting time = Completion Time- Arrival Time- Service Time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Service Time/ Burst time&lt;/strong&gt;: The time required by the process to complete its execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turnaround Time&lt;/strong&gt;: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
&amp;gt;  &lt;em&gt;Turnaround Time = Service Time + Waiting Time&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let us have a look at the First Come First serve scheduling algorithm :&lt;/p&gt;

&lt;h2&gt;
  
  
  First Come First Serve (Also called FCFS or FIFO or non-preemptive) algorithm :
&lt;/h2&gt;

&lt;p&gt;In this algorithm, the processes are executed in the order they enter the processor.&lt;/p&gt;

&lt;p&gt;Consider the example,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RCt-81u---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AxaluRBROlEUPe3lb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RCt-81u---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AxaluRBROlEUPe3lb.png" alt="" width="880" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, there are 5 processes A, B, C, D, E with their arrival time and service time.&lt;/p&gt;

&lt;p&gt;From the given arrival times, we can see that process A is the first one to arrive at 0ms (milliseconds). So that process starts executing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gwKEV8G0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AFGvqHxvRNnSBU34U.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gwKEV8G0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AFGvqHxvRNnSBU34U.png" alt="" width="880" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the execution queue. Process A starts executing at 0 and continues till 3 i.e. executes completely. Meanwhile, process B arrives at 2ms and is added to the waiting queue. Note: The value in the brackets ‘()’ denotes the service time/burst time required by the process.&lt;/p&gt;

&lt;p&gt;Next, process B is removed from the waiting queue and starts executing:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BYGEA8VN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AsyD5WPNTAq6VH0Jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BYGEA8VN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AsyD5WPNTAq6VH0Jw.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Process B executes till 9ms. Meanwhile processes C, D, E arrive and are added to the waiting queue.&lt;/p&gt;

&lt;p&gt;C being first in the queue starts executing next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jB_NOjg4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AO8MNgGb-80TL6rdB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jB_NOjg4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2060/0%2AO8MNgGb-80TL6rdB.png" alt="" width="880" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It executes for 4ms. No new process is added to the waiting queue. The next process to be executed is D.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aKlI_xBw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2660/0%2AAT1YHH96wmUXd46X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aKlI_xBw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2660/0%2AAT1YHH96wmUXd46X.png" alt="" width="880" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;D executes for 5ms from 13 through 18. After that E is the only process left to be executed. So, E executes next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mic30eNL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2660/0%2AP3d5nvAiE9NeBfv_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mic30eNL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2660/0%2AP3d5nvAiE9NeBfv_.png" alt="" width="880" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see all the processes are successfully executed. Now let us calculate the waiting time and the turnaround time using the formulae mentioned above.&lt;/p&gt;

&lt;p&gt;These are the results obtained:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lbn2bFun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AwucrI23hn6jMiIHx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lbn2bFun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2710/0%2AwucrI23hn6jMiIHx.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, this is the FCFS algorithm.&lt;/p&gt;

&lt;p&gt;Advantages :&lt;/p&gt;

&lt;p&gt;It is very simple.&lt;/p&gt;

&lt;p&gt;Disadvantages :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Favors CPU bound processes over I/O bound processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a longer process starts executing, the shorter processes have to wait for long which leads to the starvation of the shorter processes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To overcome this disadvantage, we have the Shortest Job First algorithm.&lt;/p&gt;

&lt;p&gt;Hope this helps you!&lt;/p&gt;

&lt;p&gt;Have a nice day!!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Deterministic Finite Automata</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Mon, 06 Dec 2021 22:32:06 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/the-deterministic-finite-automata-1f9i</link>
      <guid>https://dev.to/iamdigitalanna/the-deterministic-finite-automata-1f9i</guid>
      <description>&lt;p&gt;&lt;em&gt;What is DFA?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Does DFA stand for Dog Food Association?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;😂Nope, not at all. DFA has nothing to do with any dog or association. In fact, it is a very sophisticated machine.&lt;/p&gt;

&lt;p&gt;DFA in fact is the acronym for — &lt;strong&gt;Deterministic Finite Automata.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Deterministic Finite Automata?
&lt;/h3&gt;

&lt;p&gt;Firstly, automata is a mathematical model or abstract model which is used to determine if a string is accepted by a language or not.&lt;/p&gt;

&lt;p&gt;The DFA is an automata that has finite states and accepts or rejects a given string by running through several states.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Oof! That’s too much of bookish language.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To make things simpler, let's take an example.&lt;/p&gt;

&lt;p&gt;Consider a machine that accepts strings over the alphabets &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; and &lt;em&gt;**b *&lt;/em&gt;&lt;em&gt;which contain at least one *&lt;/em&gt;&lt;em&gt;a.&lt;/em&gt;**&lt;/p&gt;

&lt;p&gt;This machine can be diagrammatically represented as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HF-PabYw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQ35rZMtMP4MF8TiOoDZLmw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HF-PabYw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQ35rZMtMP4MF8TiOoDZLmw.jpeg" alt="" width="514" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the state &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt; is the state we are starting from and &lt;strong&gt;&lt;em&gt;qf&lt;/em&gt;&lt;/strong&gt; is the final state of the machine i.e. the state where we understand if the string is accepted or not.&lt;/p&gt;

&lt;p&gt;Let's say we have a string ‘&lt;strong&gt;&lt;em&gt;ba&lt;/em&gt;&lt;/strong&gt;’.&lt;/p&gt;

&lt;p&gt;‘&lt;strong&gt;&lt;em&gt;b&lt;/em&gt;&lt;/strong&gt;’ is the first alphabet that goes into the system first.&lt;/p&gt;

&lt;p&gt;As we can see from the above diagram, when &lt;strong&gt;&lt;em&gt;b&lt;/em&gt;&lt;/strong&gt; enters the system i.e. the initial state &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt;, it is accepted by &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt; and stays there. Now when we give the symbol &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; to the machine, as we can see that when &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt; accepts &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt;, it goes to &lt;em&gt;**qf *&lt;/em&gt;*which is the final state. (The double circles indicate the final state).&lt;/p&gt;

&lt;p&gt;With &lt;em&gt;**a *&lt;/em&gt;*being accepted by the machine and since the string is over, we can conclude that the string is accepted by the DFA.&lt;/p&gt;

&lt;p&gt;Now let's consider the string “&lt;strong&gt;&lt;em&gt;bbb”.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first &lt;em&gt;**b *&lt;/em&gt;&lt;em&gt;is accepted by the string *&lt;/em&gt;&lt;em&gt;q0.&lt;/em&gt;**&lt;/p&gt;

&lt;p&gt;Then comes the second &lt;strong&gt;&lt;em&gt;b&lt;/em&gt;&lt;/strong&gt;. We are at &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt; currently and the second &lt;strong&gt;&lt;em&gt;b&lt;/em&gt;&lt;/strong&gt; is again accepted by &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt;, thanks to the self-loop that we have shown.&lt;/p&gt;

&lt;p&gt;So, by the end of the string, we stay on the state &lt;em&gt;**q0 *&lt;/em&gt;&lt;em&gt;and do not reach the final state. Hence the string *&lt;/em&gt;&lt;em&gt;“bbb”&lt;/em&gt;** is not accepted by the machine. Also, the condition we had laid was that the string should contain a minimum of one “&lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;p&gt;So, obviously, the string &lt;em&gt;**“bbb” *&lt;/em&gt;*should not be accepted.&lt;/p&gt;

&lt;p&gt;This was a very easy example where one could easily predict if the string would be accepted or not without the DFA. But in some cases, it is really difficult for a person to determine that.&lt;/p&gt;

&lt;p&gt;Formally, the DFA is defined using 5 tuples:&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;**Q, Σ , 𝛿, q0 , F *&lt;/em&gt;*)&lt;/p&gt;

&lt;p&gt;where:&lt;/p&gt;

&lt;p&gt;Q : Set of finite states (for eg: {q0,q1,q2,……,qf})&lt;/p&gt;

&lt;p&gt;Σ: Set of symbols (for eg: a,b....1,0…..)&lt;/p&gt;

&lt;p&gt;𝛿: Transition Function (function applied/ condition applied onto the string)&lt;/p&gt;

&lt;p&gt;q0: Initial/start state&lt;/p&gt;

&lt;p&gt;F: Set of final states (here, qf but there can be multiple final states)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For every state, each input symbol has to have &lt;strong&gt;&lt;em&gt;one&lt;/em&gt;&lt;/strong&gt; transition&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s solve some examples now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Example 1:&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design a DFA over the input alphabets &lt;strong&gt;&lt;em&gt;{a,b}&lt;/em&gt;&lt;/strong&gt; which accepts all strings that end with &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;— Consider the strings &lt;strong&gt;&lt;em&gt;{a,aa,ba,aba,bba………}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All these ends with a, so all of them should be accepted by the DFA.&lt;/p&gt;

&lt;p&gt;From the example above, we can infer that there can be any number of alphabets before a.&lt;/p&gt;

&lt;p&gt;Considering that, we can design the DFA as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A7WFF0jl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AxX_SYNinHmR5YxjehZBMQw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A7WFF0jl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AxX_SYNinHmR5YxjehZBMQw.jpeg" alt="" width="502" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, according to the given question we need a machine to accept strings ending with &lt;strong&gt;&lt;em&gt;a.&lt;/em&gt;&lt;/strong&gt; Since our condition involves only one symbol, we have only one necessary transition and hence we take two states. Here, the string can start with any symbol but should end in &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt;. Hence, any occurrence of &lt;strong&gt;&lt;em&gt;b&lt;/em&gt;&lt;/strong&gt; is directed towards &lt;strong&gt;&lt;em&gt;q0&lt;/em&gt;&lt;/strong&gt; but &lt;strong&gt;&lt;em&gt;a’s&lt;/em&gt;&lt;/strong&gt; are directed towards the final state &lt;strong&gt;&lt;em&gt;qf.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Test it out with other strings ending with &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Example 2:&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design a DFA over the input alphabets &lt;strong&gt;&lt;em&gt;{a,b}&lt;/em&gt;&lt;/strong&gt; which accepts all strings that start with &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; and end with &lt;strong&gt;&lt;em&gt;b&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you have understood the concept, why not give your brain a little exercise?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Try this one on your own and then scroll down to check your answer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;I hope you could solve it. If you couldn’t no worries, you could check out my solution:&lt;/p&gt;

&lt;p&gt;Consider strings such as:&lt;strong&gt;&lt;em&gt;{ab ,aabb ,aab ,abb ,abab,…… }&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These strings should be accepted by the DFA. Hence, the DFA can be designed as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--skzAT1Ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnvQ0X9TPMQ8tAJubgyHR5Q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--skzAT1Ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnvQ0X9TPMQ8tAJubgyHR5Q.jpeg" alt="" width="752" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;D&lt;/em&gt;&lt;/strong&gt; is the dead state that accepts all the strings not suitable for our machine.&lt;/p&gt;

&lt;p&gt;Here, if the string starts with*** b***, it will get into a dead state and will never reach the final state.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Did you get it right? Do let me know in the comments.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Another one,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Example 3:&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design a DFA over the input alphabets &lt;strong&gt;&lt;em&gt;{a,b}&lt;/em&gt;&lt;/strong&gt; which accepts all strings that do not start with &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; or d not end with &lt;strong&gt;&lt;em&gt;b.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Try this on your own!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hope you got this one!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here's a little trick to solve this one. Take the complement of the problem statement. The complement would be:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;……………..which accepts all strings that **start with a and end with b.&lt;/em&gt;**&lt;/p&gt;

&lt;p&gt;Design a DFA for the compliment. That would be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--skzAT1Ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnvQ0X9TPMQ8tAJubgyHR5Q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--skzAT1Ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnvQ0X9TPMQ8tAJubgyHR5Q.jpeg" alt="" width="752" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, take the complement of this DFA. i.e. make the final states non-final and the non-final states final. This would give us:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uBzaKsKC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQ55nsiZ-TJWH6qTqVyjGiA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uBzaKsKC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQ55nsiZ-TJWH6qTqVyjGiA.jpeg" alt="" width="777" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the solution to our problem statement.&lt;/p&gt;

&lt;p&gt;Consider the examples: &lt;strong&gt;&lt;em&gt;{ε, a ,b ,ba , bbaa, aaa, aba,…..}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ε is a null/empty string&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These are a few strings that do not start with &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; or do not end in &lt;strong&gt;&lt;em&gt;b.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are accepted by the above DFA.&lt;/p&gt;

&lt;p&gt;On the other hand if you try the strings like &lt;strong&gt;&lt;em&gt;{ab ,aabb ,aab ,abb ,abab,…. },&lt;/em&gt;&lt;/strong&gt; they won’t be accepted.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Example 4:&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design a DFA over the input alphabets &lt;strong&gt;&lt;em&gt;{0,1}&lt;/em&gt;&lt;/strong&gt; where the 2nd symbol should &lt;strong&gt;&lt;em&gt;0&lt;/em&gt;&lt;/strong&gt; and 4th should be &lt;strong&gt;&lt;em&gt;1.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Got the answer?…..Check it with the one I got:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Consider input strings : &lt;strong&gt;&lt;em&gt;{0011, 0001, 1001 ,1011,…}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U4kwUjar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2058/1%2AxEcfoOeeQjs7qx2NTgQzmg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U4kwUjar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2058/1%2AxEcfoOeeQjs7qx2NTgQzmg.jpeg" alt="" width="880" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wasn’t it easy?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let’s take a look at the last but very very interesting example.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Example 5:&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design a DFA over the input alphabets &lt;strong&gt;&lt;em&gt;{0,1}&lt;/em&gt;&lt;/strong&gt; which should accept all numbers divisible by 3.&lt;/p&gt;

&lt;p&gt;This is a bit tricky question, but let’s solve it step by step.&lt;/p&gt;

&lt;p&gt;Firstly, if a number is divided by 3, the possible remainders are 0,1 and 2.&lt;/p&gt;

&lt;p&gt;Hence, our DFA will have 3 states.&lt;/p&gt;

&lt;p&gt;Now binary equivalents of these remainders are:&lt;/p&gt;

&lt;p&gt;0 -&amp;gt; 0&lt;/p&gt;

&lt;p&gt;1-&amp;gt;01&lt;/p&gt;

&lt;p&gt;2-&amp;gt;10&lt;/p&gt;

&lt;p&gt;These combinations, when occur, should lead to the respective state.&lt;/p&gt;

&lt;p&gt;Also, we want to target strings divisible by 3, so the expected remainder is 0 and hence, the final state is q0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--taAZJtLc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACUiZ-qD0y4ZbcuVMOToS2w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--taAZJtLc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACUiZ-qD0y4ZbcuVMOToS2w.jpeg" alt="" width="742" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, until now we have this.&lt;/p&gt;

&lt;p&gt;Now, the next step is to consider other strings like:&lt;/p&gt;

&lt;p&gt;3-&amp;gt;011&lt;/p&gt;

&lt;p&gt;4 -&amp;gt;100&lt;/p&gt;

&lt;p&gt;5-&amp;gt; 101&lt;/p&gt;

&lt;p&gt;6-&amp;gt; 110&lt;/p&gt;

&lt;p&gt;7 -&amp;gt;111&lt;/p&gt;

&lt;p&gt;From these, 3 and 6 give a remainder of 0;&lt;/p&gt;

&lt;p&gt;4 and 7 give 1; 5 give 2.&lt;/p&gt;

&lt;p&gt;If we handle all these conditions and complete our DFA this is what we get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qNsgIDtj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AFsb1RTfPFlQ2igmX6rmARw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qNsgIDtj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AFsb1RTfPFlQ2igmX6rmARw.jpeg" alt="" width="765" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the complete solution to our problem.&lt;/p&gt;

&lt;p&gt;For example, consider 15 (1111), which is divisible by 3. Hence we should get 0 as output. Let’s verify:&lt;/p&gt;

&lt;p&gt;When we give 1111 to this DFA, the path followed is q0-&amp;gt;q1-&amp;gt;q0-&amp;gt;q1-&amp;gt;q0 which brings us back to q0 which is our final state. Hence, 15 is accepted as expected.&lt;/p&gt;

&lt;p&gt;Similarly, for 21(10101), the path is: q0-&amp;gt;q1-&amp;gt;q2-&amp;gt;q2-&amp;gt;q1-&amp;gt;q0.&lt;/p&gt;

&lt;p&gt;Hence,21 is also accepted.&lt;/p&gt;

&lt;p&gt;Consider 14(1110), which is not divisible by 3 and leaves a remainder of 2 i.e. should end up at q2.&lt;/p&gt;

&lt;p&gt;Let's verify.&lt;/p&gt;

&lt;p&gt;The path obtained is q0-&amp;gt;q1-&amp;gt;q0-&amp;gt;q1-&amp;gt;q2.&lt;/p&gt;

&lt;p&gt;Hence verified.&lt;/p&gt;

&lt;p&gt;I hope things are much clearer now. I have tried my best to explain the concept and tried to take as many examples as possible.&lt;/p&gt;

&lt;p&gt;Let me know if it helped you and also you can post your questions in the comments below.&lt;/p&gt;

&lt;p&gt;Have a nice day!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Dining Philosophers Problem Solution in C</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Mon, 06 Dec 2021 22:26:37 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/the-dining-philosophers-problem-solution-in-c-54l2</link>
      <guid>https://dev.to/iamdigitalanna/the-dining-philosophers-problem-solution-in-c-54l2</guid>
      <description>&lt;p&gt;The dining philosophers problem is a very famous and interesting problem used to demonstrate the concept of deadlock.&lt;/p&gt;

&lt;p&gt;To understand what the dining philosophers problem actually is, you can refer this blog:&lt;br&gt;
&lt;a href="https://dev.to/iamdigitalanna/the-dining-philosophers-problem-3p4m"&gt;&lt;strong&gt;The Dining Philosopher’s problem&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, I am going to explain the solution to this problem using the concept of semaphores in C. Here’s the program:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
#include&amp;lt;pthread.h&amp;gt;
#include&amp;lt;semaphore.h&amp;gt;
#include&amp;lt;unistd.h&amp;gt;

sem_t room;
sem_t chopstick[5];

void * philosopher(void *);
void eat(int);
int main()
{
    int i,a[5];
    pthread_t tid[5];

    sem_init(&amp;amp;room,0,4);

    for(i=0;i&amp;lt;5;i++)
        sem_init(&amp;amp;chopstick[i],0,1);

    for(i=0;i&amp;lt;5;i++){
        a[i]=i;
        pthread_create(&amp;amp;tid[i],NULL,philosopher,(void *)&amp;amp;a[i]);
    }
    for(i=0;i&amp;lt;5;i++)
        pthread_join(tid[i],NULL);
}

void * philosopher(void * num)
{
    int phil=*(int *)num;

    sem_wait(&amp;amp;room);
    printf("\nPhilosopher %d has entered room",phil);
    sem_wait(&amp;amp;chopstick[phil]);
    sem_wait(&amp;amp;chopstick[(phil+1)%5]);

    eat(phil);
    sleep(2);
    printf("\nPhilosopher %d has finished eating",phil);

    sem_post(&amp;amp;chopstick[(phil+1)%5]);
    sem_post(&amp;amp;chopstick[phil]);
    sem_post(&amp;amp;room);
}

void eat(int phil)
{
    printf("\nPhilosopher %d is eating",phil);
}

/* BY - ANUSHKA DESHPANDE */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let us first understand what is a semaphore and why it is used.&lt;/p&gt;

&lt;p&gt;Basically, semaphore is a special type of variable used to control the access to a shared resource.&lt;/p&gt;

&lt;p&gt;The definition of semaphore is in the library &lt;em&gt;**semaphore.h *&lt;/em&gt;*.&lt;/p&gt;

&lt;p&gt;There are many functions related to semaphores like &lt;em&gt;sem_inti()&lt;/em&gt;, &lt;em&gt;sem_wait()&lt;/em&gt;, &lt;em&gt;sem_post(), etc. *These functions are also defined under the *&lt;/em&gt;&lt;em&gt;semaphore.h&lt;/em&gt;** library.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The data type of the semaphores is sem_t&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two types of semaphores-:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Binary semaphore&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Counting semaphore&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The binary semaphore is used when there is only one instance of the resource whereas the counting semaphore is used when there are multiple instances of the resources.&lt;/p&gt;

&lt;p&gt;In our program, we have used both types of semaphores.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sem_t room;
sem_t chopstick[5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, the semaphore room is a counting semaphore since there is one dining room which can accommodate 4 philosophers. (i.e. Consider there are 4 chairs in the room and that is the resource. Hence there are multiple instances of the resource in the room. Therefore, room is a counting semaphore.)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sem_init(&amp;amp;room,0,4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The function *sem_init() *is used to initialize the semaphore.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int sem_init(sem_t **sem*, int *pshared*, unsigned int *value*);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;The first parameter is the pointer to the declared semaphore.&lt;br&gt;
 The second parameter is pshared. If it is zero, the semaphore is shared between threads; else it is shared between processes. In our case it is zero meaning it is shared between threads.&lt;br&gt;
 The third parameter value is the value with which the semaphore is initialized.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, the semaphore room is initialized as 4 meaning it will vary between 0–3 and have 4 values.&lt;/p&gt;

&lt;p&gt;Now, since there are 5 chopsticks, we have created 5 binary semaphores referring to the five chopsticks C0-C4.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(i=0;i&amp;lt;5;i++)
        sem_init(&amp;amp;chopstick[i],0,1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;For the chopsticks, we have used binary semaphores since for every chopstick, C0-C4 we have only one instance of it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, according to our program, we have a scenario like:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NRtoOY6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2APzzfEqPP6n8XYk9Rt_cgLQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NRtoOY6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2APzzfEqPP6n8XYk9Rt_cgLQ.png" alt="" width="880" height="980"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An empty room with 5 chopsticks and places for four philosophers.&lt;/p&gt;

&lt;p&gt;In the program above, we have created threads.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pthread_t tid[5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;These threads refer to the 5 philosophers sitting around the table.&lt;/p&gt;

&lt;p&gt;We have them as threads since we want these to execute simultaneously (i.e. we want multiple philosophers to eat at a time).&lt;/p&gt;

&lt;p&gt;Now there can be a situation where all 5 threads start executing i.e. all 5 philosophers enter the room and cause deadlock. Hence, we are allowing 4 philosophers to enter the room first so that at least one of them can finish eating.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(i=0;i&amp;lt;5;i++){
        a[i]=i;
        pthread_create(&amp;amp;tid[i],NULL,philosopher,(void *)&amp;amp;a[i]);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are calling the function philosopher() from pthread_create and passing it the address of an integer variable which refers to the philosopher number.&lt;/p&gt;

&lt;p&gt;In the philosopher function,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void * philosopher(void * num)
{
    int phil=*(int *)num;

    sem_wait(&amp;amp;room);
    printf("\nPhilosopher %d has entered room",phil);
    sem_wait(&amp;amp;chopstick[phil]);
    sem_wait(&amp;amp;chopstick[(phil+1)%5]);

    eat(phil);
    sleep(2);
    printf("\nPhilosopher %d has finished eating",phil);

    sem_post(&amp;amp;chopstick[(phil+1)%5]);
    sem_post(&amp;amp;chopstick[phil]);
    sem_post(&amp;amp;room);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;we are first converting the number passed as a void * into integer.&lt;/p&gt;

&lt;p&gt;Then we have called the sem_wait function which first checks if the resource is available and if it is available, the resource is allocated to the philosopher i.e. the semaphore is locked.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sem_wait(&amp;amp;room);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This being a counting semaphore, the prototype of sem_wait is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct semaphore {
      int count;
      queueType queue;
};

void sem_wait(semaphore s)
{
      s.count--;
      if(s.count&amp;lt;0)
      {
            /*place this process in s.queue */;
            /*block this process*/;      
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So here, the number of semaphores is decremented, meaning one of the semaphores is allocated. If all of the resources are allocated, the thread is placed on waiting queue.&lt;/p&gt;

&lt;p&gt;Now, we apply sem_wait on chopsticks which are binary semaphores.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sem_wait(&amp;amp;chopstick[phil]);
sem_wait(&amp;amp;chopstick[(phil+1)%5]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The definition of sem_wait for binary semaphores is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct binary_semaphore {
      enum {zero,one} value;
      queueType queue;
}

void sem_wait(binary_semaphore s)
{
      if(s.value == one)
           s.value = zero;
      else
      {
              /* place this process in s.queue */
              /* block this process */
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, according to the prototype, if the value of semaphore is one, it is changed to zero indicating that the semaphore is blocked.&lt;/p&gt;

&lt;p&gt;In our case we are blocking the chopsticks towards the left and the right of the philosopher.&lt;/p&gt;

&lt;p&gt;For example, for philosopher P0, we are blocking chopstick C0 and C4.&lt;/p&gt;

&lt;p&gt;Then, we are allowing the philosophers to eat.&lt;/p&gt;

&lt;p&gt;Finally, we are freeing the semaphores by calling the &lt;em&gt;sem_post()&lt;/em&gt; function so that the other threads that are placed on the queue can use the resources.&lt;/p&gt;

&lt;p&gt;The prototype for the &lt;em&gt;sem_post()&lt;/em&gt; function is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int sem_post(sem_t **sem*);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If it returns a positive value, the semaphore is unlocked successfully.&lt;/p&gt;

&lt;p&gt;For a binary semaphore, the &lt;em&gt;sem_post()&lt;/em&gt; function works as:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void sem_post(semaphore s)
{
      if(s.queue is empty)
            s.value=one;
      else
      {
            /* remove a process P from s.queue */
            /* place process P on ready list */
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;and for a counting semaphore, the sem_post() function is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void sem_post(semaphore s)
{
      s.count++;
      if(s.count &amp;lt;= 0)
      {
            /* remove a process P from s.queue */
            /* place process P on ready list */
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Using the above functions, we free all the semaphores so that they can be used by other threads.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sem_post(&amp;amp;chopstick[(phil+1)%5]);
sem_post(&amp;amp;chopstick[phil]);
sem_post(&amp;amp;room);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The same thing happens for all 5 philosophers.&lt;/p&gt;

&lt;p&gt;After all the 5 are done, we join the threads back to the main process.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(i=0;i&amp;lt;5;i++)
        pthread_join(tid[i],NULL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, this is how we can implement the solution to the dining philosophers problem using semaphores.&lt;/p&gt;

&lt;p&gt;At the end, you should get an output which looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Philosopher 4 has entered room
Philosopher 4 is eating
Philosopher 2 has entered room
Philosopher 2 is eating
Philosopher 3 has entered room
Philosopher 1 has entered room
Philosopher 2 has finished eating
Philosopher 4 has finished eating
Philosopher 3 is eating
Philosopher 1 is eating
Philosopher 0 has entered room
Philosopher 3 has finished eating
Philosopher 1 has finished eating
Philosopher 0 is eating
Philosopher 0 has finished eating
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The sequence does not matter. It can be different on different machines.&lt;/p&gt;

&lt;p&gt;The only thing is to make sure that all philosophers are entering the room, performing the eating operation and leaving the room and also that no two philosophers are using the same chopstick at the same time.&lt;/p&gt;

&lt;p&gt;Do let me know if you face any problems and feel free to leave a feedback.&lt;/p&gt;

&lt;p&gt;Hope this helps you!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Dining Philosopher's Problem</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Mon, 06 Dec 2021 22:22:44 +0000</pubDate>
      <link>https://dev.to/iamdigitalanna/the-dining-philosophers-problem-3p4m</link>
      <guid>https://dev.to/iamdigitalanna/the-dining-philosophers-problem-3p4m</guid>
      <description>&lt;p&gt;The dining philosopher’s problem is a very well known resource sharing problem in the world of computers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fh59neqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AkTNv4zAJfdhvM9l0LiwUaA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fh59neqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AkTNv4zAJfdhvM9l0LiwUaA.png" alt="" width="544" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem says:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SETt7b1y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AWYw1_AHCq3A4RM5Dh01usg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SETt7b1y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AWYw1_AHCq3A4RM5Dh01usg.gif" alt="" width="600" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 5 philosophers sitting around a round table eating spaghetti and each of them has one chopstick between them. All 5 of them sit around the table and pick up the chopstick placed towards their right. But, here’s the problem. To eat the spaghetti they need both the chopsticks and since everyone picked up the chopstick to their right, nobody gets the left chopstick and hence, nobody can eat.&lt;/p&gt;

&lt;p&gt;Logically thinking, this problem isn’t really a problem in real life scenario. The philosophers could have asked for a few extra pairs of chopsticks, or they could have eaten using their hands 😂 .&lt;/p&gt;

&lt;p&gt;Jokes apart, but the dining philosophers problem is an excellent example to explain the concept of deadlock while resource sharing in OS.&lt;/p&gt;

&lt;p&gt;Consider the philosophers to be processes and the chopsticks to be a shared resource. Every process needs two resources out of which one it has already acquired and the other is acquired by some other process. Till the other process does not free the resource, this process cannot proceed. Similarly, the other process is dependent on another process for its resource. Since every process is dependent on each other, it forms a circular-wait and the system goes into a deadlock condition.&lt;/p&gt;

&lt;p&gt;Here, when each philosopher picks up the chopstick to their right, they also end up picking the left chopstick of the person sitting next to them which leaves every philosopher with just one chopstick and nobody can start eating.&lt;/p&gt;

&lt;p&gt;To solve this problem, we can use this strategy:&lt;/p&gt;

&lt;p&gt;Consider five philosophers: P0,P1,P2,P3,P4 &amp;amp;&lt;/p&gt;

&lt;p&gt;five chopsticks: C0,C1,C2,C3,C4&lt;/p&gt;

&lt;p&gt;Initially we let P0 enter into the dining room.&lt;/p&gt;

&lt;p&gt;Since nobody else is present there all the chopsticks are free and P0 acquires chopsticks C0 and C1 and starts eating.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lu7epOTW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ao5O5AL2Fsxav-v4pL8RKwQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lu7epOTW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ao5O5AL2Fsxav-v4pL8RKwQ.png" alt="" width="869" height="904"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, P1 enters the room. Since P0 is already eating, chopstick C1 is not available for P1 and hence P1 cannot eat and is kept waiting.&lt;/p&gt;

&lt;p&gt;PS : Since P1 does not get C1, it doesn’t claim C2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--98aieFEQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A5uSCZhqqrEAVPm0Anr-rnA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--98aieFEQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A5uSCZhqqrEAVPm0Anr-rnA.png" alt="" width="869" height="921"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, P2 enters the room. Since C2 and C3 both are available, P2 starts eating.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rpqn80le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADo6ZdUhF4MA5rBn-TMRzkQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rpqn80le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADo6ZdUhF4MA5rBn-TMRzkQ.png" alt="" width="869" height="921"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, P3 enters and is blocked and added to the waiting queue because C3 is not available.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WrLw9pHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AiVFy-aNsNppBK03Zpm440Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WrLw9pHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AiVFy-aNsNppBK03Zpm440Q.png" alt="" width="880" height="920"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly P4 is blocked and is added to the waiting queue as C0 is not available&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ghjby_qK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AIbopZTs2dgDzr_8wu8DiJA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ghjby_qK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AIbopZTs2dgDzr_8wu8DiJA.png" alt="" width="880" height="920"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, consider P0 has finished eating and the resources claimed by it i.e. C0 and C1 are released. P1 cannot start eating since C2 is still not released and hence P4 starts eating since C4 and C0 both are released. Hence,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y17gPz5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ApLhNwqHUNTPjvUDcVOkUoQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y17gPz5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ApLhNwqHUNTPjvUDcVOkUoQ.png" alt="" width="880" height="919"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If now P2 releases the chopsticks C2 and C3, philosopher P1 can start eating&lt;/p&gt;

&lt;p&gt;P3 has to stay in the waiting queue since chopstick C4 is not available.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--svv3tNM8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQQWSQzF9IKIYtgWixYeZbQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--svv3tNM8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQQWSQzF9IKIYtgWixYeZbQ.png" alt="" width="880" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, consider that philosopher P4 has finished eating and has freed chopsticks C0 and C4, P3 can start eating.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aUQiaIUr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ASUb4il9kXjSehbxPrsQ0cw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aUQiaIUr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ASUb4il9kXjSehbxPrsQ0cw.png" alt="" width="880" height="908"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the strategy using which we have ensured that all the philosophers have been served and avoided the circular wait situation.&lt;/p&gt;

&lt;p&gt;This is the logic behind the solution of the dining philosophers problem.&lt;/p&gt;

&lt;p&gt;Here is the C program designed to solve the problem:-&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(I’ve used semaphores to solve this problem. For more information on semaphores, refer this website-)&lt;/em&gt;&lt;br&gt;
&lt;a href="https://docs.oracle.com/cd/E19683-01/806-6867/sync-27385/index.html"&gt;&lt;strong&gt;Semaphores&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
#include&amp;lt;pthread.h&amp;gt;
#include&amp;lt;semaphore.h&amp;gt;
#include&amp;lt;unistd.h&amp;gt;

sem_t room;
sem_t chopstick[5];

void * philosopher(void *);
void eat(int);
int main()
{
    int i,a[5];
    pthread_t tid[5];

    sem_init(&amp;amp;room,0,4);

    for(i=0;i&amp;lt;5;i++)
        sem_init(&amp;amp;chopstick[i],0,1);

    for(i=0;i&amp;lt;5;i++){
        a[i]=i;
        pthread_create(&amp;amp;tid[i],NULL,philosopher,(void *)&amp;amp;a[i]);
    }
    for(i=0;i&amp;lt;5;i++)
        pthread_join(tid[i],NULL);
}

void * philosopher(void * num)
{
    int phil=*(int *)num;

    sem_wait(&amp;amp;room);
    printf("\nPhilosopher %d has entered room",phil);
    sem_wait(&amp;amp;chopstick[phil]);
    sem_wait(&amp;amp;chopstick[(phil+1)%5]);

    eat(phil);
    sleep(2);
    printf("\nPhilosopher %d has finished eating",phil);

    sem_post(&amp;amp;chopstick[(phil+1)%5]);
    sem_post(&amp;amp;chopstick[phil]);
    sem_post(&amp;amp;room);
}

void eat(int phil)
{
    printf("\nPhilosopher %d is eating",phil);
}

/* BY - ANUSHKA DESHPANDE */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To understand this program, refer my blog&lt;br&gt;
&lt;a href="https://dev.to/iamdigitalanna/the-dining-philosophers-problem-solution-in-c-54l2"&gt;&lt;strong&gt;The dining philosophers problem solution in C&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do let me know in the comments if this helps you.&lt;/p&gt;

&lt;p&gt;Have a nice day!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
