ppos

Usermode educational operating system.
Log | Files | Refs

pingpong-task2.c (1766B)


      1 // PingPongOS - PingPong Operating System
      2 // Prof. Carlos A. Maziero, DINF UFPR
      3 // Versão 2.1 -- 07/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 gestão básica de tarefas
      9 
     10 #include <assert.h>
     11 #include "lib/pplibc.h"
     12 #include "syscall.h"
     13 
     14 #define NUMTASKS 512
     15 
     16 static struct task_t *task[NUMTASKS];
     17 
     18 // corpo das tarefas
     19 void body_task(void *arg)
     20 {
     21     int id;
     22     int next;
     23 
     24     id = task_id(NULL);
     25     printk("\tIniciei  tarefa %5d\n", id);
     26 
     27     // passa o controle para a proxima tarefa
     28     next = ((long)arg + 1) % NUMTASKS;
     29     printk("\tVou mudar para a tarefa %d\n", task_id(task[next]));
     30     task_switch(task[next]);
     31 
     32     printk("\tEncerrei tarefa %5d\n", id);
     33 
     34     task_switch(NULL);
     35 }
     36 
     37 // corpo da tarefa principal
     38 void user_main(void *arg)
     39 {
     40     int status, id;
     41     char *name;
     42 
     43     name = task_name(NULL);
     44     id   = task_id(NULL);
     45     printk("%5u ms: %s (id %d): inicio\n", time(), name, id);
     46 
     47     // inicia tarefas
     48     for (long i = 0; i < NUMTASKS; i++)
     49     {
     50         task[i] = task_create(NULL, body_task, (void *)i);
     51         assert(task[i]);
     52         printk("%s: criei a tarefa %d\n", name, task_id(task[i]));
     53     }
     54 
     55     // passa o controle para cada uma delas em sequencia
     56     for (long i = 0; i < NUMTASKS; i++)
     57     {
     58         printk("%s: vou ativar a tarefa %d\n", name, task_id(task[i]));
     59         status = task_switch(task[i]);
     60         assert(status == NOERROR);
     61     }
     62 
     63     // destrói as tarefas
     64     for (long i = 0; i < NUMTASKS; i++)
     65     {
     66         printk("%s: vou destruir a tarefa %d\n", name, task_id(task[i]));
     67         status = task_destroy(task[i]);
     68         assert(status == NOERROR);
     69     }
     70 
     71     printk("%5u ms: %s fim\n", time(), name);
     72 
     73     task_switch(NULL);
     74 }