Add in progress a5

This commit is contained in:
Isaac Shoebottom 2023-10-27 11:24:40 -03:00
parent c1b88cf242
commit b653288368
8 changed files with 69 additions and 22 deletions

View File

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

View File

@ -4,4 +4,4 @@ project(Assignment5 C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
include_directories(lib) include_directories(lib)
add_executable(Assignment5 round_robin.c lib/queue.c lib/process.c) add_executable(Assignment5 hard_affinity.c lib/queue.c lib/process.c)

View File

@ -1,35 +1,59 @@
#include <stdio.h> #include <stdio.h>
#include <pthread.h>
#include "lib/queue.h" #include "lib/queue.h"
#define MAX_USERNAME_LENGTH 100 #define MAX_USERNAME_LENGTH 100
int QUANTUM; int QUANTUM;
int CPUS;
Queue *input_queue() { Queue *input_queue() {
Queue *queue = createQueue(); Queue *queue = createQueue();
char username[MAX_USERNAME_LENGTH]; // username buffer char username[MAX_USERNAME_LENGTH]; // username buffer
char job; char job;
int arrival_time, duration; int arrival_time, duration, affinity;
scanf("%d", &QUANTUM); 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, this is the table line
scanf("%d", &CPUS);
while (getchar() != '\n'); // clear the newline from the buffer
while (getchar() != '\n'); // ignore the rest of the line, this is the table line
// Loop through the process table and enqueue each process // 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 %d", username, &job, &arrival_time, &duration, &affinity) != EOF) {
Process *process = createProcess(username, job, arrival_time, duration); Process *process = createProcess(username, job, arrival_time, duration, affinity);
enqueue(queue, process); enqueue(queue, process);
} }
return queue; return queue;
} }
void* print(void *print_queue_void) {
// Cast the input queue to a Queue pointer
Queue *print_queue = (Queue*) print_queue_void;
// Print the Time label as well as the CPU labels
printf("Time");
for (int i = 0; i < CPUS; i++) {
printf("\tCPU%d", i);
}
printf("\n");
}
void* simulation(void *in_queue_void) {
// Cast the input queue to a Queue pointer
Queue *in_queue = (Queue*) in_queue_void;
void simulation(Queue *in_queue) {
// Summary creation // Summary creation
Process *process = in_queue->end; Process *process = in_queue->end;
Queue *summary_queue = createQueue(); Queue *summary_queue = createQueue();
for (int i = 0; i < in_queue->size; ++i) { for (int i = 0; i < in_queue->size; ++i) {
if (contains(summary_queue, process->username) == false) { if (contains(summary_queue, process->username) == false) {
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration); Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration, process->affinity);
enqueue(summary_queue, copy); enqueue(summary_queue, copy);
} }
process = process->prev_elem; process = process->prev_elem;
@ -51,7 +75,7 @@ void simulation(Queue *in_queue) {
for (int i = 0; i < in_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, process->affinity);
enqueue(sim_queue, copy); enqueue(sim_queue, copy);
addedJobs++; addedJobs++;
} }
@ -95,7 +119,24 @@ void simulation(Queue *in_queue) {
int main() { int main() {
Queue *in_queue = input_queue(); // Create the input queue Queue *in_queue = input_queue(); // Create the input queue
simulation(in_queue); // Run simulation on input queue Queue *print_queue = createQueue(); // Create the print queue
// Create the print thread
pthread_t print_thread;
pthread_create(&print_thread, NULL, &print, print_queue);
// Create the simulation threads
pthread_t threads[CPUS];
for (int i = 0; i < CPUS; i++) {
pthread_create(&threads[i], NULL, &simulation, in_queue);
}
// This should make sure all threads are done simulating, as the print function exits after simulation is done
pthread_join(print_thread, NULL);
// Just to make sure all threads are done
for (int i = 0; i < CPUS; i++) {
pthread_join(threads[i], NULL);
}
stop(in_queue); // Free memory for input queue stop(in_queue); // Free memory for input queue
return 0; return 0;
} }

View File

@ -5,7 +5,7 @@
//Assume that the string passed in is null terminated //Assume that the string passed in is null terminated
Process *createProcess(char *username, char job, int arrival_time, int duration) { Process *createProcess(char *username, char job, int arrival_time, int duration, int affinity) {
// Allocate memory for the process, and check if it was successful // Allocate memory for the process, and check if it was successful
Process *node = calloc(1, sizeof(Process)); Process *node = calloc(1, sizeof(Process));
if (node == NULL) { if (node == NULL) {
@ -26,6 +26,9 @@ Process *createProcess(char *username, char job, int arrival_time, int duration)
node->arrival_time = arrival_time; node->arrival_time = arrival_time;
node->duration = duration; node->duration = duration;
node->next_elem = NULL; node->next_elem = NULL;
node->prev_elem = NULL;
node->finish_time = 0;
node->affinity = affinity;
return node; return node;
} }

View File

@ -9,9 +9,10 @@ typedef struct Process {
int arrival_time; int arrival_time;
int duration; int duration;
int finish_time; int finish_time;
int affinity;
} Process; } Process;
Process *createProcess(char *username, char job, int arrival_time, int duration); Process *createProcess(char *username, char job, int arrival_time, int duration, int affinity);
void destroyProcess(Process *node); void destroyProcess(Process *node);

View File

@ -71,6 +71,7 @@ Process *dequeue(Queue *queue) {
queue->size--; queue->size--;
return temp; return temp;
} }
void printList(Queue *queue) { void printList(Queue *queue) {
Process *current = queue->end; Process *current = queue->end;
while (current != NULL) { while (current != NULL) {

View File

@ -1,6 +0,0 @@
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}