Add working but shit a4 implementation

This commit is contained in:
Isaac Shoebottom 2023-10-26 15:58:58 -03:00
parent a6ad3f17a1
commit ac641e6e46
6 changed files with 118 additions and 43 deletions

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Assignment4 Input 1" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/documentation/a4_sample_input_1.txt" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment4" TARGET_NAME="Assignment4" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment4" RUN_TARGET_NAME="Assignment4">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Assignment4 Input 2" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/documentation/a4_sample_input_2.txt" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment4" TARGET_NAME="Assignment4" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment4" RUN_TARGET_NAME="Assignment4">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Assignment4 Manual Input" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" REDIRECT_INPUT_PATH="$PROJECT_DIR$/documentation/a4_sample_input_1.txt" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment4" TARGET_NAME="Assignment4" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment4" RUN_TARGET_NAME="Assignment4">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

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

View File

@ -62,13 +62,15 @@ Process *dequeue(Queue *queue) {
}
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;
}
temp->prev_elem = NULL; // The dequeued element should not point to anything
queue->size--;
return temp;

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <string.h>
#include "lib/queue.h"
#define MAX_USERNAME_LENGTH 100
@ -16,7 +17,9 @@ Queue* input_queue() {
char username[MAX_USERNAME_LENGTH];
char job;
int arrival_time, duration;
while(scanf("%s100 %c %d %d", username, &job, &arrival_time, &duration) != EOF) {
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);
}
@ -25,29 +28,78 @@ Queue* input_queue() {
void simulation(Queue *queue) {
printf("Time\tJob\n");
int time = 0;
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());
}