int main() { char input[MAX_INPUT_LEN]; int arrPid[MAX_INPUT_LEN]; int arrArrival[MAX_INPUT_LEN]; int arrService[MAX_INPUT_LEN]; // Read data from stdin while(fgets(input, MAX_INPUT_LEN, stdin) != 0) { InputToArray(input, arrPid, arrArrival, arrService); } // Call function to print the 3 arrays FirstCome(arrPid, arrArrival, arrService, _pidIndex); ShortestJobFirst( arrPid, arrArrival, arrService, _pidIndex ); ShortestNext(arrPid, arrArrival, arrService, _pidIndex); RoundRobin( arrPid, arrArrival, arrService, _pidIndex ); return 0; }
//MAIN///////////////////////////////////////////////////////// int main(){ //init globals init(); //read file and store into queues read_and_store(); //based off of alg.name, determine which functino to call if(alg.name.compare("PR_noPREMP") == 0) PriorityNoPre(); else if(alg.name.compare("PR_withPREMP") == 0) PriorityWithPre(); else if(alg.name.compare("RR") == 0) RoundRobin(); else if(alg.name.compare("SJF") == 0) ShortestJobFirst(); else std::cout << "ERROR: algorithm name not recognized" << std::endl; in.close(); out.close(); return 0; }
int main(int argc, char *argv[]) { int tempo_total = 0, shmID_1, shmID_2, status; float tempo_medio = 0; long pid_filho1, pid_filho2; key_t key_1 = 567194, key_2 = 567192; input = fopen(argv[2], "r"); n_processos = tamanho_alloc(); //numero de processos a serem simulados fclose(input); //compartilhar tarefa e instante entre os processos instante = (int*) malloc (sizeof(int)); shmID_2 = shmget(key_2, sizeof(int), IPC_CREAT | 0666); instante = shmat(shmID_2, NULL, 0); *instante = 0; tarefas_input = criar_tarefas(tarefas_input, n_processos); //armazena entrada do arquivo tarefa = (Tarefa*) malloc(sizeof(Tarefa)); if ( (shmID_1 = shmget(key_1, sizeof(Tarefa), IPC_CREAT | 0666)) < 0 ){ perror("shmget error"); exit(1); } if( (tarefa = shmat(shmID_1, NULL, 0)) == (Tarefa*) -1){ perror("shmat error"); exit(1); } mutex = mmap(NULL,sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); mutex2 = mmap(NULL,sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); if(!mutex || !mutex2) perror("mmap error"); //iniciados em 0 e 1 sem_init(mutex, 1, 0); sem_init(mutex2, 1, 1); pid_filho2 = fork(); if(pid_filho2 != 0){ pid_filho1 = fork(); wait(&status); } //processo CPU if(pid_filho1 == 0){ fprintf(stderr, "[Processo CPU]...\n"); int i; tarefas_output = (Tarefa*) calloc(n_processos, sizeof(Tarefa)); for(i=0; i < n_processos; i++){ tarefas_output[i].dados[0] = i; tarefas_output[i].dados[1] = -1; } while(1){ fprintf(stderr, "[Processo CPU] Tempo: %d\n", tempo_total); sem_wait(mutex); if(tarefa==NULL){ fprintf(stderr, "Tarefa null no instante %d\n", *instante); break; } fprintf(stderr, "[Processo CPU] Executando Tarefa {%d;%d;%d;%d,%d}...\n", tarefa->dados[0], tarefa->dados[1], tarefa->dados[2], tarefa->dados[3], tarefa->dados[4]); sleep(QUANTUM); tarefa->dados[2] -= QUANTUM; //tempo restante de execucao tempo_total += QUANTUM; if(tarefas_output[ tarefa->dados[0] ].dados[1] == -1) tarefas_output[ tarefa->dados[0] ].dados[1] = (*instante); else tarefas_output[ tarefa->dados[0] ].dados[2] += QUANTUM; sem_post(mutex2); } //salvar saida output = fopen(argv[3], "w"); for(i=0; i < n_processos; i++) fprintf(stderr, "%d;%d;%d;\n", tarefas_output[i].dados[0], tarefas_output[i].dados[1], tarefas_output[i].dados[2]); //output fprintf(stderr, "Tempo Total: %d\nTempo medio: %f", tempo_total, tempo_medio); fclose(output); exit(0); } //processo escalonador if(pid_filho2 == 0){ fprintf(stderr, "\n[Processo Escalonador]...\n"); input = fopen(argv[2], "r"); lerEntradasArquivo(); //fprinTest(tarefas_input); if(!strcmp(argv[1], "FCFS")) FirstComeFirstServed(); else if(!strcmp(argv[1], "RR")) RoundRobin(); else if(!strcmp(argv[1], "SJF")) ShortestJobFirst(); fclose(input); exit(0); } sem_destroy(mutex); munmap(mutex, sizeof(sem_t)); sem_destroy(mutex2); munmap(mutex2, sizeof(sem_t)); return EXIT_SUCCESS; }