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

wirehaze git hosting

tasks implementation (ongoing)
[ppos.git] / ppos / test / pingpong-disco.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 das operações de acesso a disco com uma única tarefa,
9 // que lê e escreve/altera todos os blocos do disco.
10
11 #include <assert.h>
12 #include "lib/libc.h"
13 #include "ppos.h"
14
15 static int num_blk; // numero de blocos no disco
16 static int blk_size; // tamanho de cada bloco (bytes)
17
18 // corpo da tarefa principal
19 void user_main(void *arg)
20 {
21 int status;
22 unsigned char c, *buffer;
23
24 printf("%5d ms: user: inicio\n", systime());
25
26 // busca geometria do disco
27 num_blk = block_blocks();
28 assert(num_blk);
29 blk_size = block_size();
30 assert(blk_size);
31
32 printf("%5d ms: disco contem %d blocos de %d bytes cada\n",
33 systime(), num_blk, blk_size);
34
35 // aloca o buffer para ler blocos do disco
36 buffer = mem_alloc(blk_size);
37 assert(buffer);
38
39 // lê e imprime todos os blocos do disco, um a um
40 for (int i = 0; i < num_blk; i++)
41 {
42 // lê o bloco i do disco no buffer
43 printf("%5d ms: lendo bloco %d\n", systime(), i);
44 status = block_read(i, buffer);
45 if (status)
46 printf("Erro ao ler bloco %d!\n", i);
47
48 // mostra o conteudo do buffer
49 printf("%5d ms: buffer: [", systime());
50 for (int j = 0; j < blk_size; j++)
51 printf("%c", buffer[j]);
52 printf("]\n");
53 }
54
55 // inicia gerador de números aleatórios
56 randseed(systime());
57
58 // lê e imprime todos os blocos do disco, um a um
59 for (int i = 0; i < num_blk; i++)
60 {
61
62 // preenche o buffer com um caractere aleatório
63 c = 32 + randnum() % 48;
64 for (int j = 0; j < blk_size; j++)
65 buffer[j] = c;
66
67 // escreve o buffer no bloco i do disco
68 printf("%5d ms: escrevendo bloco %d com caracteres \"%c\"\n",
69 systime(), i, c);
70 status = block_write(i, buffer);
71 if (status)
72 printf("Erro ao escrever bloco %d!\n", i);
73 }
74
75 // libera o buffer de blocos do disco
76 mem_free(buffer);
77
78 printf("%5d ms: user fim\n", systime());
79
80 task_exit(0);
81 }