Advertisement
Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. void enqueue(
  2.   register struct proc *rp      /* this process is now runnable */
  3. )
  4. {
  5. /* Add 'rp' to one of the queues of runnable processes.  This function is
  6.  * responsible for inserting a process into one of the scheduling queues.
  7.  * The mechanism is implemented here.   The actual scheduling policy is
  8.  * defined in sched() and pick_proc().
  9.  *
  10.  * This function can be used x-cpu as it always uses the queues of the cpu the
  11.  * process is assigned to.
  12.  */
  13.   int q = rp->p_priority;                       /* scheduling queue to use */
  14.   struct proc **rdy_head, **rdy_tail;
  15.  
  16.   assert(proc_is_runnable(rp));
  17.  
  18.   assert(q >= 0);
  19.  
  20.   rdy_head = get_cpu_var(rp->p_cpu, run_q_head);
  21.   rdy_tail = get_cpu_var(rp->p_cpu, run_q_tail);
  22.  
  23.   /* Now add the process to the queue. */
  24.   if (!rdy_head[q]) {           /* add to empty queue */
  25.     rdy_head[q] = rdy_tail[q] = rp;           /* create a new queue */
  26.     rp->p_nextready = NULL;           /* mark new end */
  27.   }
  28.   else if (q != SJF_Q) {                      /* add to tail of queue */
  29.     rdy_tail[q]->p_nextready = rp;            /* chain tail of queue */
  30.     rdy_tail[q] = rp;                         /* set new queue tail */
  31.     rp->p_nextready = NULL;           /* mark new end */
  32.   } else {
  33.     struct proc* front;
  34.     struct proc* back;
  35.  
  36.     front = rdy_head[q];
  37.     while (front && front->expected_time < rp->expected_time) {
  38.       back = front;
  39.       front = front->p_nextready;
  40.     }
  41.  
  42.     if (front == rdy_head[q]) {
  43.       rp->p_nextready = rdy_head[q];
  44.       rdy_head[q] = rp;
  45.     } else {
  46.       rp->p_nextready = front;
  47.       back->p_nextready = rp;
  48.       if (back == rdy_tail[q]) {
  49.         rdy_tail[q] = rp;
  50.       }
  51.     }
  52.   }
  53.  
  54.   if (cpuid == rp->p_cpu) {
  55.     /*
  56.       * enqueueing a process with a higher priority than the current one,
  57.       * it gets preempted. The current process must be preemptible. Testing
  58.       * the priority also makes sure that a process does not preempt itself
  59.       */
  60.     struct proc * p;
  61.     p = get_cpulocal_var(proc_ptr);
  62.     assert(p);
  63.     if((p->p_priority > rp->p_priority) &&
  64.        (priv(p)->s_flags & PREEMPTIBLE))
  65.       RTS_SET(p, RTS_PREEMPTED); /* calls dequeue() */
  66.   }
  67. #ifdef CONFIG_SMP
  68.   /*
  69.    * if the process was enqueued on a different cpu and the cpu is idle, i.e.
  70.    * the time is off, we need to wake up that cpu and let it schedule this new
  71.    * process
  72.    */
  73.   else if (get_cpu_var(rp->p_cpu, cpu_is_idle)) {
  74.     smp_schedule(rp->p_cpu);
  75.   }
  76. #endif
  77.  
  78.   /* Make note of when this process was added to queue */
  79.   read_tsc_64(&(get_cpulocal_var(proc_ptr)->p_accounting.enter_queue));
  80.  
  81.  
  82. #if DEBUG_SANITYCHECKS
  83.   assert(runqueues_ok_local());
  84. #endif
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement