Better implemenation?
This commit is contained in:
parent
9a170e3334
commit
ca2d32dc34
@ -1,8 +1,7 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="1" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
<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">
|
||||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="1" workingDirectory="" arguments="">
|
<method v="2">
|
||||||
<envs />
|
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||||
</makefile>
|
</method>
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</component>
|
</component>
|
@ -9,7 +9,6 @@ typedef struct DiskRequest {
|
|||||||
|
|
||||||
typedef struct DiskQueue {
|
typedef struct DiskQueue {
|
||||||
DiskRequest *requests;
|
DiskRequest *requests;
|
||||||
int index;
|
|
||||||
int size;
|
int size;
|
||||||
} DiskQueue;
|
} DiskQueue;
|
||||||
|
|
||||||
@ -24,7 +23,6 @@ DiskQueue *createDiskQueue() {
|
|||||||
printf("Error allocating memory for queue requests\n");
|
printf("Error allocating memory for queue requests\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
queue->index = 0;
|
|
||||||
queue->size = 0;
|
queue->size = 0;
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
@ -38,21 +36,65 @@ DiskRequest diskRequest(int position, int time) {
|
|||||||
|
|
||||||
void enqueue(DiskQueue *queue, DiskRequest request) {
|
void enqueue(DiskQueue *queue, DiskRequest request) {
|
||||||
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size + 1));
|
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->requests[queue->size] = request;
|
||||||
queue->size++;
|
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;
|
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();
|
DiskQueue *sameTimeQueue = createDiskQueue();
|
||||||
for (int i = queue->index; i < queue->size - queue->index; i++) {
|
for (int i = 0; i < queue->size; i++) {
|
||||||
if (queue->requests[i].time == time) {
|
if (queue->requests[i].time <= time) {
|
||||||
enqueue(sameTimeQueue, queue->requests[i]);
|
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
|
// In first come first the time the request comes in is irrelevant
|
||||||
void fcfs(int start) {
|
void fcfs(int start) {
|
||||||
int position = 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);
|
DiskRequest request = dequeue(queue);
|
||||||
while (seekTime < request.time) { seekTime++; }
|
while (seekTime < request.time) { seekTime++; }
|
||||||
seekTime += timeToProcessRequest(position, request.position);
|
seekTime += timeToProcessRequest(position, request.position);
|
||||||
@ -121,31 +165,35 @@ void fcfs(int start) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cscan(int start) {
|
void cscan(int start) {
|
||||||
int currentTime = 0;
|
|
||||||
int position = start;
|
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));
|
DiskQueue *workingQueue = clone(queue);
|
||||||
for (; i < sameTimeQueue->size; i++) {
|
|
||||||
DiskRequest request = dequeue(sameTimeQueue);
|
|
||||||
|
|
||||||
// Need to return to 0 because we have reached the end of the disk on the current pass
|
while (workingQueue->size > 0) {
|
||||||
if (request.position < position) {
|
DiskQueue *candidates = queueOfLesserTime(workingQueue, seekTime);
|
||||||
seekTime += timeToProcessRequest(position, 9999);
|
|
||||||
movement += abs(position - 9999);
|
if (candidates->size == 0) {
|
||||||
position = 9999;
|
while (seekTime < peek(workingQueue).time) { seekTime++; }
|
||||||
seekTime += timeToProcessRequest(position, 0);
|
continue;
|
||||||
movement += abs(position - 0);
|
|
||||||
position = 0;
|
|
||||||
}
|
|
||||||
seekTime += timeToProcessRequest(position, request.position);
|
|
||||||
movement += abs(position - request.position);
|
|
||||||
position = request.position;
|
|
||||||
}
|
}
|
||||||
//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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user