/*tempo.c #Tarefa 1 : Cada nodo sem-falha testa o seguinte no anel.*/ #include #include #include "smpl.h" /* inclusao do SMPL */ /*eventos*/ #define test 1 #define fault 2 #define repair 3 /*descritor do nodo*/ typedef struct { int id /*identidificador de facility*/ /*variaveis sao declaradas aqui*/ }tnodo; tnodo *nodo; main (int argc, char *argv[]){ static int n, token, event, r,i; static char fa_name[5]; if (argc !=2){ puts("use correto: tempo"); exit(1); } n = atoi (argv[1]); smpl(0,"um exemplo de simulação"); reset(); stream(i); /*inicialização*/ nodo= (tnodo*) malloc (sizeof (tnodo)*n); for (i=0; i < n; i++){ memset(fa_name, '\0', 5); sprintf(fa_name, "%d", i); nodo[i].id=facility(fa_name,1); } /*escalonamento inicial dos eventos*/ for (i=0; i < n; i++) schedule(test, 30.0, i); //agendamento de teste schedule(fault, 50.0, 2); //agendamento de falha schedule(repair, 80.0, 2); //agendamento de reparo /*dispara o tempo de simulacao*/ while (time() < 100.0) { cause (&event,&token); switch (event){ case test: //testando os nodos if(status (nodo[token].id)!=0) break; int prox = (token+1)%n; // logica para deslocamento em forma de anel printf("o nodo %d vai testar o nodo %d no tempo %5.1f\n", token, prox, time()); schedule(test,30.0, token); break; case fault: r=request(nodo[token].id,token,0); if (r!=0){ puts("nao foi possivel falhar"); exit(1); } printf("o nodo %d falhou no tempo %5.1f\n", token, time()); break; case repair: printf("o nodo %d recuperou no tempo %5.1f\n", token,time()); release(nodo[token].id,token); schedule(test,30.0,token); break; }}}