title: What is the best way to implement a MLFQ (Multi-level feedback queue) in C?
body: <p>I have a function that looks like this that I need to implement. 
  Threads call this function with these parameters. It's supposed to return with the correct time it accessed the CPU and if it can't access the CPU, it will wait until it can access it. </p>
  
  <p>With regards to the correct time, I keep a global variable that gets updated each time, it calls it.</p>
  
  <p>How do I implement the waits and synchronize it correctly.</p>
  
  <pre><code>int MLFQ(float currentTime, int tid, int remainingTime, int tprio)
  </code></pre>
  
  <p>My code looks something like this so far and it doesn't quite work.</p>
  
  <pre><code>update globalTime (globalTime = currentTime)
  Mutex_lock
  Add to MLFQ if needed (either to 5, 10, 15, 20, or 25 MLFQ)
  if (canAccessCPU)
      getCPU
      unlock mutex
      return globalTime
  else
      mutex_unlock
      return MLFQ(globalTime, tid, remainingTime, tprio);
  </code></pre>