title: Scheduler in xv6
body: <p>I am trying to implement mixture of MLFQ and lottery scheduler in xv6. The problem I am encountering is I am making a function which calculates the total number of processes with high priority in a queue and the sum of their tickets. When I print the no_of_tickets within the loop it prints correct value. But when I am printing it before return value. It prints zero and go into forever loop.
  Here is my code</p>
  
  <pre><code>int no_of_processes = 0;
  int count(int q_rank)
  {
  struct proc *p; int no_of_tickets = 0;
  for(p = ptable.proc; p &lt; &amp;ptable.proc[NPROC]; p++){
           if(p-&gt;queue_rank==q_rank)
      {
      if(p-&gt;state != RUNNABLE)
              continue;
              no_of_tickets = (no_of_tickets + (p-&gt;tickets));
              no_of_processes++;
      }
  }
  return no_of_tickets;
  }
  
  struct pstat pstat;
  
  
  void
      scheduler(void)
      {
       struct proc *p;
       struct proc *p1;
       int f;
       for(f=0; f&lt;NPROC; f++)
       {
       pstat.hticks[f]=0;
       pstat.lticks[f]=0;
       pstat.inuse[f]=0;
       }
       for(;;){
      int h_index=-1;
      int l_index=-1;
      sti();
      acquire(&amp;ptable.lock);
      for(p1 = ptable.proc; p1 &lt; &amp;ptable.proc[NPROC]; p1++){
       int no_of_tickets = count(1);
       cprintf("%d", no_of_tickets);
       for(p = ptable.proc; p &lt; &amp;ptable.proc[NPROC]; p++){
       if(p-&gt;queue_rank == 1)
      {
        h_index++;
        if(p-&gt;state != RUNNABLE)
  
        continue;
        p-&gt;queue_rank=0;
         (p-&gt;hticks)++;
         pstat.inuse[h_index]=1;
         pstat.pid[h_index]=p-&gt;pid;
         pstat.hticks[h_index]=p-&gt;hticks;
     run(p);
  }
  }
      l_index++;  
    if((p1-&gt;state != RUNNABLE) || (p1-&gt;queue_rank != 0))
      continue;
      (p1-&gt;lticks)++;
      pstat.lticks[l_index]=p1-&gt;lticks;
      run(p1);
      if(p1-&gt;state != ZOMBIE)
  {
      run(p1);
  }
  }
     release(&amp;ptable.lock);
    }
    }
  </code></pre>