int release_cs(const void * self) { if (done_count == 0) { inc_lamport_time(); return 0; } Message msg; memset(&msg, 0, sizeof(msg)); next_proc(); inc_lamport_time(); init_message(&msg, NULL, CS_RELEASE); for (local_id id_from = 1; id_from <= num_proc; id_from++) { send(NULL, id_from, &msg); } return 0; }
/** * Insert the new processes into a scheduler index list */ int refresh_index_list(all_types *proc_list, int index_key, int (*scheduler) (int np, int max_time)) { all_types *aux; all_types *index_aux; int priority_coef; // get the last process index already on shared memory int proc_index = 0; int priority_list_index; // initialize process table memory manager to find out the proc_index offset init(PROC_TABLE_SHM_KEY); if ((aux = get_last_proc())) proc_index = index_proc(aux)+1; // initialize the index list memory manager init(index_key); // run through the new process list while (proc_list) { // calculate the coeficient of priority priority_coef = (*scheduler) (proc_list->flex_types.p.n_proc, proc_list->flex_types.p.max_time); // insert the index in the apropriate order aux = get_first_proc(); if (!aux) { // Empty list aux = malloc_proc_shr_mem(); // set the index table effective values aux->flex_types.pl.priority_coef = priority_coef; aux->flex_types.pl.proc_index = proc_index; // set the control pointers for the first (and only) element set_first_proc(aux); set_last_proc(aux); // set the process_table->priority_list index pointer priority_list_index = index_proc(aux); } else if (aux->flex_types.pl.priority_coef < priority_coef) { // new first element index_aux = malloc_proc_shr_mem(); // set the index table effective values index_aux->flex_types.pl.priority_coef = priority_coef; index_aux->flex_types.pl.proc_index = proc_index; // New one points the the old one index_aux->next_index = index_proc(aux); index_aux->next = aux; // The old one points to the new one aux->prev_index = index_proc(aux); aux->prev = index_aux; set_first_proc(index_aux); // set the process_table->priority_list index pointer priority_list_index = index_proc(index_aux); } else { // not any kind of first element // if there is at least one item we find the right spot while(next_proc(aux) && next_proc(aux)->flex_types.pl.priority_coef >= priority_coef) { aux = next_proc(aux); } if (!next_proc(aux)) { // new last // insert a new element index_aux = malloc_proc_shr_mem(); // set the index table effective values index_aux->flex_types.pl.priority_coef = priority_coef; index_aux->flex_types.pl.proc_index = proc_index; aux->next = index_aux; aux->next_index = index_proc(index_aux); index_aux->prev = aux; index_aux->prev_index = index_proc(aux); set_last_proc(index_aux); } else { // insert a new element index_aux = malloc_proc_shr_mem(); // set the index table effective values index_aux->flex_types.pl.priority_coef = priority_coef; index_aux->flex_types.pl.proc_index = proc_index; index_aux->next = next_proc(aux); index_aux->next_index = aux->next_index; index_aux->prev = aux; index_aux->prev_index = index_proc(aux); aux->next = index_aux; aux->next_index = index_proc(index_aux); index_aux->next->prev = index_aux; index_aux->next->prev_index = index_proc(index_aux); } // set the process_table->priority_list index pointer priority_list_index = index_proc(index_aux); } if (index_key == COEF_LIST_1_SHM_KEY) // THIS IS SHIT!!!!!!!!!!!!!!!!! proc_list->flex_types.p.sjf_sch_index = priority_list_index; // NEED TO IMPROVE FLEXIBILITY else // maybe we can receive the reference to where to write as a parameter proc_list->flex_types.p.ljf_sch_index = priority_list_index; proc_list = proc_list->next; proc_index++; } return 0; }
int request_cs(const void * self) { if (done_count == 0) { inc_lamport_time(); return 0; } Message * msg = (Message *) calloc(1, sizeof(Message)); memset(msg, 0, sizeof(Message)); inc_lamport_time(); init_message(msg, NULL, CS_REQUEST); queue_push(my_local_id, get_lamport_time()); for (local_id id_from = 1; id_from <= num_proc; id_from++) { if (id_from != my_local_id) { send(NULL, id_from, msg); } } local_id from; int count_reply = num_proc - 2; while (1) { memset(msg, 0, sizeof(Message)); receive_any(&from, msg); // printf("%d: type message %d\n", my_local_id, msg->s_header.s_type); switch (msg->s_header.s_type) { case CS_REPLY: { count_reply--; // printf("%d: reply head - %d\n", my_local_id, get_head()); if (count_reply <= 0 && get_head() == my_local_id) { return 0; } break; } case CS_REQUEST: { // printf("%d: request\n", my_local_id); queue_push(from, msg->s_header.s_local_time); memset(msg, 0, sizeof(Message)); inc_lamport_time(); init_message(msg, NULL, CS_REPLY); send(NULL, from, msg); break; } case CS_RELEASE: { // printf("%d: release\n", my_local_id); next_proc(); if (get_head() == my_local_id) { return 0; } break; } case DONE: { done_count--; if (done_count <= 0) { return 0; } break; } default: fprintf(stderr, "unknown type message: %d\n", msg->s_header.s_type); } } free(msg); return 0; }