]>
wirehaze git hosting - ppos.git/blob - ppos/test/pingpong-semaphore.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 de semáforos (leve)
14 static struct task_t
*a1
, *a2
, *b1
, *b2
;
15 static struct semaphore_t
*s1
, *s2
;
18 void body_a(void *arg
)
22 for (int i
= 0; i
< 5; i
++)
24 status
= sem_down(s1
);
28 printf("%5d ms: %s zig (%d)\n", systime(), (char *)arg
, i
);
39 void body_b(void *arg
)
43 for (int i
= 0; i
< 5; i
++)
45 status
= sem_down(s2
);
49 printf("%5d ms: %s zag (%d)\n", systime(), (char *)arg
, i
);
59 // corpo da tarefa principal
60 void user_main(void *arg
)
64 printf("user: inicio\n");
73 a1
= task_create("a1", body_a
, "A1");
75 a2
= task_create("a2", body_a
, "\tA2");
77 b1
= task_create("b1", body_b
, "\t\t\tB1");
79 b2
= task_create("b2", body_b
, "\t\t\t\tB2");
82 // aguarda a1 encerrar
83 status
= task_wait(a1
);
84 assert(status
== NOERROR
);
87 status
= sem_destroy(s1
);
88 assert(status
== NOERROR
);
89 status
= sem_destroy(s2
);
90 assert(status
== NOERROR
);
92 // aguarda a2, b1 e b2 encerrarem
93 status
= task_wait(a2
);
94 assert(status
== NOERROR
);
95 status
= task_wait(b1
);
96 assert(status
== NOERROR
);
97 status
= task_wait(b2
);
98 assert(status
== NOERROR
);
100 // destroi os descritores das tarefas
101 status
= task_destroy(a1
);
102 assert(status
== NOERROR
);
103 status
= task_destroy(a2
);
104 assert(status
== NOERROR
);
105 status
= task_destroy(b1
);
106 assert(status
== NOERROR
);
107 status
= task_destroy(b2
);
108 assert(status
== NOERROR
);
110 printf("user: fim\n");