From ac641e6e467e876d1fce38d4292ba987230c9996 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Thu, 26 Oct 2023 15:58:58 -0300 Subject: [PATCH] Add working but shit a4 implementation --- .../runConfigurations/Assignment4_Input_1.xml | 7 ++ .../runConfigurations/Assignment4_Input_2.xml | 7 ++ .../Assignment4_Manual_Input.xml | 7 ++ Assignment4/lib/process.h | 10 +- Assignment4/lib/queue.c | 14 ++- Assignment4/round_robin.c | 116 +++++++++++++----- 6 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 Assignment4/.idea/runConfigurations/Assignment4_Input_1.xml create mode 100644 Assignment4/.idea/runConfigurations/Assignment4_Input_2.xml create mode 100644 Assignment4/.idea/runConfigurations/Assignment4_Manual_Input.xml diff --git a/Assignment4/.idea/runConfigurations/Assignment4_Input_1.xml b/Assignment4/.idea/runConfigurations/Assignment4_Input_1.xml new file mode 100644 index 0000000..c11d63f --- /dev/null +++ b/Assignment4/.idea/runConfigurations/Assignment4_Input_1.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Assignment4/.idea/runConfigurations/Assignment4_Input_2.xml b/Assignment4/.idea/runConfigurations/Assignment4_Input_2.xml new file mode 100644 index 0000000..a3ac406 --- /dev/null +++ b/Assignment4/.idea/runConfigurations/Assignment4_Input_2.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Assignment4/.idea/runConfigurations/Assignment4_Manual_Input.xml b/Assignment4/.idea/runConfigurations/Assignment4_Manual_Input.xml new file mode 100644 index 0000000..914cea9 --- /dev/null +++ b/Assignment4/.idea/runConfigurations/Assignment4_Manual_Input.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Assignment4/lib/process.h b/Assignment4/lib/process.h index e4bba60..7dda7b9 100644 --- a/Assignment4/lib/process.h +++ b/Assignment4/lib/process.h @@ -1,9 +1,9 @@ -#ifndef NODE_H -#define NODE_H +#ifndef PROCESS_H +#define PROCESS_H typedef struct Process { - struct process *prev_elem; - struct process *next_elem; + struct Process *prev_elem; + struct Process *next_elem; char *username; char job; int arrival_time; @@ -14,4 +14,4 @@ Process *createProcess(char *username, char job, int arrival_time, int duration) void destroyProcess(Process *node); -#endif //NODE_H +#endif //PROCESS_H diff --git a/Assignment4/lib/queue.c b/Assignment4/lib/queue.c index a11a0d9..05bd05a 100644 --- a/Assignment4/lib/queue.c +++ b/Assignment4/lib/queue.c @@ -60,14 +60,16 @@ Process *dequeue(Queue *queue) { if (queue->end == NULL) { // If the queue is empty, return NULL return NULL; } - Process *temp = queue->end; // Store the end of the queue for returning later + Process *temp = queue->end; // Store the end of the queue for returning later + if (queue->size == 1) { // If the queue has one element, set the start and end to NULL + queue->start = NULL; + queue->end = NULL; + queue->size--; + return temp; + } queue->end = queue->end->prev_elem; // Set the end to the previous element - queue->end->next_elem = NULL; // Set the next element of the new end to NULL - - if (queue->end == NULL) { // If the queue is empty, set the start to NULL - queue->start = NULL; - } + queue->end->next_elem = NULL; // Set the next element of the new end to NULL temp->prev_elem = NULL; // The dequeued element should not point to anything queue->size--; diff --git a/Assignment4/round_robin.c b/Assignment4/round_robin.c index 064f450..cab4c71 100644 --- a/Assignment4/round_robin.c +++ b/Assignment4/round_robin.c @@ -1,53 +1,105 @@ #include +#include #include "lib/queue.h" #define MAX_USERNAME_LENGTH 100 int QUANTUM; typedef struct Summary { - char username[MAX_USERNAME_LENGTH]; - int last_time; + char username[MAX_USERNAME_LENGTH]; + int last_time; } Summary; -Queue* input_queue() { - Queue* queue = createQueue(); +Queue *input_queue() { + Queue *queue = createQueue(); - scanf("%d", &QUANTUM); - char username[MAX_USERNAME_LENGTH]; - char job; - int arrival_time, duration; - while(scanf("%s100 %c %d %d", username, &job, &arrival_time, &duration) != EOF) { - Process *process = createProcess(username, job, arrival_time, duration); - enqueue(queue, process); - } - return queue; + scanf("%d", &QUANTUM); + char username[MAX_USERNAME_LENGTH]; + char job; + int arrival_time, duration; + while (getchar() != '\n'); // clear the newline from the buffer + while (getchar() != '\n'); // ignore the rest of the line + 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; } -void simulation(Queue* queue) { - printf("Time\tJob\n"); - int time = 0; - Queue *sim_queue = createQueue(); +void simulation(Queue *queue) { + printf("Time Job\n"); + int time = 1; + Queue *sim_queue = createQueue(); + Process *process = queue->end; + // 24 since the max number of processes is 24 + Summary summary[24]; + int summary_size = sizeof(summary) / sizeof(summary[0]); + int how_many = 0; + for (int i = 0; i < queue->size; ++i) { + for (int j = 0; j < summary_size; ++j) { + if (strcmp(summary[i].username, process->username) != 0) { + strcpy(summary[i].username, process->username); + summary[i].last_time = 0; + break; + } + } + process = process->prev_elem; + } - //Take note of all usernames at the start, and then create an array of summary - //When a job finishes, take note of its username, and then go through the array of summary and find the username - //Once you find the username, assign the last_time to the current time. - //TODO: May need to change as the summary should be sorted by username arrival time + int quantum = QUANTUM; + int done = 0; - //Go through list and check for arrival times at current time - //If arrival time is at current time, add to sim_queue - //If there is nothing in sim_queue, print - - //If there is something in sim queue, store the process in a variable, so we can check the quantum - //If the quantum is 0, then we need to dequeue the process and enqueue it again - //If the quantum is not 0, then we need to print the process and decrement the quantum - //If the process is done, then we need to print the process and dequeue it - //If the process is not done, then we need to print it and leave the queue alone - //Once we reach the point where time is greater than the last arrival time, we need to check if sim_queue is empty - //Once sim_queue is empty, we need to break out of the loop + while (true) { + process = queue->end; + for (int i = 0; i < queue->size; i++) { + if (process->arrival_time == time) { + Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration); + enqueue(sim_queue, copy); + done++; + } + process = process->prev_elem; + } + process = sim_queue->end; + if (sim_queue->size == 0) { //If there is nothing in sim_queue, print - + printf("%d\t-\n", time); + if (done == queue->size) { + break; + } + } else { + printf("%d\t%c\n", time, process->job); + process->duration--; + quantum--; + if (process->duration == 0) { // If the process is done, remove it + Process *temp = dequeue(sim_queue); + for (int i = 0; i < summary_size; ++i) { + if (strcmp(summary[i].username, temp->username) == 0) { + summary[i].last_time = time; + how_many++; + break; + } + } + destroyProcess(temp); + quantum = QUANTUM; + } 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; + } + } + time++; + } + + printf("\nSummary\n"); + for (int i = 0; i < how_many; i++) { + if (summary[i].last_time != 0) { + printf("%s\t%d\n", summary[i].username, summary[i].last_time); + } + } } int main() { - Queue* input = input_queue(); + simulation(input_queue()); }