ppos

Usermode educational operating system.
Log | Files | Refs

pingpong-task1.c (1611B)


      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 static struct task_t *ping, *pong;
     15 
     16 // corpo da tarefa Ping
     17 void body_ping(void *)
     18 {
     19     char *name = task_name(NULL);
     20 
     21     printk("\t%s: inicio\n", name);
     22     for (int i = 0; i < 4; i++)
     23     {
     24         printk("\t%s: %d\n", name, i);
     25         task_switch(pong);
     26     }
     27     printk("\t%s: fim\n", name);
     28     task_switch(NULL);
     29 }
     30 
     31 // corpo da tarefa Pong
     32 void body_pong(void *)
     33 {
     34     char *name = task_name(NULL);
     35 
     36     printk("\t\t%s: inicio\n", name);
     37     for (int i = 0; i < 4; i++)
     38     {
     39         printk("\t\t%s: %d\n", name, i);
     40         task_switch(ping);
     41     }
     42     printk("\t\t%s: fim\n", name);
     43     task_switch(NULL);
     44 }
     45 
     46 // corpo da tarefa principal
     47 void user_main(void *arg)
     48 {
     49     int status, id;
     50     char *name;
     51 
     52     name = task_name(NULL);
     53     id   = task_id(NULL);
     54     printk("%5u ms: %s (id %d): inicio\n", time(), name, id);
     55 
     56     ping = task_create("ping", body_ping, NULL);
     57     assert(ping);
     58     pong = task_create("pong", body_pong, NULL);
     59     assert(pong);
     60 
     61     status = task_switch(ping);
     62     assert(status == NOERROR);
     63 
     64     status = task_switch(pong);
     65     assert(status == NOERROR);
     66 
     67     status = task_destroy(ping);
     68     assert(status == NOERROR);
     69 
     70     status = task_destroy(pong);
     71     assert(status == NOERROR);
     72 
     73     printk("%5u ms: %s fim\n", time(), name);
     74 
     75     task_switch(NULL);
     76 }