CS3413/Assignment4/round_robin.c

102 lines
3.7 KiB
C
Raw Normal View History

2023-10-24 13:50:05 -03:00
#include <stdio.h>
#include "lib/queue.h"
#define MAX_USERNAME_LENGTH 100
int QUANTUM;
2023-10-26 15:58:58 -03:00
Queue *input_queue() {
Queue *queue = createQueue();
2023-10-26 17:15:05 -03:00
char username[MAX_USERNAME_LENGTH]; // username buffer
2023-10-26 15:58:58 -03:00
char job;
int arrival_time, duration;
2023-10-26 17:15:05 -03:00
scanf("%d", &QUANTUM);
2023-10-26 15:58:58 -03:00
while (getchar() != '\n'); // clear the newline from the buffer
2023-10-26 17:15:05 -03:00
while (getchar() != '\n'); // ignore the rest of the line, this is the table line
// Loop through the process table and enqueue each process
2023-10-26 15:58:58 -03:00
while (scanf("%99s %c %d %d", username, &job, &arrival_time, &duration) != EOF) {
Process *process = createProcess(username, job, arrival_time, duration);
enqueue(queue, process);
}
return queue;
2023-10-24 13:50:05 -03:00
}
2023-10-26 17:15:05 -03:00
void simulation(Queue *in_queue) {
2023-10-26 16:11:12 -03:00
// Summary creation
2023-10-26 17:15:05 -03:00
Process *process = in_queue->end;
Queue *summary_queue = createQueue();
for (int i = 0; i < in_queue->size; ++i) {
if (contains(summary_queue, process->username) == false) {
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration);
enqueue(summary_queue, copy);
2023-10-26 15:58:58 -03:00
}
process = process->prev_elem;
}
2023-10-26 16:11:12 -03:00
// Loop variables
2023-10-26 15:58:58 -03:00
int quantum = QUANTUM;
2023-10-26 16:11:12 -03:00
int addedJobs = 0;
2023-10-26 17:15:05 -03:00
int time = 0;
// Create a queue for the simulation
Queue *sim_queue = createQueue();
2023-10-24 13:50:05 -03:00
2023-10-26 17:15:05 -03:00
printf("Time\tJob\n");
2023-10-26 15:58:58 -03:00
while (true) {
2023-10-26 17:15:05 -03:00
time++;
2023-10-26 16:11:12 -03:00
// Begin going through all jobs and enqueueing them if they have arrived
2023-10-26 17:15:05 -03:00
process = in_queue->end;
for (int i = 0; i < in_queue->size; i++) {
2023-10-26 15:58:58 -03:00
if (process->arrival_time == time) {
2023-10-26 16:11:12 -03:00
// Create copy to keep the queues separate
2023-10-26 15:58:58 -03:00
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration);
enqueue(sim_queue, copy);
2023-10-26 16:11:12 -03:00
addedJobs++;
2023-10-26 15:58:58 -03:00
}
process = process->prev_elem;
}
2023-10-26 16:11:12 -03:00
// Begin printing the current job
2023-10-26 15:58:58 -03:00
process = sim_queue->end;
2023-10-26 17:15:05 -03:00
if (sim_queue->size == 0) { //If there is nothing in sim_queue, print "-"
2023-10-26 15:58:58 -03:00
printf("%d\t-\n", time);
2023-10-26 17:15:05 -03:00
if (addedJobs == in_queue->size) {
break; // If all jobs have been added, and the simulation queue is empty, then we are done
2023-10-26 15:58:58 -03:00
}
} else {
2023-10-26 16:11:12 -03:00
printf("%d\t%c\n", time, process->job); // Print the current job
2023-10-26 15:58:58 -03:00
process->duration--;
quantum--;
2023-10-26 16:11:12 -03:00
if (process->duration == 0) { // If the process is done, delete it
2023-10-26 17:15:05 -03:00
Process *temp = dequeue(sim_queue); // Store the process in a temp variable for deletion
search(summary_queue, temp->username)->finish_time = time; // Set the finish time for the summary queue
destroyProcess(temp); // This should be called on every process
quantum = QUANTUM; // Make sure to reset the quantum when a process is done
2023-10-26 15:58:58 -03:00
} else if (quantum == 0) { // If the quantum is 0, then we need to dequeue the process and enqueue it again
process = dequeue(sim_queue);
enqueue(sim_queue, process);
quantum = QUANTUM;
}
}
}
2023-10-24 13:50:05 -03:00
2023-10-26 17:15:05 -03:00
// Print the summary
2023-10-26 15:58:58 -03:00
printf("\nSummary\n");
2023-10-26 17:15:05 -03:00
printList(summary_queue);
2023-10-26 16:11:12 -03:00
2023-10-26 17:15:05 -03:00
// Free memory for the simulation queue. There should be nothing left in it
2023-10-26 16:11:12 -03:00
stop(sim_queue);
2023-10-26 17:15:05 -03:00
// We never dequeue from the summary queue, so we don't need to make sure about freeing dequeued processes
stop(summary_queue);
2023-10-24 13:50:05 -03:00
}
int main() {
2023-10-26 17:15:05 -03:00
Queue *in_queue = input_queue(); // Create the input queue
simulation(in_queue); // Run simulation on input queue
stop(in_queue); // Free memory for input queue
return 0;
2023-10-24 13:50:05 -03:00
}