#include #include #include #include #include "DiskSimulate.h" #include "Strategy.h" const int NUM_CYLS = 1048; bool verbose = 0; void runSimulation(int rand_seed, int num_procs, Disk *disk, DiskStrategy *strategy); void printVerboseHeader(RequestQueue *q, Disk *disk); void printVerboseLine(RequestQueue *q, Disk *disk, int which); int main(int argc, char **argv) { // parse the command line into num_arms and num_procs int num_arms, num_procs; char *prog_name = argv[0]; char *endp; if(argc > 1 && strcmp(argv[1], "-v") == 0) { verbose = 1; ++argv; --argc; } if(argc != 3) { cerr << "usage: " << prog_name << " [-v] num_arms num_procs" << endl; exit(-1); } num_arms = (int) strtol(argv[1], &endp, 10); if(endp != NULL && endp[0] != '\0') { cerr << "error: num_arms argument not a valid number" << endl; exit(-1); } num_procs = (int) strtol(argv[2], &endp, 10); if(endp != NULL && endp[0] != '\0') { cerr << "error: num_procs argument not a valid number" << endl; exit(-1); } // run the simulations Disk *disk = new Disk(num_arms, NUM_CYLS); int rand_seed = time(NULL); // we use the same seed for the randomizer across the // strategies, so that all use the same process request sequence // for a fairer comparison. runSimulation(rand_seed, num_procs, disk, new FIFO(disk)); // runSimulation(rand_seed, num_procs, disk, new SSTF(disk)); // runSimulation(rand_seed, num_procs, disk, new CLOOK(disk)); // runSimulation(rand_seed, num_procs, disk, new Elevator(disk)); return 0; } void runSimulation(int rand_seed, int num_procs, Disk *disk, DiskStrategy *strategy) { // initialize simulation data disk->reset(); srandom(rand_seed); RequestQueue *q = new RequestQueue(num_procs, NUM_CYLS); // run the simulation if(verbose) printVerboseHeader(q, disk); while(q->hasRequestsPending()) { DiskArm *arm = disk->getReadyArm(q->getTimeElapsed()); // which arm is ready? int which = strategy->selectProcess(arm, q); // which request is served? if(verbose) printVerboseLine(q, disk, which); q->serveRequest(arm, which); // serve request } // print the data static bool header_printed = false; if(!header_printed) { header_printed = true; printf(" time mean num num\n"); printf(" elapsed sq wait arm pro strategy\n"); } printf("%10.2f%11.2f%3d%4d %s\n", q->getTimeElapsed(), q->getMeanSquareWait(), disk->getNumArms(), num_procs, strategy->getName()); } void printVerboseHeader(RequestQueue *q, Disk *disk) { printf(" time "); for(int i = 0; i < q->getNumProcesses(); i++) { printf(" p%-3d ", i); } for(int i = 0; i < disk->getNumArms(); i++) { printf(" arm %-2d ", i); } printf("\n"); } void printVerboseLine(RequestQueue *q, Disk *disk, int which) { DiskArm *arm = disk->getReadyArm(q->getTimeElapsed()); printf("%6.1f", q->getTimeElapsed()); for(int i = 0; i < q->getNumProcesses(); i++) { int req = q->getProcess(i)->getCurRequest(); if(req < 0) printf(" "); else printf("%4d", req); printf(i == which ? "X " : " "); } for(int i = 0; i < disk->getNumArms(); i++) { if(disk->getArm(i) == arm) { printf("%4d->%-4d", disk->getArm(i)->getCurCylinder(), q->getProcess(which)->getCurRequest()); } else { printf(" "); } } printf("\n"); }