lab-06-Rushilwiz/schedule.c

52 lines
1.3 KiB
C

// PID: 730677144
// I pledge the COMP 211 honor code.
#include "schedule.h"
#include <stdio.h>
#include <stdlib.h>
void run_to_completion(task_struct* task) {
printf("Task %d ran for %d cycle(s).\n", task->pid, task->remaining_cycles);
task->remaining_cycles = 0;
printf("Task %d completed.\n", task->pid);
}
void run_with_quantum(task_struct* task, unsigned int quantum) {
if (task->remaining_cycles <= quantum) {
run_to_completion(task);
} else {
printf("Task %d ran for %d cycle(s).\n", task->pid, quantum);
task->remaining_cycles -= quantum;
}
}
void fcfs() {
while (get_task(0) != NULL) {
run_to_completion(remove_task(get_task(0)->pid));
}
}
void priority_queue(unsigned int quantum) {
min_heapify();
while (get_task(0) != NULL) {
task_struct* task = get_task(0);
run_with_quantum(task, quantum);
if (task->remaining_cycles > 0) {
remove_task(task->pid);
}
min_heapify();
}
}
void round_robin(unsigned int quantum) {
while (get_task(0) != NULL) {
task_struct* task = get_task(1 % size());
run_with_quantum(task, quantum);
if (task->remaining_cycles == 0) {
remove_task(task->pid);
}
}
}