std::wstring render(bool propper, std::wstring syntax, std::wstring date_format = DATE_FORMAT, DWORD langId = 0) const { if (propper) { // To obtain the appropriate message string from the message file, load the message file with the LoadLibrary function and use the FormatMessage function strEx::replace(syntax, _T("%message%"), render_message(langId)); } else { strEx::replace(syntax, _T("%message%"), _T("%message% needs the descriptions flag set!")); } strEx::replace(syntax, _T("%source%"), get_source()); strEx::replace(syntax, _T("%computer%"), get_computer()); strEx::replace(syntax, _T("%generated%"), strEx::format_date(get_time_generated(), date_format)); strEx::replace(syntax, _T("%written%"), strEx::format_date(get_time_written(), date_format)); strEx::replace(syntax, _T("%generated-raw%"), strEx::itos(pevlr_->TimeGenerated)); strEx::replace(syntax, _T("%written-raw%"), strEx::itos(pevlr_->TimeWritten)); strEx::replace(syntax, _T("%type%"), translateType(eventType())); strEx::replace(syntax, _T("%category%"), strEx::itos(pevlr_->EventCategory)); strEx::replace(syntax, _T("%facility%"), strEx::itos(facility())); strEx::replace(syntax, _T("%qualifier%"), strEx::itos(facility())); strEx::replace(syntax, _T("%customer%"), strEx::itos(customer())); strEx::replace(syntax, _T("%rawid%"), strEx::itos(raw_id())); strEx::replace(syntax, _T("%severity%"), translateSeverity(severity())); strEx::replace(syntax, _T("%strings%"), enumStrings()); strEx::replace(syntax, _T("%log%"), file_); strEx::replace(syntax, _T("%file%"), file_); strEx::replace(syntax, _T("%id%"), strEx::itos(eventID())); strEx::replace(syntax, _T("%user%"), userSID()); return syntax; }
void init(){ /* Initialize two facilities to represent servers. */ f1 = facility("facility 1"); f2 = facility("facility 2"); /* Initialize an event to signal simulation completion */ done = event("done"); /* Initialize some boxes for bookkeeping. */ queue_box = box("queue"); queue_box2 = box("queue 2"); service_box = box("in service"); service_box2 = box("in service 2"); }
boost::optional<ModelObject> Building_Impl::facilityAsModelObject() const { OptionalModelObject result; OptionalFacility intermediate = facility(); if (intermediate) { result = *intermediate; } return result; }
int main (int argc, char*argv[]) { static int n, /* # de nodos */ token, event, r, i; static char fa_name[5]; if (argc != 2) { printf("Uso incorreto. Sintaxe: '%s <# de nodos>'\n",argv[0]); exit(1); } n=atoi(argv[1]); smpl(0,"um exemplo de simulacao"); reset(); stream(1); /* INICIALIZACAO */ 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); /* Aqui inicializa as variáveis locais */ } /* escalonamento ds eventos */ for (i=0; i<n; i++) schedule(test,30.0,i); schedule(fault,50.0,2); schedule(repair,80.0,2); while(time() < 100) { cause(&event,&token); switch(event) { case test: if(status(nodo[token].id) != 0) break; printf("O nodo %d vai estar no tempo %5.1f\n",token,time()); schedule(test,30.0,token); break; case fault: r=request(nodo[token].id,token,0); if(r!=0) { printf("Nao foi possivel falhar o nodo %d\n",token); exit(1); } printf("O nodo %d falhou no tempo %5.1f\n", token, time()); break; case repair: printf("O nodo %d se recuperou no tempo %5.1f\n", token, time()); release(nodo[token].id,token); schedule(test,30.0,token); break; } } }
static void usageerror() { char msg[] = "\ ex1_initiator [-c callingTitle][-d FAC][-m maxPDU][-t title][-v] node port\n\ \n\ -c Set calling AE title to callingTitle in Association RQ\n\ -d Place one facility(DCM, DUL, SRV) in verbose mode\n\ -m Set maximum PDU in Association RQ to maxPDU\n\ -t Set called AE title to title in Association RQ\n\ -v Place DUL and SRV facilities in verbose mode\n\ \n\ node Node name for network connection\n\ port TCP / IP port number of server application\n"; fprintf(stderr, msg); exit(1); }
/** * Create process list * @param pnum Number of processes in the system * @return List of processes of the system */ process init_processes(int pnum) { process plist = malloc(sizeof(struct process_t) * pnum); if (plist == NULL) return NULL; for(int i = 0; i < pnum; ++i) { plist[i].state = ST_RELEASED; plist[i].timestamp = 0; plist[i].request_timestamp = 0; plist[i].recv_queue = facility("recvq", 2 * pnum); plist[i].recvd_from = create_pqueue(2 * pnum); plist[i].nreplies = 0; plist[i].pending = malloc(sizeof(int) * pnum); memset(plist[i].pending, 0, sizeof(int) * pnum); } return plist; }
int main(int argc, char *argv[]){ static int N, /* Numero de nodos */ token, event, teste, r, i; static char fa_name[5]; /* Facility Name */ char *fail_code[2] = {"Sem falha", "Falho"}; if(argc != 2){ printf("USO: %s num_nodos\n", argv[0]); exit(1); } N = atoi(argv[1]); smpl(0, "Um Exemplo"); reset(); stream(1); /* Inicializacao */ 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); nodo[i].stats = -1; /* Aqui inicializa as variaveis locais */ } /* Escalonamento de eventos */ for(i=0; i<N; i++){ schedule(test, 30.0, i); } schedule(fault, 50.0, 2); /* O nodo 2 vai falhar em 50.0 */ schedule(fault, 50.0, 3); schedule(fault, 50.0, 4); schedule(repair, 100.0, 2); while(time() < 200){ cause(&event, &token); switch(event){ case test: if(status(nodo[token].id) != 0) break; teste = status(nodo[(token+1)%N].id); i = 1; while(teste){ teste = status(nodo[(token+i)%N].id); if(!teste == 0){ nodo[token].stats = (token+i)%N; i++; } } printf("%5.1f - O nodo %d testou o nodo %d. Resultado: %s\n", time(), token, (token+i)%N, fail_code[teste], nodo[token].stats); schedule(test, 30.0, token); break; case fault: r = request(nodo[token].id, token, 0); if(r != 0){ puts("Nao foi possivel falhar o nodo\n"); exit(1); } printf("%5.1f - O nodo %d falhou\n", time(), token); break; case repair: printf("%5.1f - O nodo %d recuperou\n", time(), token); release(nodo[token].id, token); schedule(test, 30.0, token); break; } } return 0; }
boost::optional<ParentObject> ExteriorLights_Impl::parent() const { return facility(); }
boost::optional<ModelObject> ExteriorLights_Impl::facilityAsModelObject() const { OptionalModelObject result = facility(); return result; }
/* Program body */ int main(int argc, char *argv[]) { static int N; static int token; static int event; static int sender; static int timestamp; static int r; static int i; static int delay; static char fa_name[5]; int scheduled_events = 0; if (argc < 2) { show_usage(); exit(1); } // Number of nodes N = argc-1; // Sets up SMPL simulation smpl(0, "mutual exclusion"); reset(); stream(1); // 1 execution thread // Node intialization nodes = (Node*) malloc(N*sizeof(Node)); int times[N]; for (i=0; i<N; i++) { memset (fa_name, '\0', 5); sprintf(fa_name, "%d", i); nodes[i].id = facility(fa_name, 1); nodes[i].running = false; nodes[i].clock = 0; nodes[i].pending = (bool *) malloc(N*sizeof(bool)); memset (nodes[i].pending, false, N*sizeof(bool)); nodes[i].waiting_reply = 0; times[i] = atoi(argv[i+1]); // Timestamp -1 means that the node has no active request nodes[i].timestamp = -1; } // Schedules the critical region requests for (i=0; i<N; i++) { schedule (REQUEST, times[i], i, i, 0); scheduled_events++; } printf("=============================================================================\n"); printf(" Ricart-Agrawala mutual exclusion algorithm \n"); printf(" Renan Greca - Distributed Systems, May 2016 \n"); printf("=============================================================================\n"); // The program keeps running while there are still events scheduled while (scheduled_events > 0) { printf("-----\n"); cause(&event, &token, &sender, ×tamp); switch(event) { case REQUEST: // REQUEST critical region access to other nodes scheduled_events--; nodes[token].clock++; printf("(%d, %d) Requested C.R. \n", token, nodes[token].clock); // Stores timestamp of request for priority reference nodes[token].timestamp = nodes[token].clock; // Sends requests to all other nodes for (i=0; i<N; i++) { if (i==token) { continue; } delay = rand_int(0, MAX_MESSAGE_DELAY); // Sends REQUEST message to node i with a random delay schedule (RECEIVE_REQUEST, delay, i, token, nodes[token].clock); scheduled_events++; nodes[token].waiting_reply++; } break; case RECEIVE_REQUEST: // Receive and handle a REQUEST scheduled_events--; // Updates logical clock nodes[token].clock = (nodes[token].clock > timestamp ? nodes[token].clock : timestamp)+1; printf("(%d, %d) Received request from %d\n", token, nodes[token].clock, sender); // Checks whether the sender node has priority over the receiver if (priority(sender, token, timestamp, nodes[token].timestamp) && !nodes[token].running) { // If the sender has priority, send a REPLY delay = rand_int(0, MAX_MESSAGE_DELAY); printf("(%d, %d) Sending reply to %d\n", token, nodes[token].clock, sender); schedule(RECEIVE_REPLY, delay, sender, token, nodes[token].clock); scheduled_events++; } else { // Else, store it in the pending array printf("(%d, %d) Adding %d to pending array\n", token, nodes[token].clock, sender); nodes[token].pending[sender] = true; } break; case RECEIVE_REPLY: // Receive and handle a REPLY scheduled_events--; // Updates logical clock nodes[token].clock = (nodes[token].clock > timestamp ? nodes[token].clock : timestamp)+1; printf("(%d, %d) Received reply from %d\n", token, nodes[token].clock, sender); nodes[token].waiting_reply--; // If node received all replies, it enters the critical region if (nodes[token].waiting_reply == 0) { printf("(%d, %d) Entered the critical region.\n", token, nodes[token].clock); schedule (RELEASE, RUNNING_INTERVAL, token, token, nodes[token].clock); scheduled_events++; nodes[token].running = true; } break; case RELEASE: // Release the critical region and alert other nodes scheduled_events--; nodes[token].clock++; printf("(%d, %d) Left the critical region.\n", token, nodes[token].clock); // Clear the information of the previous request nodes[token].running = false; nodes[token].timestamp = -1; // Send all pending REPLIES for (i=0; i<N; i++) { if (nodes[token].pending[i]) { delay = rand_int(0, MAX_MESSAGE_DELAY); printf("(%d, %d) Sending reply to %d\n", token, nodes[token].clock, i); schedule(RECEIVE_REPLY, delay, i, token, nodes[token].clock); scheduled_events++; nodes[token].pending[i] = false; } } break; } } }
/* Program body */ int main(int argc, char *argv[]) { static int N; // Number of nodes static int token; static int event; static int r; static int i; static char fa_name[5]; if (argc != 2) { puts("Uso correto: tempo [num-nodos]"); exit(1); } N = atoi(argv[1]); smpl(0, "programa tempo"); reset(); stream(1); // 1 execution thread // Node intialization nodes = (Node*) malloc(N*sizeof(Node)); for (i=0; i<N; i++) { memset (fa_name, '\0', 5); sprintf(fa_name, "%d", i); nodes[i].id = facility(fa_name, 1); } for (i=0; i<N; i++) { schedule (TEST, 30.0, i); } schedule (FAULT, 31.0, 2); schedule (REPAIR, 61.0, 2); while (time() < 100.0) { cause(&event, &token); switch(event) { case TEST: if(status(nodes[token].id != 0)) { break; } printf("Sou o nodo %d, vou testar no tempo %5.1f\n", token, time()); printf("\n"); schedule(TEST, 30.0, token); break; case FAULT: r = request(nodes[token].id, token, 0); if (r != 0) { puts("Não consegui falhar o nodo!"); exit(1); } printf("Sou o nodo %d, falhei no tempo %5.1f\n", token, time()); break; case REPAIR: release(nodes[token].id, token); printf("Sou o nodo %d, recuperei no tempo %5.1f\n", token, time()); schedule(TEST, 30.0, token); break; } } }
//============================================================================= //== Main program == //============================================================================= void sim(int argc, char *argv[]) { double lambda; // Mean arrival rate (cust/sec) double mu; // Mean service rate (cust/sec) double offered_load; // Create the simulation create("sim"); // Output usage if (argc != 2) { printf("Usage: 'offered_load' - offered_load between 0 and 1 \n"); return; } offered_load = atof(argv[1]); assert((offered_load > 0.0) && (offered_load < 1.0)); // CSIM initializations Server1 = facility("Server1"); Server2 = facility("Server2"); Server3 = facility("Server3"); Server4 = facility("Server4"); Server5 = facility("Server5"); Resp_table = table("Response time table"); // Parameter initializations mu = 1.0; lambda = offered_load * (double)5; // Output begin-of-simulation banner printf("*** BEGIN SIMULATION *** \n"); // Initiate generate function and hold for SIM_TIME generate(lambda, mu); hold(SIM_TIME); // Output results printf("============================================================= \n"); printf("== *** CSIM 5 x M/M/1 queueing system simulation *** == \n"); printf("============================================================= \n"); printf("= Lambda = %6.3f cust/sec \n", lambda); printf("= Mu (for each server) = %6.3f cust/sec \n", mu); printf("============================================================= \n"); printf("= Total CPU time = %6.3f sec \n", cputime()); printf("= Total sim time = %6.3f sec \n", clock); printf("= Total completions = %ld cust \n", (completions(Server1) + completions(Server2) + completions(Server3) + completions(Server4) + completions(Server5))); printf("=------------------------------------------------------------ \n"); printf("= >>> Simulation results - \n"); printf("=------------------------------------------------------------ \n"); printf("= Utilization 1 = %6.3f %% \n", 100.0 * util(Server1)); printf("= Mean num in system 1 = %6.3f cust \n", qlen(Server1)); printf("= Mean response time 1 = %6.3f sec \n", resp(Server1)); printf("= Mean service time 1 = %6.3f sec \n", serv(Server1)); printf("= Mean throughput 1 = %6.3f cust/sec \n", tput(Server1)); printf("=------------------------------------------------------------ \n"); printf("= Utilization 2 = %6.3f %% \n", 100.0 * util(Server2)); printf("= Mean num in system 2 = %6.3f cust \n", qlen(Server2)); printf("= Mean response time 2 = %6.3f sec \n", resp(Server2)); printf("= Mean service time 2 = %6.3f sec \n", serv(Server2)); printf("= Mean throughput 2 = %6.3f cust/sec \n", tput(Server2)); printf("=------------------------------------------------------------ \n"); printf("= Utilization 3 = %6.3f %% \n", 100.0 * util(Server3)); printf("= Mean num in system 3 = %6.3f cust \n", qlen(Server3)); printf("= Mean response time 3 = %6.3f sec \n", resp(Server3)); printf("= Mean service time 3 = %6.3f sec \n", serv(Server3)); printf("= Mean throughput 3 = %6.3f cust/sec \n", tput(Server3)); printf("=------------------------------------------------------------ \n"); printf("= Utilization 4 = %6.3f %% \n", 100.0 * util(Server4)); printf("= Mean num in system 4 = %6.3f cust \n", qlen(Server4)); printf("= Mean response time 4 = %6.3f sec \n", resp(Server4)); printf("= Mean service time 4 = %6.3f sec \n", serv(Server4)); printf("= Mean throughput 4 = %6.3f cust/sec \n", tput(Server4)); printf("=------------------------------------------------------------ \n"); printf("= Utilization 5 = %6.3f %% \n", 100.0 * util(Server5)); printf("= Mean num in system 5 = %6.3f cust \n", qlen(Server5)); printf("= Mean response time 5 = %6.3f sec \n", resp(Server5)); printf("= Mean service time 5 = %6.3f sec \n", serv(Server5)); printf("= Mean throughput 5 = %6.3f cust/sec \n", tput(Server5)); printf("=------------------------------------------------------------ \n"); printf("& Table mean for response time = %6.3f sec \n", table_mean(Resp_table)); printf("============================================================= \n"); // Output end-of-simulation banner printf("*** END SIMULATION *** \n"); getchar(); }