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

wirehaze git hosting

tasks implementation (ongoing)
[ppos.git] / ppos / test / pingpong-sleep.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 função task_sleep()
9
10 #include <assert.h>
11 #include "lib/libc.h"
12 #include "ppos.h"
13
14 static struct task_t *pang, *peng, *ping, *pong, *pung;
15
16 // corpo das tarefas
17 void body(void *arg)
18 {
19 int i, t_sleep, t_before, t_after;
20 char *status;
21
22 printf("%5d ms: %s: inicio\n", systime(), (char *)arg);
23 for (i = 0; i < 20; i++)
24 {
25 // sorteia tempo entre 0 e 2000 ms (2s), em saltos de 100 ms
26 t_sleep = 100 * (randnum() % 21);
27
28 // informa o quanto vai dormir
29 printf("%5d ms: %s vai dormir %d ms\n",
30 systime(), (char *)arg, t_sleep);
31
32 // registra tempo antes e depois de dormir
33 t_before = systime();
34 task_sleep(t_sleep);
35 t_after = systime();
36
37 // verifica se dormiu o intervalo especificado
38 status = (t_after - t_before) == t_sleep ? "ok" : "ERRADO";
39
40 // informa o quanto efetivamente dormiu
41 printf("%5d ms: %s dormiu %d ms (%s)\n", systime(),
42 (char *)arg, t_after - t_before, status);
43 }
44 printf("%5d ms: %s: fim\n", systime(), (char *)arg);
45 task_exit(0);
46 }
47
48 // corpo da tarefa principal
49 void user_main(void *arg)
50 {
51 int status;
52
53 printf("%5d ms: user: inicio\n", systime());
54
55 // cria tarefas
56 pang = task_create("pang", body, "\tPang");
57 assert(pang);
58 peng = task_create("peng", body, "\t\tPeng");
59 assert(peng);
60 ping = task_create("ping", body, "\t\t\tPing");
61 assert(ping);
62 pong = task_create("pong", body, "\t\t\t\tPong");
63 assert(pong);
64 pung = task_create("pung", body, "\t\t\t\t\tPung");
65 assert(pung);
66
67 // aguarda tarefas concluirem
68 printf("%5d ms: user: espera Pang...\n", systime());
69 status = task_wait(pang);
70 assert(status == NOERROR);
71 printf("%5d ms: user: Pang acabou\n", systime());
72
73 printf("%5d ms: user: espera Peng...\n", systime());
74 status = task_wait(peng);
75 assert(status == NOERROR);
76 printf("%5d ms: user: Peng acabou\n", systime());
77
78 printf("%5d ms: user: espera Ping...\n", systime());
79 status = task_wait(ping);
80 assert(status == NOERROR);
81 printf("%5d ms: user: Ping acabou\n", systime());
82
83 printf("%5d ms: user: espera Pong...\n", systime());
84 status = task_wait(pong);
85 assert(status == NOERROR);
86 printf("%5d ms: user: Pong acabou\n", systime());
87
88 printf("%5d ms: user: espera Pung...\n", systime());
89 status = task_wait(pung);
90 assert(status == NOERROR);
91 printf("%5d ms: user: Pung acabou\n", systime());
92
93 // destroi descritores
94 status = task_destroy(pang);
95 assert(status == NOERROR);
96 status = task_destroy(peng);
97 assert(status == NOERROR);
98 status = task_destroy(ping);
99 assert(status == NOERROR);
100 status = task_destroy(pong);
101 assert(status == NOERROR);
102 status = task_destroy(pung);
103 assert(status == NOERROR);
104
105 printf("%5d ms: user: fim\n", systime());
106
107 task_exit(0);
108 }