/************************************************* filename: printqueue.c author: dan brown email: snowman@cs.cornell.edu date: 25 august 1997 This is the source code to CS 410 programming assignment #1. The problem is to develop a simulator for a print queue, under odd circumstances. *************************************************/ #include #include #include "queue.h" int main () { int nMinutes; /* minutes in simulation */ int nSeed; /* seed for generator */ int nCurrMin= 1; /* current minute in simulation */ Queue qAdmin; /* the two queues */ Queue qFaculty; int nAdminQueued = 0; /* how many jobs of each type queued */ int nFacultyQueued = 0; int nAdminRun = 0; /* how many of each type executed */ int nFacultyRun = 0; int nAdminWaitSum = 0; /* sum of time of each type spent in queue */ int nFacultyWaitSum = 0; double fAdminWait; /* mean wait in queue for each job type */ double fFacultyWait; printf ("How many minutes in simulation? "); scanf ("%d", &nMinutes); printf ("Seed for random number generator? "); scanf ("%d", &nSeed); srand (nSeed); QInitialize (&qAdmin); QInitialize (&qFaculty); for (nCurrMin = 1; nCurrMin <= nMinutes; ++nCurrMin) { int nCurrJobTime; /* Time that extracted job was put into the queue */ printf ("Minute %d: printed ", nCurrMin); /* extract job from queue. check admin queue first. */ if (!QEmpty(qAdmin)) { nCurrJobTime = QExtract (&qAdmin); printf ("admin job from minute #%d",nCurrJobTime); nAdminRun++; nAdminWaitSum += (nCurrMin - nCurrJobTime); } else if (!QEmpty (qFaculty)) { nCurrJobTime = QExtract (&qFaculty); printf ("faculty job from minute #%d", nCurrJobTime); nFacultyRun++; nFacultyWaitSum += (nCurrMin - nCurrJobTime); } else /* both queues are empty */ printf ("no job"); /* add new jobs to queue. */ printf (", added: "); if (rand() < (0.5 * RAND_MAX)) { printf ("admin "); nAdminQueued++; QInsert (&qAdmin, nCurrMin); } if (rand() < (0.4 * RAND_MAX)) { printf ("faculty"); nFacultyQueued++; QInsert (&qFaculty, nCurrMin); } printf ("\n"); } printf ("\nSummary:\n"); /* compute mean times in queue. avoid dividing by 0 */ if (nAdminRun > 0) fAdminWait = nAdminWaitSum / nAdminRun; else fAdminWait = 0.0; if (nFacultyRun > 0) fFacultyWait = nFacultyWaitSum / nFacultyRun; else fFacultyWait = 0.0; /* print results */ printf ("Admin jobs: %d out of %d printed. Average wait: %0.1f min\n", nAdminRun, nAdminQueued, fAdminWait); printf ("Faculty jobs: %d out of %d printed. Average wait: %0.1f min\n", nFacultyRun, nFacultyQueued, fFacultyWait); QDestroy (&qAdmin); QDestroy (&qFaculty); return 0; }