CS3413/Assignment8/code/main.c

96 lines
2.7 KiB
C
Raw Permalink Normal View History

2023-11-28 13:19:22 -04:00
#include <stdio.h>
#include <stdlib.h>
2023-11-28 20:33:23 -04:00
#include "disk.h"
2023-11-28 13:19:22 -04:00
// 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) {
2023-11-28 17:21:49 -04:00
//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)
2023-11-28 13:19:22 -04:00
double time = 0;
2023-11-28 17:21:49 -04:00
// 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) {
2023-11-28 13:19:22 -04:00
time += 15;
currentDirection = direction;
}
return time;
}
2023-12-02 21:31:15 -04:00
int diskMovement(int position, int destination) {
int distance = destination - position;
return abs(distance);
}
2023-11-28 13:19:22 -04:00
// 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;
2023-11-28 21:02:49 -04:00
while (queue->size > 0) {
2023-12-02 21:55:24 -04:00
DiskRequest *request = dequeue(queue);
while (seekTime < request->time) { seekTime++; }
seekTime += timeToProcessRequest(position, request->position);
movement += diskMovement(position, request->position);
position = request->position;
free(request);
2023-11-28 13:19:22 -04:00
}
}
void cscan(int start) {
int position = start;
2023-11-28 20:12:47 -04:00
2023-11-28 21:02:49 -04:00
while (queue->size > 0) {
DiskRequest *request = findClosest(queue, position, seekTime);
if (request == NULL) {
seekTime++;
} else {
if (request->position < position) {
2023-12-02 21:55:24 -04:00
// + 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);
2023-11-28 21:02:49 -04:00
position = 0;
2023-12-02 21:31:15 -04:00
// Rescan for closest, so we can efficiently handle requests
request = findClosest(queue, position, seekTime);
2023-11-28 21:02:49 -04:00
}
seekTime += timeToProcessRequest(position, request->position);
2023-12-02 21:31:15 -04:00
movement += diskMovement(position, request->position);
2023-11-28 21:02:49 -04:00
position = request->position;
2023-11-28 20:12:47 -04:00
}
2023-12-02 21:31:15 -04:00
if (request != NULL) {
delete(queue, *request);
}
2023-11-28 13:19:22 -04:00
}
}
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))) {
2023-11-28 21:02:49 -04:00
enqueue(queue, time, position);
2023-11-28 13:19:22 -04:00
}
if (algorithm == 'F') {
fcfs(start);
2023-11-28 17:21:49 -04:00
printf("Movement:%i Time:%.1lf\n", movement, seekTime);
2023-11-28 13:19:22 -04:00
} else if (algorithm == 'C') {
cscan(start);
2023-11-28 17:21:49 -04:00
// Stupid printf difference to pass tests, could call once after if statement
printf("Movement: %i Time:%.1lf\n", movement, seekTime);
2023-11-28 13:19:22 -04:00
}
destroyDiskQueue(queue);
return 0;
}