/* ------ main1.c: M/M/1 queueing test program ------ */ #include #include #include double ARRIVALRATE; /* Avg # clients arriving per time unit */ double SERVICERATE; /* Avg # clients served per server per time unit */ 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); ReActivate(Server, ARRIVAL, NULL); Passivate(NULL, NULL); /* 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) { if (MyQLength() == 0) { Passivate(NULL, NULL); } Dequeue(HEAD, &Client, NULL, NULL, NULL); Hold( RateExponential(SERVICERATE, 1) ); ReActivate(Client, DONE, NULL); } } int main(int argc, char **argv) { double SimLength; printf("Length of simulation run = "); scanf("%lf", &SimLength); printf("Client arrival rate = "); scanf("%lf", &ARRIVALRATE); printf("Service rate = "); scanf("%lf", &SERVICERATE); printf("%lf %lf\n", ARRIVALRATE, SERVICERATE); 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(); }