title: Implementing FCFS CPU Scheduler (Wait time function)
body: <p>Process Data:<br>
  process goes {CPU burst, I/O time, CPU burst, I/O time, CPU burst, I/O time,…….., last CPU burst}</p>
  
  <p>I split the data into two arrays. cpu burst and io_burst</p>
  
  <p>P1 {4,24,5,73,3,31,5,27,4,33,6,43,4,64,5,19,2}</p>
  
  <p>P2 {18,31,19,35,11,42,18,43,19,47,18,43,17,51,19,32,10}</p>
  
  <p>P3 {6,18,4,21,7,19,4,16,5,29,7,21,8,22,6,24,5}</p>
  
  <p>P4 {17,42,19,55,20,54,17,52,15,67,12,72,15,66,14}</p>
  
  <p>P5 {5,81,4,82,5,71,3,61,5,62,4,51,3,77,4,61,3,42,5}</p>
  
  <p>P6 {10,35,12,41,14,33,11,32,15,41,13,29,11}</p>
  
  <p>P7 {21,51,23,53,24,61,22,31,21,43,20}</p>
  
  <p>P8 {11,52,14,42,15,31,17,21,16,43,12,31,13,32,15}</p>
  
  <p>I'm mostly having a hard time calculating the wait time for this. I made a class process which is here</p>
  
  <pre><code>class Process
  {
      //public Process(string name, int arrival, int cpuburst,int ioburst, int priority)
      //{
      //    this.name = name;
      //    this.arrival = arrival;
      //    this.cpuburst = cpuburst;
      //    this.ioburst = ioburst;
      //    this.priority = priority;
      //}
      public Process()
      {
  
      }
      public string name;
      public int arrival;
      public int[] cpuburst;
      public int []ioburst;
      public int priority;
      public int totburst;
      public int wait;
      public int end;
      public int start;
      public int contextswitch;
      public int response;
      public int turnAround;
  
      //public int contextswitch;
      public double cpu_utilization;
      public double avwaitingtime;
      public double avturnaroundtime;
      public double avresponse;
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>heres how I'm initializing, I made cpuburst and io_burst the same size by adding some zeroes to the end of the array                  </p>
  
  <pre><code>void initialize_process()
      {
          List&lt;Process&gt; processes = new List&lt;Process&gt;();
          Process p1 = new Process();
          p1.arrival = 0;
          p1.end = 0;
          p1.name = "P1";
          p1.cpuburst = new int[] { 4, 5, 3, 5, 4, 6, 4, 5, 2 ,0};
          p1.ioburst = new int[] { 24, 73, 31, 27, 33, 43, 64, 19, 0, 0 };
          p1.totburst = 38;
          processes.Add(p1);
  
          Process p2 = new Process();
          p2.arrival = 0;
          p2.name = "P2";
          p2.end = 0;
          p2.cpuburst = new int[] { 18, 19, 11, 18, 19, 18, 17, 19, 10, 0 };
          p2.ioburst = new int[] { 31, 35, 42, 43, 47, 43, 51, 32, 0, 0 };
          p2.totburst = 149;
          processes.Add(p2);
  
          Process p3 = new Process();
          p3.arrival = 0;
          p3.end = 0;
          p3.name = "P3";
          p3.cpuburst = new int[]{ 6, 4, 7, 4, 5, 7, 8, 6, 5 ,0};
          p3.ioburst = new int[] { 18, 21, 19, 16, 29, 21, 22, 24, 0, 0 };
          p3.totburst = 52;
          processes.Add(p3);
  
          Process p4 = new Process();
          p4.arrival = 0;
          p4.end = 0;
          p4.name = "P4";
          p4.cpuburst = new int[] { 17, 19, 20, 17, 15, 12, 15, 14,0,0 };
          p4.ioburst = new int[] { 42, 55, 54, 52, 67, 72, 66, 0, 0, 0 };
          p4.totburst = 129;
          processes.Add(p4);
  
          Process p5 = new Process();
          p5.arrival = 0;
          p5.end = 0;
          p5.name = "P5";
          p5.cpuburst = new int[]{ 5, 4, 5, 3, 5, 4, 3, 4, 3, 5 };
          p5.ioburst = new int[] { 81, 82, 71, 61, 62, 51, 77, 61, 42, 0 };
          p5.totburst = 41;
          processes.Add(p5);
  
          Process p6 = new Process();
          p6.arrival = 0;
          p6.end = 0;
          p6.name = "P6";
          p6.cpuburst = new int[] { 10, 12, 14, 11, 15, 13, 11,0,0,0 };
          p6.ioburst = new int[] { 35, 41, 33, 32, 41, 29, 0, 0, 0, 0 };
          p6.totburst = 86;
          processes.Add(p6);
  
          Process p7 = new Process();
          p7.arrival = 0;
          p7.end = 0;
          p7.name = "P7";
          p7.cpuburst = new int[]{ 21, 23, 24, 22, 21, 20,0,0,0,0 };
          p7.ioburst = new int[] { 51, 53, 61, 31, 43, 0, 0, 0, 0, 0 };
          p7.totburst = 131;
          processes.Add(p7);
  
          Process p8 = new Process();
          p8.arrival = 0;
          p8.end = 0;
          p8.name = "P8";
          p8.cpuburst = new int[] { 11, 14, 15, 17, 16, 12, 13, 15, 0, 0 };
          p8.ioburst = new int[] { 52, 42, 31, 21, 43, 31, 32, 0, 0, 0 };
          p8.totburst = 113;
          processes.Add(p8);
  
  
  
  
          FCFS(processes);
          SJF(processes);
  
      }
  </code></pre>
  
  <p>In my FCFS function I call other functions to perform the calculations</p>
  
  <pre><code>void FCFS(List&lt;Process&gt; p)
      {
          output.Items.Add("After FCFS");
          output.Items.Add("-------------------------------------------------------");
          wait(p);
          turnaroundtime(p);
          responsetime(p);
          completeoutputlist(p);
      }
  </code></pre>
  
  <p>my attempts at wait time have failed and I really don't know how to best approach this. I want to get these values calculated here so then I can call them in my other functions.
  I'm mostly stuck on how to take I/o time into account when calculating the clock</p>
  
  <p>the processes are doing I/O at the same time one other process is running. So, I/O should be decremented as timer is incremented for every process doing I/O.
  multiple processes can be doing i/o at the same time. So you would need a list of processes that are doing i/o.
  really just need some guidance in this part.</p>
  
  <pre><code>void wait(List&lt;Process&gt; p)
      {
  
  
          /* Goal keep track of context switches. - p.contextswitch
           *      Keep track of the first time it enters to get response time  - p.start
           *      Keep track of when the process ends - so later we can calc turnaround time p.end
           *      then tweak some functions so it works again
           *      
           */
  
  
  
      }
  </code></pre>
  
  <p>Before I was doing something like this</p>
  
  <pre><code>void wait(List&lt;Process&gt; p)
      {
  
  
          /* Goal keep track of context switches. - p.contextswitch
           *      Keep track of the first time it enters to get response time  - p.start
           *      Keep track of when the process ends - so later we can calc turnaround time p.end
           *      then tweak some functions so it works again
           *      
           */
  
          int k = 0;
          int i = 0;
          int clock = 0;
          int contextswitch = 0;
          int cpuready = 0;
          int ioready = 0;
          double avwaitingtime = 0;
  
  
          for (k = 0; k &lt; 10; k++)
          {
              for (i = 0; i &lt; p.Count; i++)
              {
                  //cpuready = p[i].cpuburst[k];
                  //ioready = p[i].ioburst[k];
  
  
  
  
                  clock += p[i].cpuburst[k];
  
                      //sets arrival time P1
                      if (i == 0 &amp;&amp; k == 0)
                      {
                          p[0].arrival = 0;
  
                      }
                      //sets arrival time P2
                      if (i == 1 &amp;&amp; k == 0)
                      {
                          p[1].arrival = clock;
  
                      }
                      //sets arrival time P3
                      if (i == 2 &amp;&amp; k == 0)
                      {
                          p[2].arrival = clock;
  
                      }
                      //sets arrival time P4
                      if (i == 3 &amp;&amp; k == 0)
                      {
                          p[3].arrival = clock;
  
                      }
                      //sets arrival time P5
                      if (i == 4 &amp;&amp; k == 0)
                      {
                          p[4].arrival = clock;
  
                      }
                      //sets arrival time P6
                      if (i == 5 &amp;&amp; k == 0)
                      {
                          p[5].arrival = clock;
  
                      }
                      //sets arrival time P7
                      if (i == 6 &amp;&amp; k == 0)
                      {
                          p[6].arrival = clock;
  
                      }
                      //sets arrival time P8
                      if (i == 7 &amp;&amp; k == 0)
                      {
                          p[7].arrival = clock;
  
                      }
                      //ADDS TO THE CONTEXT SWITCH CONTER
                      contextswitch += 1;
  
                      //Checks the first row and the last column
                      if (i == 0 &amp;&amp; k == 8)
                      {
  
                          p[0].end = clock;
                      }
                      //Checks the 2nd row and the last column
                      if (i == 1 &amp;&amp; k == 8)
                      {
  
                          p[1].end = clock;
                      }
                      //Checks the 3rd row and the last column
                      if (i == 2 &amp;&amp; k == 8)
                      {
  
                          p[2].end = clock;
                      }
                      //Checks the 4th row and the last column
                      if (i == 3 &amp;&amp; k == 7)
                      {
  
                          p[3].end = clock;
                      }
                      //Checks the 5th row and the last column
                      if (i == 4 &amp;&amp; k == 9)
                      {
  
                          p[4].end = clock;
                      }
                      //Checks the 6th row and the last column
                      if (i == 5 &amp;&amp; k == 6)
                      {
  
                          p[5].end = clock;
                      }
                      //Checks the 7th row and the last column
                      if (i == 6 &amp;&amp; k == 5)
                      {
  
                          p[6].end = clock;
                      }
                      //Checks the 8th row and the last column
                      if (i == 7 &amp;&amp; k == 7)
                      {
  
                          p[7].end = clock;
                      }
  
  
  
  
             }
  
  
         }
          output.Items.Add(clock);    
          double waitsum = 0;
  
              for (i = 0; i &lt; 8; i++)
              {
                  //CALCS THE WAIT TIME 
                  p[i].wait = (p[i].end- p[i].totburst);
                 // output.Items.Add(p[i].wait);
                  //CALCS THE WAITSUM
                  waitsum += p[i].wait;
              }
              //calcs avg wait time
              avwaitingtime = (waitsum / 8.0);
  
              //output.Items.Add(avg);
  
  
  
  
          output.Items.Add(contextswitch);
          cpu_utilization(p, contextswitch,clock);
          for (i = 0; i &lt; 8; i++)
          {
              p[i].avwaitingtime = avwaitingtime;
  
          }
  
      }
  </code></pre>
  
  <p>any sort of ideas on how to approach this would be tons of help, thanks!</p>