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

wirehaze git hosting

add ppos/
[ppos.git] / ppos / test / pingpong-task2.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 #define NUMTASKS 512
15
16 static struct task_t *task[NUMTASKS];
17
18 // corpo das tarefas
19 void body_task(void *arg)
20 {
21 int id;
22 int next;
23
24 id = task_id(NULL);
25 printf("\tIniciei tarefa %5d\n", id);
26
27 // passa o controle para a proxima tarefa
28 next = ((long)arg + 1) % NUMTASKS;
29 printf("\tVou mudar para a tarefa %d\n", task_id(task[next]));
30 task_switch(task[next]);
31
32 printf("\tEncerrei tarefa %5d\n", id);
33
34 task_switch(NULL);
35 }
36
37 // corpo da tarefa principal
38 void user_main(void *arg)
39 {
40 int status;
41 char *name = task_name(NULL);
42
43 printf("%s: inicio\n", name);
44
45 // inicia tarefas
46 for (long i = 0; i < NUMTASKS; i++)
47 {
48 task[i] = task_create(NULL, body_task, (void *)i);
49 assert(task[i]);
50 printf("%s: criei a tarefa %d\n", name, task_id(task[i]));
51 }
52
53 // passa o controle para cada uma delas em sequencia
54 for (long i = 0; i < NUMTASKS; i++)
55 {
56 printf("%s: vou ativar a tarefa %d\n", name, task_id(task[i]));
57 status = task_switch(task[i]);
58 assert(status == NOERROR);
59 }
60
61 // destroi os descritores
62 for (long i = 0; i < NUMTASKS; i++)
63 {
64 printf("%s: vou destruir a tarefa %d\n", name, task_id(task[i]));
65 status = task_destroy(task[i]);
66 assert(status == NOERROR);
67 }
68
69 printf("%s: fim\n", name);
70
71 task_switch(NULL);
72 }