From 67135a2c59b97177790c7da1354403158e2afd18 Mon Sep 17 00:00:00 2001
From: Syufan <zyf571635687@gmail.com>
Date: Tue, 18 Apr 2023 23:38:38 +1000
Subject: [PATCH] finished

---
 allocate.c | 105 ++++++++++++++++++++++++-----------------------------
 1 file changed, 47 insertions(+), 58 deletions(-)

diff --git a/allocate.c b/allocate.c
index c9bd0db..c524d3e 100644
--- a/allocate.c
+++ b/allocate.c
@@ -257,17 +257,21 @@ int main(int argc, char *argv[]) {
     else if(strcmp(scheduler_algorithm,"RR") == 0 && strcmp(memory_strategy,"best-fit") == 0){
         
         // pick up the process who's arrive time is suitable
+        double num = count_list(batch_list);
         pickup_input_task3(batch_list,ready_list,simulated_time,memory_list);
         process_n* current = ready_list->head;
         process_n* box = NULL;
 
+        double turnaround_time = 0;
+        double end = 0;
+        double single = 0;
+        double time_overhead = 0;
+        double time_single = 0;
+        double max_time_overhead = 0;
+
         // make sure no process in ready list and batch list
         while(ready_list->head != NULL || batch_list->head != NULL){
-            // printf("\n===================start=============\n");
-            // printf("%d\n", simulated_time);
-            // print_q(ready_list);
-            //int turn_on = 0;
-            // pickup_input_task3(batch_list, ready_list, simulated_time, memory_list);
+            start:
             while(ready_list->head == NULL && batch_list->head != NULL){
                 
                 simulated_time+=quantum;
@@ -277,28 +281,21 @@ int main(int argc, char *argv[]) {
             if(current == NULL && ready_list->head != NULL){
                 current = ready_list->head;
             }
-            // if(ready_list->head==NULL){
-            //     pickup_input(batch_list,ready_list,simulated_time,memory_list);
-            // }
-            //printf("%d\n", simulated_time);
-            //printf("xxxxxx current name = %s , arrive time = %d \n",current->process_name,current->arrival_time);
-            // if(ready_list->head == NULL){
-            //     printf("马上运行\n");
-            //     current = NULL;
-            //     goto finish_task3;
-            // }
-            // printf("马上运行\n");
-            // if(current == NULL){
-            //    current = ready_list->head;
-            //    //printf("890809current = null\n");
-            //    //printf("current name = %s , arrive time = %d \n",current->process_name,current->arrival_time);
-            // }
-            // int moved = 0;
 
             // if process is compeleted
             if(current->time_required <= 0){
 
                 strcpy(current->state, "FINISHED");
+
+                end = simulated_time;
+                single = end - current->arrival_time;
+                turnaround_time += single;
+                time_single = single / current->time_required_orgin;
+                if(max_time_overhead < time_single){
+                    max_time_overhead = time_single;
+                }
+                time_overhead += time_single;
+
                 free_memory(memory_list,current);
                 int proc_remaining = count_list(ready_list) - 1;
                 printf("%d,%s,process_name=%s,proc_remaining=%d\n",simulated_time,current->state,current->process_name,proc_remaining);
@@ -317,10 +314,10 @@ int main(int argc, char *argv[]) {
 
                 dele_node(ready_list,current);
                 current = box;
-                //moved = 1;
+                
                 if(current == NULL){
                     pickup_input_task3(batch_list,ready_list,simulated_time,memory_list);
-                    if(ready_list->head != NULL){
+                    if(ready_list->head != NULL && batch_list->head != NULL){
                         current = ready_list->head;
                     }
                     else{
@@ -332,29 +329,17 @@ int main(int argc, char *argv[]) {
                 // many nodes
                 if(strcmp(current->state,"null") == 0){
                     // if there are many same nodes, just modify first one
-                    // start
-                    //printf("%d,%s,process_name=%s,assigned_at=%d",simulated_time,current->state,);
-                    //strcpy(current->state, "READY");
-                    //printf("======allocate memory\n");
-                    //print_by_size(memory_list,0,80);
-                    //allocate_memory(memory_list,current,simulated_time);
                     strcpy(current->state, "RUNNING");
                     printf("%d,%s,process_name=%s,remaining_time=%d \n",simulated_time,current->state,current->process_name,current->time_required);
                     current->time_required -= quantum;
-                    //goto finish;
                 }
                 strcpy(current->state, "READY");
-                //if(moved != 1){
-                    if(current->next != NULL){
-                        current = current->next;
-                    }else if(current == ready_list->foot){
-                        current = ready_list->head;
-                    }
-                //}
+                if(current->next != NULL){
+                    current = current->next;
+                }else if(current == ready_list->foot){
+                    current = ready_list->head;
+                }
                 if(strcmp(current->state,"null") == 0){
-                    //printf("======allocate memory\n");
-                    //strcpy(current->state, "READY");
-                    //allocate_memory(memory_list,current,simulated_time);
                     strcpy(current->state, "RUNNING");
                     printf("%d,%s,process_name=%s,remaining_time=%d \n",simulated_time,current->state,current->process_name,current->time_required);
                     current->time_required -= quantum;
@@ -368,37 +353,40 @@ int main(int argc, char *argv[]) {
             if(current == ready_list->head && current == ready_list->foot){
                 // there are only one process
                 if(strcmp(current->state,"RUNNING") != 0){
-                    //printf("马上运行\n");
-                    //printf("process_name=,assigned_at=\n");
-                    //strcpy(current->state, "READY");
-                    //print_by_size(memory_list,0,40);
-                    //printf("allocation one process memory\n");
-                    //allocate_memory(memory_list,current,simulated_time);
                     strcpy(current->state, "RUNNING");
                     printf("%d,%s,process_name=%s,remaining_time=%d \n",simulated_time,current->state,current->process_name,current->time_required);
                 }
                 current->time_required -= quantum;
             }
+            int before_num = 0;
+            before_num = count_list(ready_list);
+            int after_num = 0;
+
             finish_task3:
-            //if(turn_on == 0){
-                pickup_input_task3(batch_list, ready_list, simulated_time, memory_list);
-                simulated_time += quantum;
-            //}
             
-            // printf("提前减少的下半时区\n");
+            pickup_input_task3(batch_list, ready_list, simulated_time, memory_list);
+            
+            after_num = count_list(ready_list);
+            if(after_num!=before_num && ready_list->head!=NULL && batch_list->head != NULL){
+                goto start;
+            }
+            
+            simulated_time += quantum;
+            
             if(current == NULL && ready_list->head != NULL){
                 current = ready_list->head;
             }
-            // printf("\n\nready List:\n");
-            // print_q(ready_list);
         }
         
+        max_time_overhead = round(max_time_overhead*100)/100;
+        turnaround_time = ceil(turnaround_time/(num));
+        time_overhead = round((time_overhead / num) *100)/100;
+        printf("Turnaround time %0.f\nTime overhead %.2f %.2f \nMakespan %d\n",turnaround_time,max_time_overhead,time_overhead,simulated_time-quantum);
+
+        
     }
-    // 打印解析的参数
-    // printf("memory_strategy = %s ", memory_strategy);
-    // printf("quantum = %d\n", quantum);
 
-    //=====================Task 3 sjf===================//
+    //=====================Task 3 SJF===================//
     if(strcmp(scheduler_algorithm,"SJF") == 0 && strcmp(memory_strategy,"best-fit") == 0){
         // put all the process which arrive time <= simmulated time
         pickup_input_task3(batch_list, input_list, simulated_time,memory_list);
@@ -452,6 +440,7 @@ int main(int argc, char *argv[]) {
     }
     return 0;
 }
+//=====================Task 3 SJF END===================//
 
 //---------------functions---------------//
 
-- 
GitLab