Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MLF.h"
- #include <stdio.h>
- static queue_object** MLF_queues;
- //You can add more global variables here
- int g_quantum;
- int l_quantum;
- int level;
- int fac (int a){
- if (a==0 || a==1){
- return 1;
- }
- else return fac(a-1)*a;
- }
- //mein Ansatz war zuerst, dass man bei MLF aus der queue jeder ebene das element mit max prio rausnimmt (quelle:internet)
- //aber so passt der code den test nicht, also habe ich es gelassen und mit einem einfachen queue_poll gearbeitet
- //ich hinterlasse hier trotzdem meine poll_highest_prio funktion, weil es vielleicht unter anderen bediengungen effizientere methode waere
- void* poll_highest_prio_new(queue_object* queue){
- queue_object *prev_el = queue;
- queue_object *current_el = queue->next;
- queue_object *prev_old; //das element vor dem zu loeschenden element
- queue_object *old; //das zu loeschende element
- void* obj;
- int prio = 0;
- if (current_el->next == NULL){ //ein element in der warteschlange
- queue->next = NULL;
- obj = current_el->object;
- free(current_el);
- }
- else{
- while(current_el!=NULL){
- process *cur_process = current_el->object;
- if(cur_process->priority > prio){
- prio = cur_process->priority;
- prev_old = prev_el;
- old = current_el;
- }
- prev_el=prev_el->next;
- current_el=current_el->next;
- }
- obj = old->object;
- prev_old->next = old->next;
- free(old);
- }
- return obj;
- }
- process* MLF_tick (process* running_process){
- if (running_process == NULL || running_process->time_left == 0){
- for (int i = 0; i < 4; i++){
- if (queue_peek(MLF_queues[i]) != NULL){ //wenn die grade betrachtete schlange nicht leer
- running_process = queue_poll(MLF_queues[i]); // den prozess mit dem hoechsten prio auswaeheln
- level = i+1; //merken uns, bei welcher ebene wir sind
- g_quantum = fac(level);
- l_quantum = g_quantum;
- break; //ausbrechen damit wir nicht zur naechsten ebene uebergehen
- }
- }
- }
- if (running_process != NULL){
- if(level==4){ //sind bei FCFS ebene
- running_process->time_left--;
- }
- else{
- if(l_quantum == 0 && running_process->time_left > 0){ //zeitscheibe um
- queue_add(running_process, MLF_queues[level+1]); //verschieben den prozess in die niedriegere ebene
- running_process = NULL;
- running_process = MLF_tick(running_process);
- }
- else{
- running_process->time_left--;
- l_quantum--;
- }
- }
- }
- return running_process;
- }
- /**
- * Do everything you have to at startup in this function. It is called once.
- * @result 0 if everything was fine. Any other number if there was an error.
- */
- int MLF_startup(){
- MLF_queues = malloc(4* sizeof(queue_object*));
- for (int i=0; i<4; i++){
- MLF_queues[i] = new_queue();
- if (MLF_queues[i] == NULL){
- return 1;
- }
- }
- if (MLF_queues == NULL){
- return 1;
- }
- return 0;
- }
- process* MLF_new_arrival(process* arriving_process, process* running_process){
- if (arriving_process!=NULL){
- queue_add(arriving_process, MLF_queues[0]);
- }
- return running_process;
- }
- void MLF_finish(){
- for (int i = 0; i<4; i++){
- free_queue(MLF_queues[i]);
- }
- free(MLF_queues);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement