ppos

Usermode educational operating system.
Log | Files | Refs

scheduler.c (1158B)


      1 // GRR20235159 Gabriel Gioia de Brito
      2 // PingPongOS - PingPong Operating System
      3 
      4 // Este arquivo PODE/DEVE ser alterado.
      5 
      6 // Escalonador de tarefas prontas.
      7 
      8 #include "tcb.h"
      9 
     10 #include <queue.h>
     11 
     12 extern struct task_t* current;
     13 
     14 void sched_init()
     15 {
     16 }
     17 
     18 void sched_term()
     19 {
     20 }
     21 
     22 struct task_t* scheduler(struct queue_t* ready)
     23 {
     24     struct task_t* task = queue_head(ready);
     25     if (task == NULL)
     26         return NULL;
     27 
     28     for (struct task_t* it = task; it != NULL; it = queue_next(ready)) {
     29         if (it->prio < task->prio)
     30             task = it;
     31     }
     32     for (struct task_t* it = queue_head(ready); it != NULL;
     33          it                = queue_next(ready)) {
     34         if (it != task && it->prio > -20)
     35             it->prio -= 1;
     36     }
     37     task->prio = task->nice;
     38     return task;
     39 }
     40 
     41 void sched_setprio(struct task_t* task, int prio)
     42 {
     43     if (task == NULL)
     44         task = current;
     45     if (prio < -20 || prio > 20)
     46         return;
     47 
     48     task->nice = prio;
     49     task->prio = prio;
     50 }
     51 
     52 int sched_getprio(struct task_t* task)
     53 {
     54     // Jeito simples de indicar erros, se precisar no futuro...
     55     if (task == NULL)
     56         task = current;
     57 
     58     return task->nice;
     59 }