diff --git a/Assignment4/README.md b/Assignment4/README.md new file mode 100644 index 0000000..98bd8c2 --- /dev/null +++ b/Assignment4/README.md @@ -0,0 +1,27 @@ +# How to run + +To compile please run: +```shell +gcc -Ilib lib/process.c lib/queue.c round_robin.c +``` + +And to run please run: +```shell +./a.out < example.txt +``` + +# Notes + +I'm not sure if the input will be exactly like the samples, or if the header table was just for us to help understand, so in case the input is not like the samples, line 15 can be commented out and the program should work as expected. + +# In case of error + +If that command to compile does not work, please place all C and C header files in the same directory and run: +```shell +gcc process.c queue.c round_robin.c +``` + +In case you need to move the files: +```shell +mv lib/process.c . & mv lib/queue.c . & mv lib/queue.h . & mv lib/process.h . & mv lib/round_robin.h . +``` \ No newline at end of file diff --git a/Assignment4/lib/process.h b/Assignment4/lib/process.h index 7dda7b9..8823493 100644 --- a/Assignment4/lib/process.h +++ b/Assignment4/lib/process.h @@ -8,6 +8,7 @@ typedef struct Process { char job; int arrival_time; int duration; + int finish_time; } Process; Process *createProcess(char *username, char job, int arrival_time, int duration); diff --git a/Assignment4/lib/queue.c b/Assignment4/lib/queue.c index 05bd05a..1aeea01 100644 --- a/Assignment4/lib/queue.c +++ b/Assignment4/lib/queue.c @@ -16,27 +16,24 @@ * And when you dequeue, you are exiting from the end of the line */ -//TODO: Refactor -int contains(Queue *queue, char *job) { - Process *current = queue->start; +int contains(Queue *queue, char *username) { + Process *current = queue->end; while (current != NULL) { - if (strcmp(current->username, job) == 0) { + if (strcmp(current->username, username) == 0) { return true; } - current = current->next_elem; + current = current->prev_elem; } return false; } -//TODO: Refactor -Process *search(Queue *queue, char *job) { - Process *current = queue->start; - +Process *search(Queue *queue, char *username) { + Process *current = queue->end; while (current != NULL) { - if (strcmp(current->username, job) == 0) { + if (strcmp(current->username, username) == 0) { return current; } - current = current->next_elem; + current = current->prev_elem; } return NULL; } @@ -54,7 +51,6 @@ void enqueue(Queue *queue, Process *process) { queue->size++; } - // WARNING: Returns a pointer to a process that is not in the queue, it is your responsibility to free it Process *dequeue(Queue *queue) { if (queue->end == NULL) { // If the queue is empty, return NULL @@ -75,17 +71,13 @@ Process *dequeue(Queue *queue) { queue->size--; return temp; } - -//TODO: Refactor void printList(Queue *queue) { - Process *current = queue->start; + Process *current = queue->end; while (current != NULL) { - // printf("%s %d\n", current->username, current->duration); - current = current->next_elem; + printf("%s\t%d\n", current->username, current->finish_time); + current = current->prev_elem; } } - -//TODO: Refactor int stop(Queue *queue) { Process *current = queue->end; while (current != NULL) { diff --git a/Assignment4/lib/queue.h b/Assignment4/lib/queue.h index 37dca9d..b4b3af4 100644 --- a/Assignment4/lib/queue.h +++ b/Assignment4/lib/queue.h @@ -10,9 +10,9 @@ typedef struct Queue { int size; } Queue; -int contains(Queue *queue, char *job); +int contains(Queue *queue, char *username); -Process *search(Queue *queue, char *job); +Process *search(Queue *queue, char *username); void enqueue(Queue *queue, Process *process); diff --git a/Assignment4/round_robin.c b/Assignment4/round_robin.c index a14d9ed..6904737 100644 --- a/Assignment4/round_robin.c +++ b/Assignment4/round_robin.c @@ -1,24 +1,20 @@ #include -#include #include "lib/queue.h" #define MAX_USERNAME_LENGTH 100 int QUANTUM; -typedef struct Summary { - char username[MAX_USERNAME_LENGTH]; - int last_time; -} Summary; - Queue *input_queue() { Queue *queue = createQueue(); - - scanf("%d", &QUANTUM); - char username[MAX_USERNAME_LENGTH]; + char username[MAX_USERNAME_LENGTH]; // username buffer char job; int arrival_time, duration; + + scanf("%d", &QUANTUM); while (getchar() != '\n'); // clear the newline from the buffer - while (getchar() != '\n'); // ignore the rest of the line + while (getchar() != '\n'); // ignore the rest of the line, this is the table line + + // Loop through the process table and enqueue each process while (scanf("%99s %c %d %d", username, &job, &arrival_time, &duration) != EOF) { Process *process = createProcess(username, job, arrival_time, duration); enqueue(queue, process); @@ -27,24 +23,14 @@ Queue *input_queue() { } -void simulation(Queue *queue) { - printf("Time Job\n"); - int time = 1; // Timer starts at 1 - Queue *sim_queue = createQueue(); // Create a queue for the simulation - +void simulation(Queue *in_queue) { // Summary creation - 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 = 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); } process = process->prev_elem; } @@ -52,11 +38,17 @@ void simulation(Queue *queue) { // Loop variables int quantum = QUANTUM; int addedJobs = 0; + int time = 0; + // Create a queue for the simulation + Queue *sim_queue = createQueue(); + + printf("Time\tJob\n"); while (true) { + time++; // Begin going through all jobs and enqueueing them if they have arrived - process = queue->end; - for (int i = 0; i < queue->size; i++) { + process = in_queue->end; + for (int i = 0; i < in_queue->size; i++) { if (process->arrival_time == time) { // Create copy to keep the queues separate Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration); @@ -68,51 +60,42 @@ void simulation(Queue *queue) { // Begin printing the current job process = sim_queue->end; - if (sim_queue->size == 0) { //If there is nothing in sim_queue, print - + if (sim_queue->size == 0) { //If there is nothing in sim_queue, print "-" printf("%d\t-\n", time); - if (addedJobs == queue->size) { - break; + if (addedJobs == in_queue->size) { + break; // If all jobs have been added, and the simulation queue is empty, then we are done } } else { printf("%d\t%c\n", time, process->job); // Print the current job process->duration--; quantum--; if (process->duration == 0) { // If the process is done, delete it - Process *temp = dequeue(sim_queue); - - // Update summary - 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; + 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 } 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++; } - // Print summary + // Print the summary 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); - } - } + printList(summary_queue); - // Free memory + // Free memory for the simulation queue. There should be nothing left in it stop(sim_queue); - stop(queue); + + // We never dequeue from the summary queue, so we don't need to make sure about freeing dequeued processes + stop(summary_queue); } int main() { - simulation(input_queue()); + 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; }