Yeah, you’re right. It is a sim of naive disk IO and the assignment is for disk scheduling. Java and erlang are the only programming languages that we learn in the core modules however we have special classes for C, C++, etc. Classes such as DiskSim and all the arrays available are provided. I’d give you the whole source code but it would be pointless (imo).
Here’s the complete method:
public void readcomplete(int blk, BRequest req) {
if (blk >= 0) {
/* give the block read back to the high-level system */
DiskSim.highlevel_didread(blk, req);
}
if (readqueue_size > 0) {
int lastReadTrack = DiskSim.block_to_track(blk);
/*
* The distance is set to max_value for the first iteration to
* complete, to ensure the first blk is not the closest
*/
int distance = Integer.MAX_VALUE;
int closestDistance = 0;
for (int i = readqueue_tail; i < readqueue_head; i++) {
int val = lastReadTrack - DiskSim.block_to_track(readqueue[i].blk);
closestDistance = (val < 0) ? -val : val;
while (distance > closestDistance) {
distance = closestDistance;
int prevTailBlock = readqueue[readqueue_tail].blk;
BRequest prevTailReq = readqueue[readqueue_tail].req;
readqueue[readqueue_tail].blk = readqueue[i].blk;
readqueue[readqueue_tail].req = readqueue[i].req;
readqueue[i].blk = prevTailBlock;
readqueue[i].req = prevTailReq;
}
}
/*
* still got requests to service, dispatch the next block request
* (at tail)
*/
DiskSim.disk_readblock(readqueue[readqueue_tail].blk, readqueue[readqueue_tail].req);
/* increment tail pointer, modulo buffer size */
readqueue_tail = (readqueue_tail + 1) % DiskSim.MAXREQUESTS;
readqueue_size--;
}
}
The code I posted on thread was what I added. The assignments task is to modify readcomplete method only. What I had to do was implement a disk scheduling algo. E.g. SSTF, Scan, C-Scan or whatever I can find/create that is better. When running the application you get a type of score to see how well your implentation performed. So far I’m getting 700-1000 which is good but not A grade. I need the score to be 500-600 or less.
“If there are errors in your code that cause incorrect blocks to be read, the simulation framework will emit various errors as it runs. These generally indicate serious problems with your scheduling algorithm — i.e. it is reading or returning the wrong blocks.” So far that code I showed was my implementation of SSTF.
Regards to the Math.abs justaguy, thank you for that. Very helpful. The while loop was a meh, blame my lack of understanding.
Thanks for the comments.