]>
wirehaze git hosting - ppos.git/blob - ppos/test/pingpong-semaphore-stress.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 (stress)
15 #define NUMSTEPS 1000000
17 static struct task_t
*task
[NUMTASKS
];
18 static struct semaphore_t
*s
;
26 for (int i
= 0; i
< NUMSTEPS
; i
++)
28 // incrementa contador (seção crítica)
43 // corpo da tarefa principal
44 void user_main(void *arg
)
48 printf("user: inicio\n");
50 // inicia semáforo em 0 (bloqueado)
54 printf("%d tarefas somando %d vezes cada, aguarde...\n",
58 for (int i
= 0; i
< NUMTASKS
; i
++)
60 task
[i
] = task_create(NULL
, body
, "Task");
64 // espera um pouco para liberar o semáforo; isso faz com que todas
65 // as tarefas tenham sido criadas e possam competir pelo semáforo
66 // em pé de igualdade.
69 assert(status
== NOERROR
);
71 // aguarda as tarefas encerrarem
72 for (int i
= 0; i
< NUMTASKS
; i
++)
74 status
= task_wait(task
[i
]);
75 assert(status
== NOERROR
);
79 status
= sem_destroy(s
);
80 assert(status
== NOERROR
);
82 // verifica se a soma está correta
83 if (soma
== (NUMTASKS
* NUMSTEPS
))
84 printf("A soma deu %d, valor correto!\n", soma
);
86 printf("A soma deu %d, mas deveria dar %d!\n",
87 soma
, NUMTASKS
* NUMSTEPS
);
89 // destroi os descritores
90 for (int i
= 0; i
< NUMTASKS
; i
++)
92 status
= task_destroy(task
[i
]);
93 assert(status
== NOERROR
);
96 printf("user: fim\n");