Add some comments to a4
This commit is contained in:
parent
ac641e6e46
commit
8d66016007
@ -29,8 +29,10 @@ Queue *input_queue() {
|
|||||||
|
|
||||||
void simulation(Queue *queue) {
|
void simulation(Queue *queue) {
|
||||||
printf("Time Job\n");
|
printf("Time Job\n");
|
||||||
int time = 1;
|
int time = 1; // Timer starts at 1
|
||||||
Queue *sim_queue = createQueue();
|
Queue *sim_queue = createQueue(); // Create a queue for the simulation
|
||||||
|
|
||||||
|
// Summary creation
|
||||||
Process *process = queue->end;
|
Process *process = queue->end;
|
||||||
// 24 since the max number of processes is 24
|
// 24 since the max number of processes is 24
|
||||||
Summary summary[24];
|
Summary summary[24];
|
||||||
@ -47,32 +49,38 @@ void simulation(Queue *queue) {
|
|||||||
process = process->prev_elem;
|
process = process->prev_elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Loop variables
|
||||||
int quantum = QUANTUM;
|
int quantum = QUANTUM;
|
||||||
int done = 0;
|
int addedJobs = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
// Begin going through all jobs and enqueueing them if they have arrived
|
||||||
process = queue->end;
|
process = queue->end;
|
||||||
for (int i = 0; i < queue->size; i++) {
|
for (int i = 0; i < queue->size; i++) {
|
||||||
if (process->arrival_time == time) {
|
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);
|
||||||
enqueue(sim_queue, copy);
|
enqueue(sim_queue, copy);
|
||||||
done++;
|
addedJobs++;
|
||||||
}
|
}
|
||||||
process = process->prev_elem;
|
process = process->prev_elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Begin printing the current job
|
||||||
process = sim_queue->end;
|
process = sim_queue->end;
|
||||||
if (sim_queue->size == 0) { //If there is nothing in sim_queue, print -
|
if (sim_queue->size == 0) { //If there is nothing in sim_queue, print -
|
||||||
printf("%d\t-\n", time);
|
printf("%d\t-\n", time);
|
||||||
if (done == queue->size) {
|
if (addedJobs == queue->size) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("%d\t%c\n", time, process->job);
|
printf("%d\t%c\n", time, process->job); // Print the current job
|
||||||
process->duration--;
|
process->duration--;
|
||||||
quantum--;
|
quantum--;
|
||||||
if (process->duration == 0) { // If the process is done, remove it
|
if (process->duration == 0) { // If the process is done, delete it
|
||||||
Process *temp = dequeue(sim_queue);
|
Process *temp = dequeue(sim_queue);
|
||||||
|
|
||||||
|
// Update summary
|
||||||
for (int i = 0; i < summary_size; ++i) {
|
for (int i = 0; i < summary_size; ++i) {
|
||||||
if (strcmp(summary[i].username, temp->username) == 0) {
|
if (strcmp(summary[i].username, temp->username) == 0) {
|
||||||
summary[i].last_time = time;
|
summary[i].last_time = time;
|
||||||
@ -92,12 +100,17 @@ void simulation(Queue *queue) {
|
|||||||
time++;
|
time++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print summary
|
||||||
printf("\nSummary\n");
|
printf("\nSummary\n");
|
||||||
for (int i = 0; i < how_many; i++) {
|
for (int i = 0; i < how_many; i++) {
|
||||||
if (summary[i].last_time != 0) {
|
if (summary[i].last_time != 0) {
|
||||||
printf("%s\t%d\n", summary[i].username, summary[i].last_time);
|
printf("%s\t%d\n", summary[i].username, summary[i].last_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Free memory
|
||||||
|
stop(sim_queue);
|
||||||
|
stop(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user