Advertisement
madegoff

SJN

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