#include #include #include "disk.h" // Global tracking variables DiskDirection currentDirection = UP; int movement = 0; double seekTime = 0; // Array for storing the requests 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) double time = 0; // 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; } return time; } int diskMovement(int position, int destination) { int distance = destination - position; return abs(distance); } // Your simulated disk is of size 10000, numbered from 0 to 9999. // In first come first the time the request comes in is irrelevant void fcfs(int start) { int position = start; while (queue->size > 0) { DiskRequest *request = dequeue(queue); while (seekTime < request->time) { seekTime++; } seekTime += timeToProcessRequest(position, request->position); movement += diskMovement(position, request->position); position = request->position; free(request); } } void cscan(int start) { int position = start; while (queue->size > 0) { DiskRequest *request = findClosest(queue, position, seekTime); if (request == NULL) { seekTime++; } else { if (request->position < position) { // + 1 simulates the time it takes to move to the end of the disk (it is a circular disk) seekTime += timeToProcessRequest(position, 9999 + 1); movement += diskMovement(position, 9999 + 1); position = 0; // Rescan for closest, so we can efficiently handle requests request = findClosest(queue, position, seekTime); } seekTime += timeToProcessRequest(position, request->position); movement += diskMovement(position, request->position); position = request->position; } if (request != NULL) { delete(queue, *request); } //printf("Movement: %i Time:%.1lf\n", movement, seekTime); } } int main(int argc, char **argv) { int position, time; char algorithm = argv[1][0]; int start = 0; queue = createDiskQueue(); while (EOF != (scanf("%i %i\n", &position, &time))) { enqueue(queue, time, position); } 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); } destroyDiskQueue(queue); return 0; }