Add in progress a5
This commit is contained in:
parent
c1b88cf242
commit
b653288368
@ -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>
|
@ -4,4 +4,4 @@ project(Assignment5 C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
|
||||
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)
|
||||
|
@ -1,7 +1,7 @@
|
||||
1
|
||||
3
|
||||
User Process Arrival Duration
|
||||
Jim A 2 5
|
||||
Mary B 2 2
|
||||
Sue C 5 5
|
||||
Mary D 6 2
|
||||
Jim A 2 5
|
||||
Mary B 2 2
|
||||
Sue C 5 5
|
||||
Mary D 6 2
|
@ -1,35 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include "lib/queue.h"
|
||||
|
||||
#define MAX_USERNAME_LENGTH 100
|
||||
int QUANTUM;
|
||||
int CPUS;
|
||||
|
||||
Queue *input_queue() {
|
||||
Queue *queue = createQueue();
|
||||
char username[MAX_USERNAME_LENGTH]; // username buffer
|
||||
char job;
|
||||
int arrival_time, duration;
|
||||
int arrival_time, duration, affinity;
|
||||
|
||||
scanf("%d", &QUANTUM);
|
||||
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
|
||||
while (scanf("%99s %c %d %d", username, &job, &arrival_time, &duration) != EOF) {
|
||||
Process *process = createProcess(username, job, arrival_time, duration);
|
||||
while (scanf("%99s %c %d %d %d", username, &job, &arrival_time, &duration, &affinity) != EOF) {
|
||||
Process *process = createProcess(username, job, arrival_time, duration, affinity);
|
||||
enqueue(queue, process);
|
||||
}
|
||||
return queue;
|
||||
}
|
||||
|
||||
void* print(void *print_queue_void) {
|
||||
// Cast the input queue to a Queue pointer
|
||||
Queue *print_queue = (Queue*) print_queue_void;
|
||||
|
||||
void simulation(Queue *in_queue) {
|
||||
// Summary creation
|
||||
// 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;
|
||||
|
||||
// Summary creation
|
||||
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);
|
||||
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration, process->affinity);
|
||||
enqueue(summary_queue, copy);
|
||||
}
|
||||
process = process->prev_elem;
|
||||
@ -51,7 +75,7 @@ void simulation(Queue *in_queue) {
|
||||
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);
|
||||
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration, process->affinity);
|
||||
enqueue(sim_queue, copy);
|
||||
addedJobs++;
|
||||
}
|
||||
@ -95,7 +119,24 @@ void simulation(Queue *in_queue) {
|
||||
|
||||
int main() {
|
||||
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
|
||||
return 0;
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
|
||||
//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
|
||||
Process *node = calloc(1, sizeof(Process));
|
||||
if (node == NULL) {
|
||||
@ -26,6 +26,9 @@ Process *createProcess(char *username, char job, int arrival_time, int duration)
|
||||
node->arrival_time = arrival_time;
|
||||
node->duration = duration;
|
||||
node->next_elem = NULL;
|
||||
node->prev_elem = NULL;
|
||||
node->finish_time = 0;
|
||||
node->affinity = affinity;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
@ -9,9 +9,10 @@ typedef struct Process {
|
||||
int arrival_time;
|
||||
int duration;
|
||||
int finish_time;
|
||||
int affinity;
|
||||
} 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);
|
||||
|
||||
|
@ -71,6 +71,7 @@ Process *dequeue(Queue *queue) {
|
||||
queue->size--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
void printList(Queue *queue) {
|
||||
Process *current = queue->end;
|
||||
while (current != NULL) {
|
||||
|
@ -1,6 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("Hello, World!\n");
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user