diff --git a/Assignment8/.idea/runConfigurations/1.xml b/Assignment8/.idea/runConfigurations/1.xml new file mode 100644 index 0000000..a6f904c --- /dev/null +++ b/Assignment8/.idea/runConfigurations/1.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment8/.idea/runConfigurations/2.xml b/Assignment8/.idea/runConfigurations/2.xml new file mode 100644 index 0000000..ecf6679 --- /dev/null +++ b/Assignment8/.idea/runConfigurations/2.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment8/.idea/runConfigurations/3.xml b/Assignment8/.idea/runConfigurations/3.xml new file mode 100644 index 0000000..bb42c50 --- /dev/null +++ b/Assignment8/.idea/runConfigurations/3.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment8/.idea/runConfigurations/4.xml b/Assignment8/.idea/runConfigurations/4.xml new file mode 100644 index 0000000..49c00a6 --- /dev/null +++ b/Assignment8/.idea/runConfigurations/4.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment8/.idea/runConfigurations/5.xml b/Assignment8/.idea/runConfigurations/5.xml new file mode 100644 index 0000000..329ae4b --- /dev/null +++ b/Assignment8/.idea/runConfigurations/5.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment8/.idea/runConfigurations/6.xml b/Assignment8/.idea/runConfigurations/6.xml new file mode 100644 index 0000000..22cb25c --- /dev/null +++ b/Assignment8/.idea/runConfigurations/6.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment8/code/main.c b/Assignment8/code/main.c index 020aa4c..ab90fe6 100644 --- a/Assignment8/code/main.c +++ b/Assignment8/code/main.c @@ -49,7 +49,7 @@ DiskRequest dequeue(DiskQueue *queue) { } // Allocates a new queue with the requests that have the same time -DiskQueue* queueOfSameTime(DiskQueue *queue, int time) { +DiskQueue *queueOfSameTime(DiskQueue *queue, int time) { DiskQueue *sameTimeQueue = createDiskQueue(); for (int i = queue->index; i < queue->size - queue->index; i++) { if (queue->requests[i].time == time) { @@ -67,7 +67,7 @@ int compareDiskRequestsByPosition(const void *a, const void *b) { } // Sorts the queue by position, in place -DiskQueue* sortQueueByPosition(DiskQueue* queue) { +DiskQueue *sortQueueByPosition(DiskQueue *queue) { qsort(queue->requests, queue->size, sizeof(DiskRequest), compareDiskRequestsByPosition); return queue; } @@ -92,13 +92,14 @@ double seekTime = 0; DiskQueue *queue; double timeToProcessRequest(int position, int destination) { - //The time (a floating point number) required to process a request is computed by distance the head travels divided by 5, - //plus additional 15 milliseconds penalty if the direction has to change (for FCFS) + //The time (a floating point number) required to process a request is computed by distance the head travels divided by 5 + //Plus additional 15 milliseconds penalty if the direction has to change (for FCFS) double time = 0; - int distance = position - destination; - DiskDirection direction = distance > 0 ? UP : DOWN; - time += abs(distance) / 5; - if (currentDirection != direction) { + // Calculated this way so that positive means up and negative means down + int distance = destination - position; + DiskDirection direction = (distance > 0) ? UP : DOWN; + time += abs(distance) / 5.0; + if (direction != currentDirection) { time += 15; currentDirection = direction; } @@ -126,7 +127,7 @@ void cscan(int start) { while (currentTime < request.time) { currentTime++; } DiskQueue *sameTimeQueue = sortQueueByPosition(queueOfSameTime(queue, request.time)); - for (;i < sameTimeQueue->size; i++) { + 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 @@ -160,11 +161,12 @@ int main(int argc, char **argv) { } if (algorithm == 'F') { fcfs(start); + printf("Movement:%i Time:%.1lf\n", movement, seekTime); } else if (algorithm == 'C') { cscan(start); + // Stupid printf difference to pass tests, could call once after if statement + printf("Movement: %i Time:%.1lf\n", movement, seekTime); } - printf("Movement: %i Time:%.1lf\n", movement, seekTime); - destroyDiskQueue(queue); return 0; diff --git a/Assignment8/documentation/verify.md b/Assignment8/documentation/verify.md new file mode 100644 index 0000000..94674b7 --- /dev/null +++ b/Assignment8/documentation/verify.md @@ -0,0 +1,14 @@ +| Initial position | Next Position | Distance | Time From Distance | Time from Change Direction | Total Time | +|------------------|---------------|----------|--------------------|----------------------------|------------| +| 0 | 93 | 93 | 18.6 | 0 | 18.6 | +| 93 | 183 | 90 | 18 | 0 | 18 | +| 183 | 37 | 146 | 29.2 | 15 | 44.2 | +| 37 | 122 | 85 | 17 | 0 | 17 | +| 122 | 14 | 108 | 21.6 | 15 | 36.6 | +| 14 | 124 | 110 | 22 | 0 | 22 | +| 124 | 65 | 59 | 11.8 | 15 | 26.8 | +| 65 | 67 | 2 | 0.4 | 0 | 0.4 | + +| Total Distance | Total Time | +|----------------|------------| +| 693 | 183 |