ppos

Usermode educational operating system.
Log | Files | Refs

pingpong-contab-prio.c (1674B)


      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 da contabilização com tarefas de prioridades distintas
      9 
     10 #include <assert.h>
     11 #include "lib/pplibc.h"
     12 #include "syscall.h"
     13 
     14 #define WORKLOAD 40000
     15 
     16 struct task_t *pang, *peng, *ping, *pong, *pung;
     17 
     18 // simula um processamento pesado
     19 int hardwork(int n)
     20 {
     21     int soma = 0;
     22 
     23     for (int i = 0; i < n; i++)
     24         for (int j = 0; j < n; j++)
     25             soma += j;
     26     return (soma);
     27 }
     28 
     29 // corpo das tarefas
     30 void body(void *arg)
     31 {
     32     printk("%5u ms: %s inicia (prio: %d)\n", time(),
     33            (char *)arg, sched_getprio(0));
     34     hardwork(WORKLOAD);
     35     printk("%5u ms: %s termina\n", time(), (char *)arg);
     36     task_exit(NOERROR);
     37 }
     38 
     39 // corpo da tarefa principal
     40 void user_main(void *arg)
     41 {
     42     int id;
     43     char *name;
     44 
     45     name = task_name(NULL);
     46     id   = task_id(NULL);
     47     printk("%5u ms: %s (id %d): inicio\n", time(), name, id);
     48 
     49     // cria tarefas
     50     pang = task_create("pang", body, "\tPang");
     51     assert(pang);
     52     peng = task_create("peng", body, "\t\tPeng");
     53     assert(peng);
     54     ping = task_create("ping", body, "\t\t\tPing");
     55     assert(ping);
     56     pong = task_create("pong", body, "\t\t\t\tPong");
     57     assert(pong);
     58     pung = task_create("pung", body, "\t\t\t\t\tPung");
     59     assert(pung);
     60 
     61     // ajusta prioridades
     62     sched_setprio(pang,  0);
     63     sched_setprio(peng, -2);
     64     sched_setprio(ping, -4);
     65     sched_setprio(pong, -6);
     66     sched_setprio(pung, -8);
     67 
     68     printk("%5u ms: %s fim\n", time(), name);
     69 
     70     task_exit(NOERROR);
     71 }