From 5e8c3c09afffa06b6f58bd81046d2425750cf64b Mon Sep 17 00:00:00 2001 From: Syufan <zyf571635687@gmail.com> Date: Tue, 18 Apr 2023 23:43:21 +1000 Subject: [PATCH] final --- allocate.c | 56 ++++-------------------------------------------------- 1 file changed, 4 insertions(+), 52 deletions(-) diff --git a/allocate.c b/allocate.c index c524d3e..e1e9746 100644 --- a/allocate.c +++ b/allocate.c @@ -51,8 +51,6 @@ int count_memory(queue_memory* q); int allocate_memory(queue_memory* q, process_n* process, int simulated_time); int find_position(memory_n* current_node,queue_memory* q); void free_memory(queue_memory* q, process_n* process); -void print_by_size(queue_memory* q, int start, int num); - int count_list(queue_n* q); //---------signature end---------// @@ -153,7 +151,6 @@ int main(int argc, char *argv[]) { pickup_input(batch_list,ready_list,simulated_time); process_n* current = ready_list->head; process_n* box = NULL; - double turnaround_time = 0; double end = 0; @@ -284,9 +281,7 @@ int main(int argc, char *argv[]) { // if process is compeleted if(current->time_required <= 0){ - strcpy(current->state, "FINISHED"); - end = simulated_time; single = end - current->arrival_time; turnaround_time += single; @@ -443,16 +438,14 @@ int main(int argc, char *argv[]) { //=====================Task 3 SJF END===================// //---------------functions---------------// - int allocate_memory(queue_memory* q, process_n* process, int simulated_time){ int min = -1; memory_n* min_start = NULL; memory_n* min_end = NULL; memory_n* temp = q->head; - //printf("**************memory**************\n"); while(temp->next != NULL && temp != NULL){ - //printf("bug121\n"); + // find the start of 0 while(temp->memory_state != 0 && temp->next != NULL){ temp = temp->next; @@ -467,38 +460,20 @@ int allocate_memory(queue_memory* q, process_n* process, int simulated_time){ } memory_n* temp_end = temp; - // printf("start = %d, end = %d \n",temp_start->number,temp_end->number); - // printf("count_memory end = %d \n",temp->number); - // printf("count_memory = %d \n",count_memory); - // check the memeory size if(count_memory < process->memory && min_end == NULL){ return 0; } if (count_memory == process->memory){ - //printf("true\n"); - // 直接赋值,用temp start开始 - //printf("go in to ready 1 start = %d, end = %d process memory%d\n\n",temp_start->number,temp_end->number, process->memory); memory_n* temp_allocate = temp_start; for (int i = 0; i < process->memory; i++){ - //循环正常 - temp_allocate -> memory_state = 1; strcpy(temp_allocate->process_name,process->process_name); temp_allocate = temp_allocate->next; - // temp = temp_start; - // temp->memory_state = 1; - // strcpy(temp_start->process_name,process->process_name); - // temp = temp->next; } - //int location = find_position(temp_start,q); - //printf("finish ready 1 start = %d memory state = %d process name = %s \n\n",temp_start->number,temp_start->memory_state,temp_start->process_name); - //printf("finish ready 1 start = %d memory state = %d process name = %s \n\n",temp_start->next->number,temp_start->next->memory_state,temp_start->next->process_name); - //printf("finish ready 1 start = %d memory state = %d process name = %s \n\n",temp_start->next->next->number,temp_start->next->next->memory_state,temp_start->next->next->process_name); printf("%d,READY,process_name=%s,assigned_at=%d\n",simulated_time, process->process_name, temp_start->number); return 1; }else if(count_memory > process->memory){ - //printf("compare\n"); if(min == -1){ min = count_memory; min_start = temp_start; @@ -513,45 +488,25 @@ int allocate_memory(queue_memory* q, process_n* process, int simulated_time){ } } } - //printf("min satrt = %d, min end = %d\n",min_start->number, min_end->number); - //printf("bug2\n"); int position = find_position(min_start,q); - //printf("===========min = %d start=%d %d\n", min, position,process->memory); int size = min_end->number - min_start->number + 1; if(min != -1){ - //printf("enter min, process size = %d memory size = %d\n",process->memory,size); if (process->memory){ for (size_t i = 0; i < process->memory; i++){ min_start->memory_state = 1; strcpy(min_start->process_name,process->process_name); - //printf("number %d state = %d name= %s\n",min_start->number,min_start->memory_state, min_start->process_name); min_start = min_start->next; - // temp = min_start; - // temp = temp->next; } printf("%d,READY,process_name=%s,assigned_at=%d\n",simulated_time, process->process_name, position); return 1; } return 0; } - // print_by_size(q,0,50); - // printf("\n\n\n\n"); return 0; } -void print_by_size(queue_memory* q, int start, int num){ - memory_n* temp = q->head; - // find the start point - while(temp->number != start){ - temp = temp->next; - } - for(int i = 0; i < num; i++){ - printf("number = %d memory state = %d,process name = %s\n",temp->number,temp->memory_state, temp->process_name); - temp = temp->next; - } - -} +// received node and then free it void free_memory(queue_memory* q, process_n* process){ memory_n* temp = q->head; // firstly, find the location where need free @@ -565,6 +520,7 @@ void free_memory(queue_memory* q, process_n* process){ temp = temp->next; } } +// received the list find the position int find_position(memory_n* current_node,queue_memory* q){ int position = 0; memory_n* temp = q->head; @@ -575,6 +531,7 @@ int find_position(memory_n* current_node,queue_memory* q){ return position; } +// count how many memory int count_memory(queue_memory* q){ memory_n* temp = q->head; int num = 0; @@ -677,8 +634,6 @@ void pickup_input_task3(queue_n* batch_list, queue_n* input_list, int simulated_ process_n* temp_next = temp->next; int sta = allocate_memory(memory_list,temp,simulated_time); - //cleaprintf("allocate memory: %d \n",temp->memory); - //printf("============%d\n", sta); if (sta==1){ //1.delete node from current list move_node(batch_list, temp); @@ -701,9 +656,6 @@ void pickup_input(queue_n* batch_list, queue_n* input_list, int simulated_time){ //find arrive time <=> simulated-time if(temp->arrival_time <= simulated_time){ process_n* temp_next = temp->next; - - //cleaprintf("allocate memory: %d \n",temp->memory); - // printf("============%d\n", sta); //1.delete node from current list move_node(batch_list, temp); //2.add node to aim list -- GitLab