ppos

Usermode educational operating system.
Log | Files | Refs

pingpong-scheduler.c (1504B)


      1 // PingPongOS - PingPong Operating System
      2 // © Prof. Carlos A. Maziero, DINF UFPR
      3 // Versão 2.1 -- 06/2026
      4 
      5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO;
      6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO.
      7 
      8 // Teste do escalonador com prioridades dinâmicas
      9 
     10 #include <assert.h>
     11 #include "lib/pplibc.h"
     12 #include "syscall.h"
     13 
     14 struct task_t *pang, *peng, *ping, *pong, *pung;
     15 
     16 // corpo das tarefas
     17 void body(void *arg)
     18 {
     19     printk("%s: inicio (prioridade %d)\n",
     20            (char *)arg, sched_getprio(NULL));
     21 
     22     for (int i = 0; i < 10; i++)
     23     {
     24         printk("%s: %d\n", (char *)arg, i);
     25         task_yield();
     26     }
     27     printk("%s: fim\n", (char *)arg);
     28     task_exit(NOERROR);
     29 }
     30 
     31 // corpo da tarefa principal
     32 void user_main(void *arg)
     33 {
     34     int id;
     35     char *name;
     36 
     37     name = task_name(NULL);
     38     id   = task_id(NULL);
     39     printk("%5u ms: %s (id %d): inicio\n", time(), name, id);
     40 
     41     // cria tarefas
     42     pang = task_create("pang", body, "\tPang");
     43     assert(pang);
     44     peng = task_create("peng", body, "\t\tPeng");
     45     assert(peng);
     46     ping = task_create("ping", body, "\t\t\tPing");
     47     assert(ping);
     48     pong = task_create("pong", body, "\t\t\t\tPong");
     49     assert(pong);
     50     pung = task_create("pung", body, "\t\t\t\t\tPung");
     51     assert(pung);
     52 
     53     // ajusta prioridades
     54     sched_setprio(pang, 0);
     55     sched_setprio(peng, 2);
     56     sched_setprio(ping, 4);
     57     sched_setprio(pong, 6);
     58     sched_setprio(pung, 8);
     59 
     60     printk("%5u ms: %s fim\n", time(), name);
     61 
     62     task_exit(NOERROR);
     63 }