Advertisement
madegoff

MLF

Jun 16th, 2023 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. #include "MLF.h"
  2. #include <stdio.h>
  3.  
  4. static queue_object** MLF_queues;
  5. //You can add more global variables here
  6. int g_quantum;
  7. int l_quantum;
  8. int level;
  9.  
  10. int fac (int a){
  11.  
  12.     if (a==0 || a==1){
  13.         return 1;
  14.     }
  15.     else return fac(a-1)*a;
  16.  
  17. }
  18.  
  19. //mein Ansatz war zuerst, dass man bei MLF aus der queue jeder ebene das element mit max prio rausnimmt (quelle:internet)
  20. //aber so passt der code den test nicht, also habe ich es gelassen und mit einem einfachen queue_poll gearbeitet
  21. //ich hinterlasse hier trotzdem meine poll_highest_prio funktion, weil es vielleicht unter anderen bediengungen effizientere methode waere
  22.  
  23. void* poll_highest_prio_new(queue_object* queue){
  24.  
  25.     queue_object *prev_el = queue;
  26.     queue_object *current_el = queue->next;
  27.  
  28.     queue_object *prev_old; //das element vor dem zu loeschenden element
  29.  
  30.     queue_object *old; //das zu loeschende element
  31.     void* obj;
  32.  
  33.     int prio = 0;
  34.  
  35.     if (current_el->next == NULL){ //ein element in der warteschlange
  36.         queue->next = NULL;
  37.         obj = current_el->object;
  38.         free(current_el);
  39.     }
  40.     else{
  41.         while(current_el!=NULL){
  42.  
  43.             process *cur_process = current_el->object;
  44.             if(cur_process->priority > prio){
  45.  
  46.                 prio = cur_process->priority;
  47.                 prev_old = prev_el;
  48.                 old = current_el;
  49.             }
  50.             prev_el=prev_el->next;
  51.             current_el=current_el->next;
  52.         }
  53.         obj = old->object;
  54.         prev_old->next = old->next;
  55.         free(old);
  56.     }
  57.     return obj;
  58. }
  59.  
  60. process* MLF_tick (process* running_process){
  61.  
  62.     if (running_process == NULL || running_process->time_left == 0){
  63.  
  64.         for (int i = 0; i < 4; i++){
  65.  
  66.             if (queue_peek(MLF_queues[i]) != NULL){ //wenn die grade betrachtete schlange nicht leer
  67.                 running_process = queue_poll(MLF_queues[i]); // den prozess mit dem hoechsten prio auswaeheln
  68.                 level = i+1; //merken uns, bei welcher ebene wir sind
  69.                 g_quantum = fac(level);
  70.                 l_quantum = g_quantum;
  71.                 break; //ausbrechen damit wir nicht zur naechsten ebene uebergehen
  72.             }
  73.         }
  74.     }
  75.  
  76.     if (running_process != NULL){
  77.  
  78.         if(level==4){ //sind bei FCFS ebene
  79.             running_process->time_left--;
  80.         }
  81.         else{
  82.  
  83.             if(l_quantum == 0 && running_process->time_left > 0){ //zeitscheibe um
  84.  
  85.                 queue_add(running_process, MLF_queues[level+1]); //verschieben den prozess in die niedriegere ebene
  86.                 running_process = NULL;
  87.                 running_process = MLF_tick(running_process);
  88.             }
  89.             else{
  90.                 running_process->time_left--;
  91.                 l_quantum--;
  92.             }
  93.         }
  94.     }
  95.     return running_process;
  96. }
  97.  
  98. /**
  99.  * Do everything you have to at startup in this function. It is called once.
  100.  * @result 0 if everything was fine. Any other number if there was an error.
  101.  */
  102. int MLF_startup(){
  103.  
  104.     MLF_queues = malloc(4* sizeof(queue_object*));
  105.  
  106.     for (int i=0; i<4; i++){
  107.  
  108.         MLF_queues[i] = new_queue();
  109.         if (MLF_queues[i] == NULL){
  110.             return 1;
  111.         }
  112.     }
  113.  
  114.     if (MLF_queues == NULL){
  115.  
  116.         return 1;
  117.     }
  118.  
  119.     return 0;
  120. }
  121.  
  122. process* MLF_new_arrival(process* arriving_process, process* running_process){
  123.  
  124.     if (arriving_process!=NULL){
  125.         queue_add(arriving_process, MLF_queues[0]);
  126.     }
  127.  
  128.     return running_process;
  129. }
  130.  
  131. void MLF_finish(){
  132.  
  133.     for (int i = 0; i<4; i++){
  134.         free_queue(MLF_queues[i]);
  135.     }
  136.     free(MLF_queues);
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement