Finish a4

This commit is contained in:
Isaac Shoebottom 2023-10-26 17:15:05 -03:00
parent 8d66016007
commit 1c3056ac56
5 changed files with 79 additions and 76 deletions

27
Assignment4/README.md Normal file
View File

@ -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 .
```

View File

@ -8,6 +8,7 @@ typedef struct Process {
char job; char job;
int arrival_time; int arrival_time;
int duration; int duration;
int finish_time;
} Process; } Process;
Process *createProcess(char *username, char job, int arrival_time, int duration); Process *createProcess(char *username, char job, int arrival_time, int duration);

View File

@ -16,27 +16,24 @@
* And when you dequeue, you are exiting from the end of the line * And when you dequeue, you are exiting from the end of the line
*/ */
//TODO: Refactor int contains(Queue *queue, char *username) {
int contains(Queue *queue, char *job) { Process *current = queue->end;
Process *current = queue->start;
while (current != NULL) { while (current != NULL) {
if (strcmp(current->username, job) == 0) { if (strcmp(current->username, username) == 0) {
return true; return true;
} }
current = current->next_elem; current = current->prev_elem;
} }
return false; return false;
} }
//TODO: Refactor Process *search(Queue *queue, char *username) {
Process *search(Queue *queue, char *job) { Process *current = queue->end;
Process *current = queue->start;
while (current != NULL) { while (current != NULL) {
if (strcmp(current->username, job) == 0) { if (strcmp(current->username, username) == 0) {
return current; return current;
} }
current = current->next_elem; current = current->prev_elem;
} }
return NULL; return NULL;
} }
@ -54,7 +51,6 @@ void enqueue(Queue *queue, Process *process) {
queue->size++; queue->size++;
} }
// WARNING: Returns a pointer to a process that is not in the queue, it is your responsibility to free it // WARNING: Returns a pointer to a process that is not in the queue, it is your responsibility to free it
Process *dequeue(Queue *queue) { Process *dequeue(Queue *queue) {
if (queue->end == NULL) { // If the queue is empty, return NULL if (queue->end == NULL) { // If the queue is empty, return NULL
@ -75,17 +71,13 @@ Process *dequeue(Queue *queue) {
queue->size--; queue->size--;
return temp; return temp;
} }
//TODO: Refactor
void printList(Queue *queue) { void printList(Queue *queue) {
Process *current = queue->start; Process *current = queue->end;
while (current != NULL) { while (current != NULL) {
// printf("%s %d\n", current->username, current->duration); printf("%s\t%d\n", current->username, current->finish_time);
current = current->next_elem; current = current->prev_elem;
} }
} }
//TODO: Refactor
int stop(Queue *queue) { int stop(Queue *queue) {
Process *current = queue->end; Process *current = queue->end;
while (current != NULL) { while (current != NULL) {

View File

@ -10,9 +10,9 @@ typedef struct Queue {
int size; int size;
} Queue; } 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); void enqueue(Queue *queue, Process *process);

View File

@ -1,24 +1,20 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "lib/queue.h" #include "lib/queue.h"
#define MAX_USERNAME_LENGTH 100 #define MAX_USERNAME_LENGTH 100
int QUANTUM; int QUANTUM;
typedef struct Summary {
char username[MAX_USERNAME_LENGTH];
int last_time;
} Summary;
Queue *input_queue() { Queue *input_queue() {
Queue *queue = createQueue(); Queue *queue = createQueue();
char username[MAX_USERNAME_LENGTH]; // username buffer
scanf("%d", &QUANTUM);
char username[MAX_USERNAME_LENGTH];
char job; char job;
int arrival_time, duration; int arrival_time, duration;
scanf("%d", &QUANTUM);
while (getchar() != '\n'); // clear the newline from the buffer 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) { while (scanf("%99s %c %d %d", username, &job, &arrival_time, &duration) != EOF) {
Process *process = createProcess(username, job, arrival_time, duration); Process *process = createProcess(username, job, arrival_time, duration);
enqueue(queue, process); enqueue(queue, process);
@ -27,24 +23,14 @@ Queue *input_queue() {
} }
void simulation(Queue *queue) { void simulation(Queue *in_queue) {
printf("Time Job\n");
int time = 1; // Timer starts at 1
Queue *sim_queue = createQueue(); // Create a queue for the simulation
// Summary creation // Summary creation
Process *process = queue->end; Process *process = in_queue->end;
// 24 since the max number of processes is 24 Queue *summary_queue = createQueue();
Summary summary[24]; for (int i = 0; i < in_queue->size; ++i) {
int summary_size = sizeof(summary) / sizeof(summary[0]); if (contains(summary_queue, process->username) == false) {
int how_many = 0; Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration);
for (int i = 0; i < queue->size; ++i) { enqueue(summary_queue, copy);
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; process = process->prev_elem;
} }
@ -52,11 +38,17 @@ void simulation(Queue *queue) {
// Loop variables // Loop variables
int quantum = QUANTUM; int quantum = QUANTUM;
int addedJobs = 0; int addedJobs = 0;
int time = 0;
// Create a queue for the simulation
Queue *sim_queue = createQueue();
printf("Time\tJob\n");
while (true) { while (true) {
time++;
// Begin going through all jobs and enqueueing them if they have arrived // Begin going through all jobs and enqueueing them if they have arrived
process = queue->end; process = in_queue->end;
for (int i = 0; i < queue->size; i++) { for (int i = 0; i < in_queue->size; i++) {
if (process->arrival_time == time) { if (process->arrival_time == time) {
// Create copy to keep the queues separate // Create copy to keep the queues separate
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration); 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 // Begin printing the current job
process = sim_queue->end; 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); printf("%d\t-\n", time);
if (addedJobs == queue->size) { if (addedJobs == in_queue->size) {
break; break; // If all jobs have been added, and the simulation queue is empty, then we are done
} }
} else { } else {
printf("%d\t%c\n", time, process->job); // Print the current job printf("%d\t%c\n", time, process->job); // Print the current job
process->duration--; process->duration--;
quantum--; quantum--;
if (process->duration == 0) { // If the process is done, delete it if (process->duration == 0) { // If the process is done, delete it
Process *temp = dequeue(sim_queue); 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
// Update summary destroyProcess(temp); // This should be called on every process
for (int i = 0; i < summary_size; ++i) { quantum = QUANTUM; // Make sure to reset the quantum when a process is done
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 } else if (quantum == 0) { // If the quantum is 0, then we need to dequeue the process and enqueue it again
process = dequeue(sim_queue); process = dequeue(sim_queue);
enqueue(sim_queue, process); enqueue(sim_queue, process);
quantum = QUANTUM; quantum = QUANTUM;
} }
} }
time++;
} }
// Print summary // Print the summary
printf("\nSummary\n"); printf("\nSummary\n");
for (int i = 0; i < how_many; i++) { printList(summary_queue);
if (summary[i].last_time != 0) {
printf("%s\t%d\n", summary[i].username, summary[i].last_time);
}
}
// Free memory // Free memory for the simulation queue. There should be nothing left in it
stop(sim_queue); 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() { 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;
} }