/* ------ hw3.c: Ski-lift simulation ------ */ #include #include #include double ARRIVALRATE; /* Avg # clients arriving per time unit */ double myP; /* Inter chait time */ double myT; /* Travel up-mountain time */ PCBType *Server; /* The server thread */ PCBType *Generator; /* The thread that create client threads */ /* Variables to accumulate statistical data */ double AccDelay; /* Cumulative delay of all clients served */ long NCust; /* Number of clients served */ /* Avg delay = AccDelay/NCust */ /* --------------------------- Error handling routine --------------------------- */ int OnAbort() { printf("Aborting the Simulation !!!!\n"); Abort(); } /* ------------------------------------------------------- Client() ------------------------------------------------------- */ void ClientProc() { Enqueue(TAIL, Server, NULL, NULL, NULL); Passivate(NULL, NULL); /* Wait for chair */ Hold(myT); /* Travel up the mountain */ /* printf("Client...Delay = %lf\n", Now - CurrentPtr->ArrivalTime); */ AccDelay += (Now - CurrentPtr->ArrivalTime); NCust++; Terminate(); } /* ------------------------------------------------------- Thread that create clients.... (generate arrivals) ------------------------------------------------------- */ void GeneratorProc() { printf("Generator has started.\n"); while(1) { Hold( RateExponential(ARRIVALRATE, 0) ); Generate("Cli", ClientProc, 0); } } /* ------------------------------------------------------- Server() ------------------------------------------------------- */ void ServerProc() { PCBType *Client; while (1) { /* ----------------------------- Wait for a chair to come by ----------------------------- */ Hold(myP); /* --------------------- First client --------------------- */ if (MyQLength() > 0) { Dequeue(HEAD, &Client, NULL, NULL, NULL); ReActivate(Client, DONE, NULL); } /* --------------------- Second client --------------------- */ if (MyQLength() > 0) { Dequeue(HEAD, &Client, NULL, NULL, NULL); ReActivate(Client, DONE, NULL); } } } int main(int argc, char **argv) { double SimLength; ARRIVALRATE = atof(argv[1]); myT = atof(argv[2]); myP = atof(argv[3]); SimLength = atof(argv[4]); printf("%lf %lf %lf %lf\n", ARRIVALRATE, myT, myP, SimLength); AccDelay = 0.0; NCust = 0; printf("Init Prosim\n"); InitProsim(1000, 1000, 1000); printf("Activate generator\n"); Generator = Generate("Gen", GeneratorProc, 0); printf("Activate server\n"); Server = Generate("Serv", ServerProc, 0); StartSimulate(SimLength); printf("Average Delay = %lf\n", AccDelay/NCust); printf("Done.\n"); Abort(); }