Initial implementation with bugs
This commit is contained in:
parent
131210cbab
commit
531c063830
171
Assignment8/code/main.c
Normal file
171
Assignment8/code/main.c
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Data structures and helper functions
|
||||||
|
typedef struct DiskRequest {
|
||||||
|
int position;
|
||||||
|
int time;
|
||||||
|
} DiskRequest;
|
||||||
|
|
||||||
|
typedef struct DiskQueue {
|
||||||
|
DiskRequest *requests;
|
||||||
|
int index;
|
||||||
|
int size;
|
||||||
|
} DiskQueue;
|
||||||
|
|
||||||
|
DiskQueue *createDiskQueue() {
|
||||||
|
DiskQueue *queue = malloc(sizeof(DiskQueue));
|
||||||
|
if (queue == NULL) {
|
||||||
|
printf("Error allocating memory for queue\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
queue->requests = malloc(sizeof(DiskRequest));
|
||||||
|
if (queue->requests == NULL) {
|
||||||
|
printf("Error allocating memory for queue requests\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
queue->index = 0;
|
||||||
|
queue->size = 0;
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
DiskRequest diskRequest(int position, int time) {
|
||||||
|
DiskRequest request;
|
||||||
|
request.position = position;
|
||||||
|
request.time = time;
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
void enqueue(DiskQueue *queue, DiskRequest request) {
|
||||||
|
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size + 1));
|
||||||
|
queue->requests[queue->size] = request;
|
||||||
|
queue->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
DiskRequest dequeue(DiskQueue *queue) {
|
||||||
|
DiskRequest request = queue->requests[queue->index];
|
||||||
|
queue->index++;
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocates a new queue with the requests that have the same 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) {
|
||||||
|
enqueue(sameTimeQueue, queue->requests[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sameTimeQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare function for disk requests by position
|
||||||
|
int compareDiskRequestsByPosition(const void *a, const void *b) {
|
||||||
|
DiskRequest *requestA = (DiskRequest *) a;
|
||||||
|
DiskRequest *requestB = (DiskRequest *) b;
|
||||||
|
return requestA->position - requestB->position;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sorts the queue by position, in place
|
||||||
|
DiskQueue* sortQueueByPosition(DiskQueue* queue) {
|
||||||
|
qsort(queue->requests, queue->size, sizeof(DiskRequest), compareDiskRequestsByPosition);
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void destroyDiskQueue(DiskQueue *queue) {
|
||||||
|
free(queue->requests);
|
||||||
|
free(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef enum DiskDirection {
|
||||||
|
UP, DOWN
|
||||||
|
} DiskDirection;
|
||||||
|
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
int distance = position - destination;
|
||||||
|
DiskDirection direction = distance > 0 ? UP : DOWN;
|
||||||
|
time += abs(distance) / 5;
|
||||||
|
if (currentDirection != direction) {
|
||||||
|
time += 15;
|
||||||
|
currentDirection = direction;
|
||||||
|
}
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
for (int i = 0; i < queue->size; i++) {
|
||||||
|
DiskRequest request = dequeue(queue);
|
||||||
|
seekTime += timeToProcessRequest(position, request.position);
|
||||||
|
movement += abs(position - request.position);
|
||||||
|
position = request.position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cscan(int start) {
|
||||||
|
int currentTime = 0;
|
||||||
|
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));
|
||||||
|
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
|
||||||
|
if (request.position < position) {
|
||||||
|
seekTime += timeToProcessRequest(position, 9999);
|
||||||
|
movement += abs(position - 9999);
|
||||||
|
position = 9999;
|
||||||
|
seekTime += timeToProcessRequest(position, 0);
|
||||||
|
movement += abs(position - 0);
|
||||||
|
position = 0;
|
||||||
|
}
|
||||||
|
seekTime += timeToProcessRequest(position, request.position);
|
||||||
|
movement += abs(position - request.position);
|
||||||
|
position = request.position;
|
||||||
|
}
|
||||||
|
//destroyDiskQueue(sameTimeQueue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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, diskRequest(position, time));
|
||||||
|
// printf("Delete me: position %i, Delete me: time %i\n", position, time);
|
||||||
|
}
|
||||||
|
if (algorithm == 'F') {
|
||||||
|
fcfs(start);
|
||||||
|
} else if (algorithm == 'C') {
|
||||||
|
cscan(start);
|
||||||
|
}
|
||||||
|
printf("Movement: %i Time:%.1lf\n", movement, seekTime);
|
||||||
|
|
||||||
|
destroyDiskQueue(queue);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,34 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
char currentDirection;
|
|
||||||
|
|
||||||
void fcfs(int start){
|
|
||||||
int movement = 0;
|
|
||||||
double time = 0;
|
|
||||||
printf("Movement: %i Time:%.1lf\n", movement, time);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cscan(int start){
|
|
||||||
int movement = 0;
|
|
||||||
double time = 0;
|
|
||||||
printf("Movement: %i Time:%.1lf\n", movement, time);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main (int argc, char** argv){
|
|
||||||
int position, time;
|
|
||||||
char algorithm = argv[1][0];
|
|
||||||
int start = 0;
|
|
||||||
currentDirection = 'a';
|
|
||||||
|
|
||||||
|
|
||||||
while ( EOF!=(scanf("%i %i\n",&position,&time)))
|
|
||||||
{
|
|
||||||
printf("Delete me: position %i, Delete me: time %i\n",position,time);
|
|
||||||
}
|
|
||||||
if (algorithm == 'F'){
|
|
||||||
fcfs(start);
|
|
||||||
}else if ( algorithm == 'C'){
|
|
||||||
cscan(start);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user