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

wirehaze git hosting

add ppos/
[ppos.git] / ppos / test / pingpong-wait-stress.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() com muitas tarefas
9
10 #include <assert.h>
11 #include "lib/libc.h"
12 #include "ppos.h"
13
14 #define WORKLOAD 3000
15 #define NUMTASKS 512
16
17 static struct task_t *task[NUMTASKS];
18
19 // simula um processamento pesado
20 int hardwork(int n)
21 {
22 int soma = 0;
23
24 for (int i = 0; i < n; i++)
25 for (int j = 0; j < n; j++)
26 soma += j;
27 return (soma);
28 }
29
30 // corpo das tarefas
31 void body()
32 {
33 int max = 1 + randnum() % 5 ;
34
35 for (int i = 0; i < max; i++)
36 hardwork(WORKLOAD + randnum() % WORKLOAD);
37
38 printf("task %3d: fim\n", task_id(NULL));
39 task_exit(task_id(NULL));
40 }
41
42 // corpo da tarefa principal
43 void user_main(void *arg)
44 {
45 int status, exit_code;
46
47 printf("user: inicio\n");
48
49 // cria tarefas
50 printf("Creating %d tasks\n", NUMTASKS);
51 for (int i = 0; i < NUMTASKS; i++)
52 {
53 task[i] = task_create(NULL, body, NULL);
54 assert(task[i]);
55 }
56
57 // espera tarefas encerrarem
58 for (int i = 0; i < NUMTASKS; i++)
59 {
60 printf("Waiting for task %d\n", task_id(task[i]));
61 exit_code = task_wait(task[i]);
62 assert(exit_code == task_id(task[i]));
63 }
64
65 // destroi descritores
66 for (int i = 0; i < NUMTASKS; i++)
67 {
68 status = task_destroy(task[i]);
69 assert(status == NOERROR);
70 }
71
72 printf("user: fim\n");
73
74 task_exit(0);
75 }