/** * Add a parameter @param name and the corresponding default @param defaultValue. * We check on disk to see if there is the parameter to bound to, otherwise * we store the default provided value. */ template<typename T> void add(std::string name, T defaultValue) { cv::FileStorage file(parametersFilename, cv::FileStorage::READ); cv::FileNode node = file[name]; T toStoreValue; if(node.isNone()) //on disk there is no value to retrieve, //then we use the provided default toStoreValue = defaultValue; else node >> toStoreValue; //inject the value to be associated with @param name into //an object of type hold_any any anyH(toStoreValue); std::pair<std::string, any> insertP(name, anyH); //insert and check what happened bool r = parametersMap.insert(insertP).second; if(!r) throw ParameterNameError("Parameter '" + name + "' was already added"); }
// main scheduling program int main() { Process P[max_numP]; int schedule[max_totalT]; char policy_name[5]; scanf("%s", policy_name); int numP; scanf("%d", &numP); getchar(); int totalT = 0; for(int i = 0; i < numP; i++) { char *process_name = malloc(max_name_length * sizeof(char)); int ready_time; int execution_time; scanf("%s %d %d", process_name, &ready_time, &execution_time); getchar(); totalT += execution_time; P[i].name = process_name; P[i].readyT = ready_time; P[i].execT = execution_time; P[i].ID = i; } // sort P according to readyT qsort(P, numP, sizeof(Process), compare_Process); int policy = 0; char policy_list[4][5] = {"FIFO", "RR", "SJF", "PSJF"}; for(int i = 0; i < 4; i++) if(strcmp(policy_name, policy_list[i]) == 0) policy = i; Process *waiting_list[max_numP]; for(int i = 0; i < max_numP; i++) waiting_list[i] = NULL; Process *exec_order[max_totalT]; int index_now = 0; int t = 0; while(t < totalT) { while(P[index_now].readyT <= t) { insertP(waiting_list, policy, (P + index_now)); index_now++; } if(waiting_list[0] != NULL) { Process *exec_process = waiting_list[0]; int exec_length = execP(waiting_list, policy); for(int i = 1; i <= exec_length; i++) { exec_order[t] = exec_process; t++; } } else { exec_order[t] = NULL; // no waiting process now t++; } } // test result for(int i = 0; i < totalT; i++) { if(exec_order[i] != NULL) printf("%d time unit : execute %s\n", i + 1, exec_order[i]->name); else printf("%d time unit : No process\n", i + 1); } return 0; }
// main scheduling program int main() { Process P[max_numP]; char policy_name[5]; scanf("%s", policy_name); int numP; scanf("%d", &numP); for(int i = 0; i < numP; i++) { char *process_name = malloc(max_name_length * sizeof(char)); int ready_time; int execution_time; scanf("%s %d %d", process_name, &ready_time, &execution_time); P[i].name = process_name; P[i].readyT = ready_time; P[i].execT = execution_time; P[i].ID = i; } // sort P according to readyT qsort(P, numP, sizeof(Process), compare_Process); int policy = 0; char policy_list[4][5] = {"FIFO", "RR", "SJF", "PSJF"}; for(int i = 0; i < 4; i++) if(strcmp(policy_name, policy_list[i]) == 0) policy = i; Process *waiting_list[max_numP]; for(int i = 0; i < max_numP; i++) waiting_list[i] = NULL; //set CPU cpu_set_t cpu_mask; CPU_ZERO(&cpu_mask); CPU_SET(0, &cpu_mask); if(sched_setaffinity(0, sizeof(cpu_set_t), &cpu_mask) != 0){ perror("sched_setaffinity error"); exit(EXIT_FAILURE); } //raise the priority const int priorityH = 80;//sched_get_priority_max(SCHED_RR); const int priorityL = 10;//sched_get_priority_min(SCHED_RR); //printf("H:%d L:%d\n",priorityH,priorityL); struct sched_param param; param.sched_priority = 50; //Just want to make sure it won't get preempted by other processes on its CPU pid_t pidP = getpid(); if(sched_setscheduler(pidP, SCHED_FIFO, ¶m) != 0) { perror("sched_setscheduler error"); exit(EXIT_FAILURE); } // P already sorted in ascending order of readyT int time_count = 0; int fork_count = 0; Process *exec_process = NULL; Process *exec_process_last = NULL; int numP_finish_last = 0; int exec_length = 0; pid_t pids[max_numP]; while(!(numP_finish == numP && exec_length == 0)) { //fork the processes that are ready at time_count long long start_time = syscall(328); long long start_time_s = start_time / n; long long start_time_ns = start_time % n; while(P[fork_count].readyT <= time_count && fork_count < numP) { printf("%s : fork at time %d\n", P[fork_count].name, time_count); pid_t pid = fork(); if(pid < 0) printf("error in fork!"); else if(pid == 0) { // child char exec_time[10]; sprintf(exec_time, "%d", P[fork_count].execT); char start_time_s_string[20]; char start_time_ns_string[20]; sprintf(start_time_s_string, "%lld", start_time_s); sprintf(start_time_ns_string, "%lld", start_time_ns); if(execlp("./process", "process", P[fork_count].name, exec_time, start_time_s_string, start_time_ns_string, (char *)NULL) < 0){ perror("execlp error"); exit(EXIT_FAILURE); } } // parent //change the newly forked child's CPU cpu_set_t cpu_mask; CPU_ZERO(&cpu_mask); CPU_SET(1, &cpu_mask); if(sched_setaffinity(pid, sizeof(cpu_set_t), &cpu_mask) != 0){ perror("sched_setaffinity error"); exit(EXIT_FAILURE); } pids[P[fork_count].ID] = pid; // use ID (also 0 ~ numP - 1) as index to store pids insertP(waiting_list, policy, (P + fork_count)); fork_count++; } // decide next process // no process is waiting if(waiting_list[0] == NULL && exec_length == 0) { time_count++; volatile unsigned long i; for(i=0;i<1000000UL;i++); exec_process_last = NULL; } else { // find next process in the list if(exec_length == 0) { exec_process = waiting_list[0]; exec_length = execP(waiting_list, policy); //change priority if a different process is going to run if(exec_process_last == NULL || exec_process_last->execT == 0) { printf("Set %s to high at time %d\n", exec_process->name, time_count); pid_t pid = pids[exec_process->ID]; printf("PID: %d\n",pid); param.sched_priority = priorityH; if(sched_setscheduler(pid, SCHED_RR, ¶m) != 0) { perror("sched_setscheduler error"); exit(EXIT_FAILURE); } if(sched_getscheduler(pid) == SCHED_RR) puts("RR"); } // recover priority of last process else { printf("Set %s to high, recover %s to low at time %d\n", exec_process->name, exec_process_last->name, time_count); pid_t pid_last = pids[exec_process_last->ID]; pid_t pid = pids[exec_process->ID]; printf("PID:%d\n",pid); param.sched_priority = priorityL; if(sched_setscheduler(pid_last, SCHED_RR, ¶m) != 0) { perror("sched_setscheduler error"); exit(EXIT_FAILURE); } param.sched_priority = priorityH; if(sched_setscheduler(pid, SCHED_FIFO, ¶m) != 0) { perror("sched_setscheduler error"); exit(EXIT_FAILURE); } } } exec_length--; time_count++; volatile unsigned long i; for(i=0;i<1000000UL;i++); // exec time section for this process is over if(exec_length == 0) { // check if the process has terminated, if so, wait for it if(numP_finish == numP_finish_last + 1) { printf("%s : end at time %d\n", exec_process->name, time_count); int status; if(waitpid(pids[exec_process->ID], &status, 0) == -1) { perror("waitpid error"); exit(EXIT_FAILURE); } } exec_process_last = exec_process; numP_finish_last = numP_finish; } } } for(int i = 0; i < numP; i++){ printf("%s ", P[i].name); printf("%d\n", pids[P[i].ID]); } }
int leitura (char * nome_fich) { FILE * ficheiro; char *string = (char *) malloc(100); char *str = (char *) malloc(100); char *token = NULL; char limit[2] = " "; char modo; int linhas = 0; int linhas_val = 0; int i; ficheiro = fopen(nome_fich, "r"); if (strcmp(nome_fich, "FichClientes.txt") == 0){ while ((string = fgets (str, 100, ficheiro)) != NULL) { linhas++; token = strtok(string, limit); token[5] = '\0'; clientes[token[0]-'A'] = insertC(clientes[token[0]-'A'], token); } printf("Ficheiro lido: %s\nNúmero de linhas lidas: %d\n", nome_fich, linhas); } else if (strcmp(nome_fich, "FichProdutos.txt") == 0){ while ((string = fgets (str, 100, ficheiro)) != NULL) { linhas++; token = strtok(string, limit); token[6] = '\0'; produtos[token[0]-'A'] = insertP(produtos[token[0]-'A'], token); for(i = 0; i < 12; i++){ contas[i] = inserirContabilidade(contas[i],token); } } printf("Ficheiro lido: %s\nNúmero de linhas lidas: %d\n", nome_fich, linhas); } else if (strcmp(nome_fich, "Compras.txt") == 0){ int i = 0; int nr; int mes; double preco; char * compra[6]; while ((string = fgets (str, 100, ficheiro)) != NULL) { linhas++; token = strtok(string, limit); compra[0] = token; for(i=1; token != NULL && i < 6; i++) { token = strtok(NULL, " "); compra[i] = token; } /* compra[4] -> cliente * compra[0] -> produto */ preco = strtod(compra[1], &compra[1]); nr = atoi(compra[2]); mes = atoi(compra[5]); modo = compra[3][0]; if (linha_valida(compra, preco, nr, mes, clientes[compra[4][0]-'A'], produtos[compra[0][0]-'A'])){ linhas_val++; compras[mes-1] = insereCompra(compras[mes-1], compra[4], compra[0], modo, preco, nr); contas[mes-1] = actualizaContabilidade(contas[mes-1],compra[0],modo,preco,nr); } } printf("Ficheiro lido: %s\nNúmero de linhas lidas: %d\nNúmero de linhas válidas: %d\n", nome_fich, linhas, linhas_val); } else return 0; free(str); free(string); fclose(ficheiro); return 1; }