Advertisement
madegoff

PRIO_NP

Jun 15th, 2023 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. #include "PRIONP.h"
  2. #include <stdio.h>
  3.  
  4. static queue_object* PRIONP_queue;
  5. //You can add more global variables here
  6. void* poll_highest_prio(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 prio = 0;
  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 *cur_process = current_el->object;
  27.             if(cur_process->priority > prio){
  28.  
  29.                 prio = cur_process->priority;
  30.                 prev_old = prev_el;
  31.                 old = current_el;
  32.             }
  33.             prev_el=prev_el->next;
  34.             current_el=current_el->next;
  35.         }
  36.         obj = old->object;
  37.         prev_old->next = old->next;
  38.         free(old);
  39.     }
  40.     return obj;
  41. }
  42.  
  43. process* PRIONP_tick (process* running_process){
  44.  
  45.     if (running_process == NULL){ //den ersten prozess aus der schlange nehmen
  46.  
  47.         running_process = queue_poll(PRIONP_queue);
  48.     }
  49.  
  50.     if( running_process->time_left==0){
  51.  
  52.         running_process = poll_highest_prio(PRIONP_queue);
  53.     }
  54.  
  55.     if (running_process != NULL){
  56.  
  57.         running_process->time_left--;
  58.     }
  59.  
  60.     return running_process;
  61. }
  62.  
  63. int PRIONP_startup(){
  64.  
  65.     PRIONP_queue=new_queue();
  66.     if (PRIONP_queue==NULL){
  67.         return 1;
  68.     }
  69.     return 0;
  70. }
  71.  
  72. process* PRIONP_new_arrival(process* arriving_process, process* running_process){
  73.  
  74.     if(arriving_process!=NULL){
  75.  
  76.                 queue_add(arriving_process, PRIONP_queue);
  77.  
  78.     }
  79.     return running_process;
  80. }
  81.  
  82. void PRIONP_finish(){
  83.  
  84.     free(PRIONP_queue);
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement