Advertisement
madegoff

SJN_richtig

Jun 16th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include "SJN.h"
  2.  
  3. static queue_object* SJN_queue;
  4. //You can add more global variables here
  5.  
  6. void* poll_lowest_time(queue_object* queue){
  7.  
  8.     queue_object *prev_el = queue;
  9.     queue_object *current_el = queue->next;
  10.  
  11.     queue_object *prev_old; //das element vor dem zu loeschenden element
  12.  
  13.     queue_object *old; //das zu loeschende element
  14.     void* obj;
  15.  
  16.     int time = 100;
  17.  
  18.     if (current_el->next == NULL){ //ein element in der warteschlange
  19.         queue->next = NULL;
  20.         obj = current_el->object;
  21.         free(current_el);
  22.     }
  23.     else{
  24.         while(current_el!=NULL){
  25.  
  26.             process* current_process = current_el->object;
  27.             int current_bedienzeit = current_process->time_left;
  28.  
  29.             if(current_bedienzeit < time){
  30.  
  31.                 time = current_bedienzeit;
  32.                 prev_old = prev_el;
  33.                 old = current_el;
  34.             }
  35.             prev_el=prev_el->next;
  36.             current_el=current_el->next;
  37.         }
  38.         obj = old->object;
  39.         prev_old->next = old->next;
  40.         free(old);
  41.     }
  42.     return obj;
  43. }
  44.  
  45. process* SJN_tick (process* running_process){
  46.  
  47.    if (running_process == NULL){ //den ersten prozess aus der schlange nehmen
  48.  
  49.         running_process = queue_poll(SJN_queue);
  50.     }
  51.  
  52.     if( running_process->time_left==0){
  53.  
  54.         running_process = poll_lowest_time(SJN_queue);
  55.     }
  56.  
  57.     if (running_process != NULL){
  58.  
  59.         running_process->time_left--;
  60.     }
  61.  
  62.     return running_process;
  63. }
  64.  
  65. int SJN_startup(){
  66.  
  67.     SJN_queue=new_queue();
  68.     if (SJN_queue==NULL){
  69.         return 1;
  70.     }
  71.     return 0;
  72. }
  73.  
  74. process* SJN_new_arrival(process* arriving_process, process* running_process){
  75.     if(arriving_process!=NULL){
  76.  
  77.                 queue_add(arriving_process, SJN_queue);
  78.  
  79.     }
  80.     return running_process;
  81. }
  82.  
  83. void SJN_finish(){
  84.  
  85.     free(SJN_queue);
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement