Advertisement
madegoff

HRRN_richtig

Jun 16th, 2023 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include "HRRN.h"
  2.  
  3. static queue_object* HRRN_queue;
  4. //You can add more global variables and structs here
  5. int current_time; //die zeit die bisher vergangen ist
  6.  
  7. float get_ratio(queue_object* queue){ // ratio = (wartezeit + bedienzeit) / bedienzeit ; bedienzeit = time_left ; wartezeit = current_time - start_time
  8.  
  9.     process* current_process = queue->object;
  10.  
  11.     int start_time = current_process->start_time;
  12.     int left_time = current_process->time_left;
  13.  
  14.     int wait_time = current_time - start_time;
  15.  
  16.     float rr = (wait_time + left_time)/left_time;
  17.  
  18.     return rr;
  19.  
  20. }
  21.  
  22. void* queue_poll_hrr(queue_object* queue){
  23.  
  24.     void* obj;
  25.     float max_rr  = 0.0;
  26.  
  27.     queue_object* prev_el = queue;
  28.     queue_object* current_el = queue->next;
  29.  
  30.     queue_object* prev_old; //das element vor dem zu loeschenden element
  31.     queue_object* old; //das zu loeschende element
  32.  
  33.  
  34.     if(current_el == NULL){//wenn queue leer
  35.  
  36.         return NULL;
  37.     }
  38.  
  39.     if(current_el->next == NULL){ //queue besitzt nur ein element
  40.  
  41.         return queue_poll(HRRN_queue);
  42.     }
  43.     else{
  44.  
  45.         while (current_el!=NULL){
  46.  
  47.             float current_ratio = get_ratio(current_el);
  48.             if (current_ratio >= max_rr){
  49.  
  50.                 max_rr = current_ratio;
  51.                 prev_old = prev_el;
  52.                 old = current_el;
  53.  
  54.             }
  55.  
  56.             prev_el=prev_el->next;
  57.             current_el=current_el->next;
  58.  
  59.         }
  60.  
  61.         obj = old->object;
  62.         prev_old->next = old->next;
  63.         free(old);
  64.         return obj;
  65.  
  66.     }
  67.  
  68. }
  69.  
  70. process* HRRN_tick (process* running_process){
  71.  
  72.     current_time++;
  73.  
  74.     if (running_process == NULL || running_process->time_left == 0){
  75.  
  76.         running_process = queue_poll_hrr(HRRN_queue);
  77.     }
  78.  
  79.     if (running_process!=NULL){
  80.  
  81.         running_process->time_left--;//eine Zeiteinheit vergangen - andere waerendessen warten
  82.     }
  83.  
  84.     return running_process;
  85. }
  86.  
  87. int HRRN_startup(){
  88.  
  89.     current_time = -1;
  90.     HRRN_queue=new_queue();
  91.     if (HRRN_queue==NULL){
  92.         return 1;
  93.     }
  94.     return 0;
  95. }
  96.  
  97. process* HRRN_new_arrival(process* arriving_process, process* running_process){
  98.  
  99.     if(arriving_process!=NULL){
  100.  
  101.         queue_add(arriving_process, HRRN_queue);
  102.     }
  103.     return running_process;
  104. }
  105.  
  106. void HRRN_finish(){
  107.     free_queue(HRRN_queue);
  108. }
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement