Skip to content

Instantly share code, notes, and snippets.

@victoroliveirab
Created September 4, 2019 19:35
Show Gist options
  • Save victoroliveirab/d22b7db96442e05999860f178fd028bb to your computer and use it in GitHub Desktop.
Save victoroliveirab/d22b7db96442e05999860f178fd028bb to your computer and use it in GitHub Desktop.
Round-Robin Implementation in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "task.h"
#include "list.h"
#include "cpu.h"
/*
Round-Robin Scheduling Algorithm Implementation
Programming Project of Chapter 5 from the book "Operating System Concepts - 10th Edition"
*/
struct node * list = NULL;
void add(char *name, int priority, int burst) {
Task * task = (Task *)malloc(sizeof (Task));
task->name = name;
task->priority = priority;
task->burst = burst;
task->tid = 0;
insert(&list, task);
}
void run_schedule(struct node * current) {
if (current) {
int slice = current->task->burst;
if (slice <= QUANTUM) {
run(current->task, slice);
delete(&list,current->task);
}
else {
run(current->task, QUANTUM);
current->task->burst -= QUANTUM;
struct node * tail = current;
struct node * temp = current->next;
while (temp) {
tail = temp;
temp = temp->next;
}
list = current->next;
current->next = NULL;
tail->next = current;
}
run_schedule(list);
}
}
void schedule() {
run_schedule(list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment