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

wirehaze git hosting

tasks implementation (ongoing)
[ppos.git] / ppos / test / pingpong-wait.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_wait() (leve)
9
10 #include <assert.h>
11 #include "lib/libc.h"
12 #include "ppos.h"
13
14 #define WORKLOAD 20000
15
16 static 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 int max, tid;
33
34 tid = task_id(NULL);
35 max = tid * 2;
36
37 printf("%5d ms: %s inicia\n", systime(), (char *)arg);
38 for (int i = 0; i < max; i++)
39 {
40 printf("%5d ms: %s %d\n", systime(), (char *)arg, i);
41 hardwork(WORKLOAD);
42 }
43 printf("%5d ms: %s termina\n", systime(), (char *)arg);
44
45 task_exit(tid);
46 }
47
48 // corpo da tarefa principal
49 void user_main(void *arg)
50 {
51 int exit_code, status;
52
53 printf("user: inicio\n");
54
55 pang = task_create("pang", body, "\tPang");
56 assert(pang);
57 peng = task_create("peng", body, "\t\tPeng");
58 assert(peng);
59 ping = task_create("ping", body, "\t\t\tPing");
60 assert(ping);
61 pong = task_create("pong", body, "\t\t\t\tPong");
62 assert(pong);
63 pung = task_create("pung", body, "\t\t\t\t\tPung");
64 assert(pung);
65
66 for (int i = 0; i < 2; i++)
67 {
68 printf("%5d ms: user %d\n", systime(), i);
69 hardwork(WORKLOAD);
70 }
71
72 printf("%5d ms: user esperando Pang...\n", systime());
73 exit_code = task_wait(pang);
74 assert(exit_code == task_id(pang));
75
76 printf("%5d ms: user esperando Peng...\n", systime());
77 exit_code = task_wait(peng);
78 assert(exit_code == task_id(peng));
79
80 printf("%5d ms: user esperando Ping...\n", systime());
81 exit_code = task_wait(ping);
82 assert(exit_code == task_id(ping));
83
84 printf("%5d ms: user esperando Pong...\n", systime());
85 exit_code = task_wait(pong);
86 assert(exit_code == task_id(pong));
87
88 printf("%5d ms: user esperando Pung...\n", systime());
89 exit_code = task_wait(pung);
90 assert(exit_code == task_id(pung));
91
92 status = task_destroy(pang);
93 assert(status == NOERROR);
94 status = task_destroy(peng);
95 assert(status == NOERROR);
96 status = task_destroy(ping);
97 assert(status == NOERROR);
98 status = task_destroy(pong);
99 assert(status == NOERROR);
100 status = task_destroy(pung);
101 assert(status == NOERROR);
102
103 printf("%5d ms: user termina\n", systime());
104
105 task_exit(0);
106 }