Work on a5
This commit is contained in:
parent
72ab0d8fe9
commit
3b149882ee
@ -1,7 +1,7 @@
|
|||||||
1
|
1
|
||||||
3
|
3
|
||||||
User Process Arrival Duration
|
User Process Arrival Duration Affinity
|
||||||
Jim A 2 5
|
Jim A 2 5 0
|
||||||
Mary B 2 2
|
Mary B 2 2 0
|
||||||
Sue C 5 5
|
Sue C 5 5 0
|
||||||
Mary D 6 2
|
Mary D 6 2 0
|
||||||
|
7
Assignment5/documentation/a5_sample_input_2.txt
Normal file
7
Assignment5/documentation/a5_sample_input_2.txt
Normal 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
|
8
Assignment5/documentation/a5_sample_input_3.txt
Normal file
8
Assignment5/documentation/a5_sample_input_3.txt
Normal 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
|
16
Assignment5/documentation/a5_sample_output_2.txt
Normal file
16
Assignment5/documentation/a5_sample_output_2.txt
Normal 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
|
19
Assignment5/documentation/a5_sample_output_3.txt
Normal file
19
Assignment5/documentation/a5_sample_output_3.txt
Normal 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
|
@ -6,14 +6,26 @@
|
|||||||
|
|
||||||
// TODO: Look into semaphore increment too much in print function, look into synchronization
|
// 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
|
#define MAX_USERNAME_LENGTH 100
|
||||||
int QUANTUM;
|
int QUANTUM;
|
||||||
int CPUS;
|
int CPUS;
|
||||||
int TIME = 0;
|
int TIME = 0;
|
||||||
|
|
||||||
// Semaphore for the print function
|
// Semaphore for the threads to simulate
|
||||||
sem_t print_semaphore;
|
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 {
|
typedef struct ThreadArgs {
|
||||||
int cpu_id;
|
int cpu_id;
|
||||||
@ -22,7 +34,7 @@ typedef struct ThreadArgs {
|
|||||||
Queue *in_queue;
|
Queue *in_queue;
|
||||||
} ThreadArgs;
|
} 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));
|
ThreadArgs *args = malloc(sizeof(ThreadArgs));
|
||||||
args->cpu_id = cpu_id;
|
args->cpu_id = cpu_id;
|
||||||
args->print_buffer = print_buffer;
|
args->print_buffer = print_buffer;
|
||||||
@ -37,14 +49,11 @@ Queue *input_queue() {
|
|||||||
char job;
|
char job;
|
||||||
int arrival_time, duration, affinity;
|
int arrival_time, duration, affinity;
|
||||||
|
|
||||||
scanf("%d", &QUANTUM);
|
|
||||||
while (getchar() != '\n'); // clear the newline from the buffer
|
|
||||||
|
|
||||||
scanf("%d", &CPUS);
|
scanf("%d", &CPUS);
|
||||||
while (getchar() != '\n'); // clear the newline from the buffer
|
while (getchar() != '\n'); // clear the newline from the buffer
|
||||||
|
|
||||||
// Make sure sem is init right after getting cpus
|
scanf("%d", &QUANTUM);
|
||||||
sem_init(&print_semaphore, 0, CPUS); // Initialize the semaphore
|
while (getchar() != '\n'); // clear the newline from the buffer
|
||||||
|
|
||||||
while (getchar() != '\n'); // ignore the rest of the line, this is the table line
|
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
|
||||||
@ -55,9 +64,9 @@ Queue *input_queue() {
|
|||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* print(void *args) {
|
void *print(void *args) {
|
||||||
// Cast args and create local variables
|
// Cast args and create local variables
|
||||||
ThreadArgs *thread_args = (ThreadArgs*) args;
|
ThreadArgs *thread_args = (ThreadArgs *) args;
|
||||||
char *print_buffer = thread_args->print_buffer;
|
char *print_buffer = thread_args->print_buffer;
|
||||||
Queue *summary_queue = thread_args->summary_queue;
|
Queue *summary_queue = thread_args->summary_queue;
|
||||||
Queue *in_queue = thread_args->in_queue;
|
Queue *in_queue = thread_args->in_queue;
|
||||||
@ -69,15 +78,38 @@ void* print(void *args) {
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
for (int i = 0; i < in_queue->size; ++i) {
|
int test = 0;
|
||||||
TIME++;
|
|
||||||
|
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
|
// 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) {
|
for (int j = 0; j < CPUS; ++j) {
|
||||||
printf("%d\t%c", TIME, print_buffer[i]);
|
|
||||||
|
printf("\t%c", print_buffer[j]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
TIME++;
|
||||||
|
|
||||||
|
test++;
|
||||||
|
if (test == 35) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&sim_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the summary
|
// 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
|
// Cast args and create local variables
|
||||||
ThreadArgs *thread_args = (ThreadArgs*) args;
|
ThreadArgs *thread_args = (ThreadArgs *) args;
|
||||||
Queue *in_queue = thread_args->in_queue;
|
Queue *in_queue = thread_args->in_queue;
|
||||||
Queue *summary_queue = thread_args->summary_queue;
|
Queue *summary_queue = thread_args->summary_queue;
|
||||||
char *print_buffer = thread_args->print_buffer;
|
char *print_buffer = thread_args->print_buffer;
|
||||||
@ -105,7 +137,9 @@ void* simulation(void *args) {
|
|||||||
// Create a queue for the simulation
|
// Create a queue for the simulation
|
||||||
Queue *sim_queue = createQueue();
|
Queue *sim_queue = createQueue();
|
||||||
while (true) {
|
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
|
// Begin going through all jobs and enqueueing them if they have arrived
|
||||||
process = in_queue->end;
|
process = in_queue->end;
|
||||||
for (int i = 0; i < in_queue->size; i++) {
|
for (int i = 0; i < in_queue->size; i++) {
|
||||||
@ -140,16 +174,32 @@ void* simulation(void *args) {
|
|||||||
quantum = QUANTUM;
|
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
|
// Free memory for the simulation queue. There should be nothing left in it
|
||||||
stop(sim_queue);
|
stop(sim_queue);
|
||||||
|
|
||||||
|
// Signal that the thread is done
|
||||||
|
pthread_mutex_lock(&finish_mutex);
|
||||||
|
finish_count++;
|
||||||
|
pthread_mutex_unlock(&finish_mutex);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
setvbuf(stdout, NULL, _IONBF, 0);
|
||||||
Queue *in_queue = input_queue(); // Create the input queue
|
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
|
Queue *summary_queue = createQueue(); // Create the summary queue
|
||||||
char *print_buffer = malloc(sizeof(char) * CPUS); // Create the print buffer
|
char *print_buffer = malloc(sizeof(char) * CPUS); // Create the print buffer
|
||||||
|
|
||||||
@ -170,7 +220,7 @@ int main() {
|
|||||||
|
|
||||||
// Create the simulation threads
|
// Create the simulation threads
|
||||||
pthread_t threads[CPUS];
|
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++) {
|
for (int i = 0; i < CPUS; i++) {
|
||||||
args[i] = createArgs(i, print_buffer, summary_queue, in_queue);
|
args[i] = createArgs(i, print_buffer, summary_queue, in_queue);
|
||||||
pthread_create(&threads[i], NULL, &simulation, args[i]);
|
pthread_create(&threads[i], NULL, &simulation, args[i]);
|
||||||
|
Loading…
Reference in New Issue
Block a user