]>
wirehaze git hosting - ppos.git/blob - ppos/test/pingpong-mqueue.c
1 // PingPongOS - PingPong Operating System
2 // Prof. Carlos A. Maziero, DINF UFPR
3 // Versão 2.0 -- Junho de 2025
5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO;
6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO.
8 // Teste das filas de mensagens
14 // estrutura com vários valores para teste
20 static struct task_t
*prod
[3], *mult
, *cons
[2];
21 static struct mqueue_t
*fila_inteiros
, *fila_pacotes
;
23 // corpo da tarefa produtor
29 name
= task_name(NULL
);
31 printf("%5d ms: %s inicia\n", systime(), name
);
35 // sorteia um valor inteiro e o envia na fila de valores
36 valor
= 1 + randnum() % 20;
37 status
= mqueue_send(fila_inteiros
, &valor
);
40 printf("%5d ms: %s envia %d\n", systime(), name
, valor
);
42 // dorme um intervalo aleatorio
43 task_sleep(randnum() % 3000);
46 printf("%5d ms: %s termina\n", systime(), name
);
50 // corpo da tarefa agrupador
53 struct pacote_t pacote
;
57 name
= task_name(NULL
);
59 printf("%5d ms: \t\t\t%s inicia\n", systime(), name
);
61 for (int i
= 0; i
< 10; i
++)
63 // recebe N valores inteiros e os põe no pacote
65 for (int j
= 0; j
< 3; j
++)
67 status
= mqueue_recv(fila_inteiros
, &pacote
.v
[j
]);
68 assert(status
== NOERROR
);
70 printf("%5d ms: \t\t\t%s recebe %d\n", systime(), name
,
72 pacote
.prod
*= pacote
.v
[j
] ;
76 printf("%5d ms: \t\t\t%s envia [%d*%d*%d = %d]\n",
77 systime(), name
, pacote
.v
[0], pacote
.v
[1], pacote
.v
[2],
81 status
= mqueue_send(fila_pacotes
, &pacote
);
82 assert(status
== NOERROR
);
84 // dorme um intervalo aleatorio
85 task_sleep(randnum() % 3000);
88 printf("%5d ms: \t\t\t%s termina\n", systime(), name
);
92 // corpo da tarefa consumidor
95 struct pacote_t pacote
;
99 name
= task_name(NULL
);
101 printf("%5d ms: \t\t\t\t\t\t%s inicia\n", systime(), name
);
105 // recebe um pacote e o imprime
106 status
= mqueue_recv(fila_pacotes
, &pacote
);
109 printf("%5d ms: \t\t\t\t\t\t%s recebe [%d*%d*%d = %d]\n",
110 systime(), name
, pacote
.v
[0], pacote
.v
[1], pacote
.v
[2],
113 // dorme um intervalo aleatorio
114 task_sleep(randnum() % 3000);
117 printf("%5d ms: \t\t\t\t\t\t%s termina\n", systime(), name
);
121 // corpo da tarefa principal
122 void user_main(void *arg
)
126 printf("%5d ms: user inicia\n", systime());
128 // cria as filas de mensagens (5 valores cada)
129 fila_inteiros
= mqueue_create(5, sizeof(int));
130 assert(fila_inteiros
);
131 fila_pacotes
= mqueue_create(5, sizeof(struct pacote_t
));
132 assert(fila_pacotes
);
135 mult
= task_create("agrup", body_agrup
, NULL
);
137 cons
[0] = task_create("cons0", body_cons
, NULL
);
139 cons
[1] = task_create("cons1", body_cons
, NULL
);
141 prod
[0] = task_create("prod0", body_prod
, NULL
);
143 prod
[1] = task_create("prod1", body_prod
, NULL
);
145 prod
[2] = task_create("prod2", body_prod
, NULL
);
148 // aguarda o multiplicador encerrar
149 status
= task_wait(mult
);
150 assert(status
== NOERROR
);
152 // destroi as filas de mensagens
153 printf("%5d ms: user destroi fila_inteiros\n", systime());
154 status
= mqueue_destroy(fila_inteiros
);
155 assert(status
== NOERROR
);
156 printf("%5d ms: user destroi fila_pacotes\n", systime());
157 status
= mqueue_destroy(fila_pacotes
);
158 assert(status
== NOERROR
);
160 printf("%5d ms: user termina\n", systime());