Better implemenation?

This commit is contained in:
Isaac Shoebottom 2023-11-28 20:12:47 -04:00
parent 9a170e3334
commit ca2d32dc34
2 changed files with 82 additions and 35 deletions

View File

@ -1,8 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="1" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="1" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
<configuration default="false" name="1" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="F" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/all_known_in_advance_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
<method v="2">
<option name="CLION.COMPOUND.BUILD" enabled="true" />
</method>
</configuration>
</component>

View File

@ -9,7 +9,6 @@ typedef struct DiskRequest {
typedef struct DiskQueue {
DiskRequest *requests;
int index;
int size;
} DiskQueue;
@ -24,7 +23,6 @@ DiskQueue *createDiskQueue() {
printf("Error allocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->index = 0;
queue->size = 0;
return queue;
}
@ -38,21 +36,65 @@ DiskRequest diskRequest(int position, int time) {
void enqueue(DiskQueue *queue, DiskRequest request) {
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size + 1));
if (queue->requests == NULL) {
printf("Error reallocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->requests[queue->size] = request;
queue->size++;
}
DiskRequest dequeue(DiskQueue *queue) {
DiskRequest request = queue->requests[queue->index];
queue->index++;
DiskRequest delete(DiskQueue *queue, DiskRequest request) {
// Find the index of the request
int index = -1;
for (int i = 0; i < queue->size; i++) {
if (queue->requests[i].position == request.position && queue->requests[i].time == request.time) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: Request not found in queue\n");
exit(EXIT_FAILURE);
}
// Shift all the elements after the index down one
for (int i = index; i < queue->size - 1; i++) {
queue->requests[i] = queue->requests[i + 1];
}
// Resize the queue
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size - 1));
if (queue->requests == NULL) {
printf("Error reallocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->size--;
return request;
}
// Allocates a new queue with the requests that have the same time
DiskQueue *queueOfSameTime(DiskQueue *queue, int time) {
DiskQueue* clone(DiskQueue *queue) {
DiskQueue *clone = createDiskQueue();
for (int i = 0; i < queue->size; i++) {
enqueue(clone, queue->requests[i]);
}
return clone;
}
DiskRequest peek(DiskQueue *queue) {
return queue->requests[0];
}
DiskRequest dequeue(DiskQueue *queue) {
return delete(queue, peek(queue));
}
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time) {
DiskQueue *sameTimeQueue = createDiskQueue();
for (int i = queue->index; i < queue->size - queue->index; i++) {
if (queue->requests[i].time == time) {
for (int i = 0; i < queue->size; i++) {
if (queue->requests[i].time <= time) {
enqueue(sameTimeQueue, queue->requests[i]);
}
}
@ -111,7 +153,9 @@ double timeToProcessRequest(int position, int destination) {
// In first come first the time the request comes in is irrelevant
void fcfs(int start) {
int position = start;
for (int i = 0; i < queue->size; i++) {
int size = queue->size;
for (int i = 0; i < size; i++) {
DiskRequest request = dequeue(queue);
while (seekTime < request.time) { seekTime++; }
seekTime += timeToProcessRequest(position, request.position);
@ -121,31 +165,35 @@ void fcfs(int start) {
}
void cscan(int start) {
int currentTime = 0;
int position = start;
for (int i = 0; i < queue->size; i++) {
DiskRequest request = dequeue(queue);
while (currentTime < request.time) { currentTime++; }
DiskQueue *sameTimeQueue = sortQueueByPosition(queueOfSameTime(queue, request.time));
for (; i < sameTimeQueue->size; i++) {
DiskRequest request = dequeue(sameTimeQueue);
DiskQueue *workingQueue = clone(queue);
// Need to return to 0 because we have reached the end of the disk on the current pass
if (request.position < position) {
seekTime += timeToProcessRequest(position, 9999);
movement += abs(position - 9999);
position = 9999;
seekTime += timeToProcessRequest(position, 0);
movement += abs(position - 0);
position = 0;
}
seekTime += timeToProcessRequest(position, request.position);
movement += abs(position - request.position);
position = request.position;
while (workingQueue->size > 0) {
DiskQueue *candidates = queueOfLesserTime(workingQueue, seekTime);
if (candidates->size == 0) {
while (seekTime < peek(workingQueue).time) { seekTime++; }
continue;
}
//destroyDiskQueue(sameTimeQueue);
sortQueueByPosition(candidates);
DiskRequest request = dequeue(candidates);
if (request.position < position) {
// Go to end of disk and back to 0
seekTime += timeToProcessRequest(position, 9999);
seekTime += timeToProcessRequest(9999, 0);
movement += abs(position - 9999);
movement += abs(9999 - 0);
position = 0;
}
seekTime += timeToProcessRequest(position, request.position);
movement += abs(position - request.position);
position = request.position;
delete(workingQueue, request);
destroyDiskQueue(candidates);
}
destroyDiskQueue(workingQueue);
}