ppos

Usermode educational operating system.
Log | Files | Refs

pingpong-preempcao.c (1499B)


      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 preempção por tempo (quantum)
      9 
     10 #include <assert.h>
     11 #include "lib/pplibc.h"
     12 #include "syscall.h"
     13 
     14 #define WORKLOAD 10000
     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("%s: inicio\n", (char *)arg);
     33     for (int i = 0; i < 10; i++)
     34     {
     35         printk("%s: %d\n", (char *)arg, i);
     36         hardwork(WORKLOAD);
     37     }
     38     printk("%s: fim\n", (char *)arg);
     39     task_exit(NOERROR);
     40 }
     41 
     42 // corpo da tarefa principal
     43 void user_main(void *arg)
     44 {
     45     int id;
     46     char *name;
     47 
     48     name = task_name(NULL);
     49     id   = task_id(NULL);
     50     printk("%5u ms: %s (id %d): inicio\n", time(), name, id);
     51 
     52     // cria tarefas
     53     pang = task_create("pang", body, "\tPang");
     54     assert(pang);
     55     peng = task_create("peng", body, "\t\tPeng");
     56     assert(peng);
     57     ping = task_create("ping", body, "\t\t\tPing");
     58     assert(ping);
     59     pong = task_create("pong", body, "\t\t\t\tPong");
     60     assert(pong);
     61     pung = task_create("pung", body, "\t\t\t\t\tPung");
     62     assert(pung);
     63 
     64     printk("%5u ms: %s fim\n", time(), name);
     65 
     66     task_exit(NOERROR);
     67 }