]>
wirehaze git hosting - ppos.git/blob - 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
5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO;
6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO.
8 // Teste da função task_sleep()
14 static struct task_t
*pang
, *peng
, *ping
, *pong
, *pung
;
19 int i
, t_sleep
, t_before
, t_after
;
22 printf("%5d ms: %s: inicio\n", systime(), (char *)arg
);
23 for (i
= 0; i
< 20; i
++)
25 // sorteia tempo entre 0 e 2000 ms (2s), em saltos de 100 ms
26 t_sleep
= 100 * (randnum() % 21);
28 // informa o quanto vai dormir
29 printf("%5d ms: %s vai dormir %d ms\n",
30 systime(), (char *)arg
, t_sleep
);
32 // registra tempo antes e depois de dormir
37 // verifica se dormiu o intervalo especificado
38 status
= (t_after
- t_before
) == t_sleep
? "ok" : "ERRADO";
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
);
44 printf("%5d ms: %s: fim\n", systime(), (char *)arg
);
48 // corpo da tarefa principal
49 void user_main(void *arg
)
53 printf("%5d ms: user: inicio\n", systime());
56 pang
= task_create("pang", body
, "\tPang");
58 peng
= task_create("peng", body
, "\t\tPeng");
60 ping
= task_create("ping", body
, "\t\t\tPing");
62 pong
= task_create("pong", body
, "\t\t\t\tPong");
64 pung
= task_create("pung", body
, "\t\t\t\t\tPung");
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());
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());
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());
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());
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());
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
);
105 printf("%5d ms: user: fim\n", systime());