|
|
|
@@ -6,14 +6,26 @@
|
|
|
|
|
|
|
|
|
|
// TODO: Look into semaphore increment too much in print function, look into synchronization
|
|
|
|
|
|
|
|
|
|
// TODO: Need to use semaphore for thread synchronization, and mutex for shared variables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_USERNAME_LENGTH 100
|
|
|
|
|
int QUANTUM;
|
|
|
|
|
int CPUS;
|
|
|
|
|
int TIME = 0;
|
|
|
|
|
|
|
|
|
|
// Semaphore for the print function
|
|
|
|
|
sem_t print_semaphore;
|
|
|
|
|
// Semaphore for the threads to simulate
|
|
|
|
|
sem_t sim_sem;
|
|
|
|
|
|
|
|
|
|
// Semaphore for the thread to control printing
|
|
|
|
|
sem_t print_sem;
|
|
|
|
|
|
|
|
|
|
// Mutex for simulation
|
|
|
|
|
pthread_mutex_t sim_mutex;
|
|
|
|
|
|
|
|
|
|
// Mutex for thread finish count
|
|
|
|
|
pthread_mutex_t finish_mutex;
|
|
|
|
|
int finish_count = 0;
|
|
|
|
|
|
|
|
|
|
typedef struct ThreadArgs {
|
|
|
|
|
int cpu_id;
|
|
|
|
@@ -22,7 +34,7 @@ typedef struct ThreadArgs {
|
|
|
|
|
Queue *in_queue;
|
|
|
|
|
} ThreadArgs;
|
|
|
|
|
|
|
|
|
|
ThreadArgs *createArgs(int cpu_id, char* print_buffer, Queue *summary_queue, Queue *in_queue) {
|
|
|
|
|
ThreadArgs *createArgs(int cpu_id, char *print_buffer, Queue *summary_queue, Queue *in_queue) {
|
|
|
|
|
ThreadArgs *args = malloc(sizeof(ThreadArgs));
|
|
|
|
|
args->cpu_id = cpu_id;
|
|
|
|
|
args->print_buffer = print_buffer;
|
|
|
|
@@ -37,14 +49,11 @@ Queue *input_queue() {
|
|
|
|
|
char job;
|
|
|
|
|
int arrival_time, duration, affinity;
|
|
|
|
|
|
|
|
|
|
scanf("%d", &QUANTUM);
|
|
|
|
|
while (getchar() != '\n'); // clear the newline from the buffer
|
|
|
|
|
|
|
|
|
|
scanf("%d", &CPUS);
|
|
|
|
|
while (getchar() != '\n'); // clear the newline from the buffer
|
|
|
|
|
|
|
|
|
|
// Make sure sem is init right after getting cpus
|
|
|
|
|
sem_init(&print_semaphore, 0, CPUS); // Initialize the semaphore
|
|
|
|
|
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
|
|
|
|
|
// Loop through the process table and enqueue each process
|
|
|
|
@@ -55,9 +64,9 @@ Queue *input_queue() {
|
|
|
|
|
return queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* print(void *args) {
|
|
|
|
|
void *print(void *args) {
|
|
|
|
|
// Cast args and create local variables
|
|
|
|
|
ThreadArgs *thread_args = (ThreadArgs*) args;
|
|
|
|
|
ThreadArgs *thread_args = (ThreadArgs *) args;
|
|
|
|
|
char *print_buffer = thread_args->print_buffer;
|
|
|
|
|
Queue *summary_queue = thread_args->summary_queue;
|
|
|
|
|
Queue *in_queue = thread_args->in_queue;
|
|
|
|
@@ -69,15 +78,38 @@ void* print(void *args) {
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < in_queue->size; ++i) {
|
|
|
|
|
TIME++;
|
|
|
|
|
int test = 0;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
pthread_mutex_lock(&sim_mutex);
|
|
|
|
|
if (finish_count == CPUS) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("%d", TIME);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < CPUS; ++i) {
|
|
|
|
|
// Allow the simulation to begin
|
|
|
|
|
sem_post(&print_semaphore);
|
|
|
|
|
//sem_post(&sim_sem);
|
|
|
|
|
|
|
|
|
|
// Wait for the simulation to finish
|
|
|
|
|
//sem_wait(&print_sem);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < CPUS; ++j) {
|
|
|
|
|
printf("%d\t%c", TIME, print_buffer[i]);
|
|
|
|
|
|
|
|
|
|
printf("\t%c", print_buffer[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
TIME++;
|
|
|
|
|
|
|
|
|
|
test++;
|
|
|
|
|
if (test == 35) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(&sim_mutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print the summary
|
|
|
|
@@ -88,9 +120,9 @@ void* print(void *args) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* simulation(void *args) {
|
|
|
|
|
void *simulation(void *args) {
|
|
|
|
|
// Cast args and create local variables
|
|
|
|
|
ThreadArgs *thread_args = (ThreadArgs*) args;
|
|
|
|
|
ThreadArgs *thread_args = (ThreadArgs *) args;
|
|
|
|
|
Queue *in_queue = thread_args->in_queue;
|
|
|
|
|
Queue *summary_queue = thread_args->summary_queue;
|
|
|
|
|
char *print_buffer = thread_args->print_buffer;
|
|
|
|
@@ -105,7 +137,9 @@ void* simulation(void *args) {
|
|
|
|
|
// Create a queue for the simulation
|
|
|
|
|
Queue *sim_queue = createQueue();
|
|
|
|
|
while (true) {
|
|
|
|
|
sem_wait(&print_semaphore); // Wait for the print semaphore
|
|
|
|
|
pthread_mutex_lock(&sim_mutex);
|
|
|
|
|
|
|
|
|
|
//sem_wait(&sim_sem); // Wait for the thread to be allowed to start
|
|
|
|
|
// Begin going through all jobs and enqueueing them if they have arrived
|
|
|
|
|
process = in_queue->end;
|
|
|
|
|
for (int i = 0; i < in_queue->size; i++) {
|
|
|
|
@@ -140,16 +174,32 @@ void* simulation(void *args) {
|
|
|
|
|
quantum = QUANTUM;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Allow the print thread to print
|
|
|
|
|
//sem_post(&print_sem);
|
|
|
|
|
pthread_mutex_unlock(&sim_mutex);
|
|
|
|
|
}
|
|
|
|
|
// Free memory for the simulation queue. There should be nothing left in it
|
|
|
|
|
stop(sim_queue);
|
|
|
|
|
|
|
|
|
|
// Signal that the thread is done
|
|
|
|
|
pthread_mutex_lock(&finish_mutex);
|
|
|
|
|
finish_count++;
|
|
|
|
|
pthread_mutex_unlock(&finish_mutex);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
|
Queue *in_queue = input_queue(); // Create the input queue
|
|
|
|
|
|
|
|
|
|
// Make sure sem is init right after getting cpus, which is done in input_queue
|
|
|
|
|
//sem_init(&sim_sem, 0, CPUS); // Initialize the semaphore
|
|
|
|
|
//sem_init(&print_sem, 0, 0); // Initialize the semaphore
|
|
|
|
|
pthread_mutex_init(&finish_mutex, NULL); // Initialize the mutex
|
|
|
|
|
pthread_mutex_init(&sim_mutex, NULL); // Initialize the mutex
|
|
|
|
|
|
|
|
|
|
Queue *summary_queue = createQueue(); // Create the summary queue
|
|
|
|
|
char *print_buffer = malloc(sizeof(char) * CPUS); // Create the print buffer
|
|
|
|
|
|
|
|
|
@@ -170,7 +220,7 @@ int main() {
|
|
|
|
|
|
|
|
|
|
// Create the simulation threads
|
|
|
|
|
pthread_t threads[CPUS];
|
|
|
|
|
ThreadArgs* args[CPUS]; // Array of arguments for each thread, so we can free them later
|
|
|
|
|
ThreadArgs *args[CPUS]; // Array of arguments for each thread, so we can free them later
|
|
|
|
|
for (int i = 0; i < CPUS; i++) {
|
|
|
|
|
args[i] = createArgs(i, print_buffer, summary_queue, in_queue);
|
|
|
|
|
pthread_create(&threads[i], NULL, &simulation, args[i]);
|
|
|
|
|