]> wirehaze git hosting - ppos.git/blob - ppos/test/pingpong-task1.c

wirehaze git hosting

add ppos/
[ppos.git] / ppos / test / pingpong-task1.c
1 // PingPongOS - PingPong Operating System
2 // Prof. Carlos A. Maziero, DINF UFPR
3 // Versão 2.0 -- Junho de 2025
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/libc.h"
12 #include "ppos.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 printf("\t%s: inicio\n", name);
22 for (int i = 0; i < 4; i++)
23 {
24 printf("\t%s: %d\n", name, i);
25 task_switch(pong);
26 }
27 printf("\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 printf("\t\t%s: inicio\n", name);
37 for (int i = 0; i < 4; i++)
38 {
39 printf("\t\t%s: %d\n", name, i);
40 task_switch(ping);
41 }
42 printf("\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;
50 char *name = task_name(NULL);
51
52 printf("%s: inicio\n", name);
53
54 ping = task_create("ping", body_ping, NULL);
55 assert(ping);
56 pong = task_create("pong", body_pong, NULL);
57 assert(pong);
58
59 status = task_switch(ping);
60 assert(status == NOERROR);
61
62 status = task_switch(pong);
63 assert(status == NOERROR);
64
65 printf("%s: fim\n", name);
66
67 status = task_destroy(ping);
68 assert(status == NOERROR);
69
70 status = task_destroy(pong);
71 assert(status == NOERROR);
72
73 task_switch(NULL);
74 }