pingpong-sleep.c (3184B)
1 // PingPongOS - PingPong Operating System 2 // © Prof. Carlos A. Maziero, DINF UFPR 3 // Versão 2.1 -- 06/2026 4 5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO; 6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO. 7 8 // Teste da função task_sleep() 9 10 #include <assert.h> 11 #include "lib/pplibc.h" 12 #include "syscall.h" 13 14 struct task_t *pang, *peng, *ping, *pong, *pung; 15 16 // corpo das tarefas 17 void body(void *arg) 18 { 19 int i, t_sleep, t_before, t_after; 20 char *status; 21 22 printk("%5u ms: %s: inicio\n", time(), (char *)arg); 23 for (i = 0; i < 20; i++) 24 { 25 // sorteia tempo entre 0 e 2000 ms (2s), em saltos de 100 ms 26 t_sleep = 100 * (randnum() % 21); 27 28 // informa o quanto vai dormir 29 printk("%5u ms: %s vai dormir %d ms\n", 30 time(), (char *)arg, t_sleep); 31 32 // registra tempo antes e depois de dormir 33 t_before = time(); 34 task_sleep(t_sleep); 35 t_after = time(); 36 37 // verifica se dormiu o intervalo especificado 38 status = (t_after - t_before) == t_sleep ? "ok" : "ERRADO"; 39 40 // informa o quanto efetivamente dormiu 41 printk("%5u ms: %s dormiu %d ms (%s)\n", time(), 42 (char *)arg, t_after - t_before, status); 43 } 44 printk("%5u ms: %s: fim\n", time(), (char *)arg); 45 task_exit(NOERROR); 46 } 47 48 // corpo da tarefa principal 49 void user_main(void *arg) 50 { 51 int status, id; 52 char *name; 53 54 name = task_name(NULL); 55 id = task_id(NULL); 56 printk("%5u ms: %s (id %d): inicio\n", time(), name, id); 57 58 // cria tarefas 59 pang = task_create("pang", body, "\tPang"); 60 assert(pang); 61 peng = task_create("peng", body, "\t\tPeng"); 62 assert(peng); 63 ping = task_create("ping", body, "\t\t\tPing"); 64 assert(ping); 65 pong = task_create("pong", body, "\t\t\t\tPong"); 66 assert(pong); 67 pung = task_create("pung", body, "\t\t\t\t\tPung"); 68 assert(pung); 69 70 // aguarda tarefas concluírem 71 printk("%5u ms: user: espera Pang...\n", time()); 72 status = task_wait(pang); 73 assert(status == NOERROR); 74 printk("%5u ms: user: Pang acabou\n", time()); 75 76 printk("%5u ms: user: espera Peng...\n", time()); 77 status = task_wait(peng); 78 assert(status == NOERROR); 79 printk("%5u ms: user: Peng acabou\n", time()); 80 81 printk("%5u ms: user: espera Ping...\n", time()); 82 status = task_wait(ping); 83 assert(status == NOERROR); 84 printk("%5u ms: user: Ping acabou\n", time()); 85 86 printk("%5u ms: user: espera Pong...\n", time()); 87 status = task_wait(pong); 88 assert(status == NOERROR); 89 printk("%5u ms: user: Pong acabou\n", time()); 90 91 printk("%5u ms: user: espera Pung...\n", time()); 92 status = task_wait(pung); 93 assert(status == NOERROR); 94 printk("%5u ms: user: Pung acabou\n", time()); 95 96 // destrói tarefas 97 status = task_destroy(pang); 98 assert(status == NOERROR); 99 status = task_destroy(peng); 100 assert(status == NOERROR); 101 status = task_destroy(ping); 102 assert(status == NOERROR); 103 status = task_destroy(pong); 104 assert(status == NOERROR); 105 status = task_destroy(pung); 106 assert(status == NOERROR); 107 108 printk("%5u ms: %s fim\n", time(), name); 109 110 task_exit(NOERROR); 111 }