Work on a5

This commit is contained in:
Isaac Shoebottom 2023-10-30 12:21:07 -03:00
parent 72ab0d8fe9
commit 3b149882ee
6 changed files with 181 additions and 81 deletions

View File

@ -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
User Process Arrival Duration Affinity
Jim A 2 5 0
Mary B 2 2 0
Sue C 5 5 0
Mary D 6 2 0

View File

@ -0,0 +1,7 @@
2
3 2
User Process Arrival Duration Affinity
Jim A 2 5 0
Mary B 2 2 1
Sue C 5 5 1
Mary D 6 2 0

View File

@ -0,0 +1,8 @@
2
3 2
User Process Arrival Duration Affinity
Jim A 2 5 0
Mary B 2 2 1
Mary C 3 4 1
Sue D 5 5 1
Mary E 6 2 0

View File

@ -0,0 +1,16 @@
Time CPU0 CPU1
1 - -
2 A B
3 A B
4 A -
5 A C
6 A C
7 D C
8 D C
9 - C
10 - -
Summary
Jim 6
Mary 8
Sue 9

View File

@ -0,0 +1,19 @@
Time CPU0 CPU1
1 - -
2 A B
3 A B
4 A C
5 A C
6 A D
7 E D
8 E C
9 - C
10 - D
11 - D
12 - D
13 - -
Summary
Jim 6
Mary 9
Sue 12

View File

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