Fix time calculation
This commit is contained in:
@ -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;
|
||||
|
Reference in New Issue
Block a user