|
|
|
|
typedef struct PCB { char Name[12]; /* Name to id Prosim Entity (for debugging) */ long *Param; /* Parameter */ int State; /* State of the process can be: (PASSIVE, WAITING or ACTIVE) */ struct QElem *Queue; /* Queue of this Prosim entity */ double ArrivalTime; /* Time of arrival */ double EventTime; /* Next event time */ long Mem[MAXWSSIZE]; /* Stack space for process */ ..... } PCBType; |
Notes:
|
void InitProsim(unsigned int MaxNProcess, unsigned int MaxNQElems, unsigned int MaxNTimeHeap); |
|
InitProsim( 1000, 1000, 1000 ); |
PCBType * Generate(char *name, void (*ProcName)(), void *param); |
|
/* -------------------------------------------------- Function that is executed by a Prosim co-routine -------------------------------------------------- */ void Proc1() { while (1) { Hold(1.2); printf("Proc1: Now = %lf\n", Now); } } int main(int argc, char **argv) { .... Generate("Name:Proc1", Proc1, 0); .... } |
void Terminate(); |
|
Terminate( ); |
void Hold(double time); |
|
double serviceTime = ....; Hold( serviceTime ); // Sim. serving a client for given service time ! |
void Passivate(0, 0); |
|
Passivate( 0,0 ); |
void ReActivate(PCBType *PCBPtr, 0, 0); |
|
PCBType * client; .... Dequeue(HEAD, &client, 0, 0, 0); // Get next client from head of queue Hold( serviceTime ); // Serve client ReActivate( client, 0, 0 ); // Resume client (so it can do calculations...) |
void Enqueue(int pos, PCBType *ServerPCBPtr, 0, 0, 0); |
|
PCBType *Server; void Client() { .... Enqueue( HEAD, Server, 0, 0, 0 ); /* Enter server's queue */ ReActivate(Server, 0, 0); /* ReActivate the server */ Passivate(0, 0); /* Wait until server finishes serving me */ } |
void Dnqueue(int pos, PCBType **ClientPCBPtr, 0, 0, 0); |
|
void Server() { PCBType *Client; .... while (1) { if ( my queue is empty ) { Passivate(0, 0); } /* ---------------------------------------------- There is a client in my queue (Server MAY have been ReActivated by a client) ---------------------------------------------- */ Dequeue( HEAD, &Client, 0, 0, 0 ); /* Enter server's queue */ Hold(serviceTime); /* Provide service to client */ ReActivate(Server, 0, 0); /* ReActivate the server */ } |
/* ------------------------------------------------------- Client generator process ------------------------------------------------------- */ void GeneratorProc() { while(1) { Hold( ARRIVAL_DELAY ); // Wait Inter-arrval time Generate("Cli", ClientProc, 0); // Create a client process... // The client process will do furthero processing for itself } } |
/* ------------------------------------------------------- Client() ------------------------------------------------------- */ void ClientProc() { /* ---------------------- Enter server's queue ---------------------- */ Enqueue(TAIL, Server, 0, 0, 0); /* ---------------------- ReActivate the server (it may be Passivated due to empty queue) ---------------------- */ ReActivate(Server, 0, 0); /* ------------------------------------------ Wait for server to complete serving me... ------------------------------------------ */ Passivate(0, 0); /* --------------------------------------- Print delay experienced by the client --------------------------------------- */ printf("Now = %lf: Client is done... my delay = %lf\n", GetSimTime(), GetSimTime() - GetMyArrivalTime() ); /* ------------------------------------------ De-allocate Prosim resources (PCB) ------------------------------------------ */ Terminate(); } |
/* ------------------------------------------------------- Server() ------------------------------------------------------- */ void ServerProc() { PCBType *Client; while (1) { if (MyQLength() == 0) { Passivate(0, 0); // Wait for a client to arrive } /* ----------------------------------------------------- We execution reaches/resumes here, we know there is a client in the server's queue ! ----------------------------------------------------- */ Dequeue(HEAD, &Client, NULL, NULL, NULL); // Get client at head of queue Hold( SERVICE_DELAY ); // "Service" the client (it only takes time :-)) ReActivate(Client, 0, 0); // Make client resume (further processing)... } } |
int main(int argc, char **argv) { double SimLength; .... /* ----------------------------------------- Allocate space for 1000 Prosim processes ----------------------------------------- */ InitProsim(1000, 1000, 1000); /* ----------------------------------------- Create the "client generator" process (now you have 999 PCB's left) ----------------------------------------- */ Generator = Generate("Gen", GeneratorProc, 0); /* ----------------------------------------- Create one "server" process (now you have 998 PCB's left) ----------------------------------------- */ Server = Generate("Serv", ServerProc, 0); /* ---------------------------------------------- Run simulation for "SimLength" virtual seconds ---------------------------------------------- */ StartSimulate(SimLength); printf("Done.\n"); } |
Compile with:
cc -g -O -o main2 -I /home/cs558000/lib main2.c \ -L /home/cs558000/lib -lProSim4 -lOnAbort -lm |
#include <stdlib.h> void srandom(unsigned int seed); /* Set the seed for the RN generator */ long random(); /* Get a random number between (0 .. 2^31-1) */ |
long I_Uniform(int a, int b, int stream); /* Integer Unif[a..b] */ double D_Uniform(double a, double b, int stream); /* Unif[a..b] */ |
Note:
|
|
The exponential distributed number generator in Prosim:
double RateExponential(double lambda, int stream); /* Exponential dist with rate */ |
/* ------------------------------------------------------- Client generator process ------------------------------------------------------- */ void GeneratorProc() { while(1) { /* -------------------------------- Wait an expon. dist. random time -------------------------------- */ Hold( RateExponential(ARRIVALRATE, 0) ); // ***** Inter-arrval time is expon. distr. !!! Generate("Cli", ClientProc, 0); // Create a client process... // The client process will do further processing for itself } } |
/* ------------------------------------------------------- Client() ------------------------------------------------------- */ void ClientProc() { /* ---------------------- Enter server's queue ---------------------- */ Enqueue(TAIL, Server, 0, 0, 0); /* ---------------------- ReActivate the server (it may be Passivated due to empty queue) ---------------------- */ ReActivate(Server, 0, 0); /* ------------------------------------------ Wait for server to complete serving me... ------------------------------------------ */ Passivate(0, 0); /* --------------------------------------- Print delay experienced by the client --------------------------------------- */ printf("Now = %lf: Client is done... my delay = %lf\n", GetSimTime(), GetSimTime() - GetMyArrivalTime() ); /* ------------------------------------------ De-allocate Prosim resources (PCB) ------------------------------------------ */ Terminate(); } |
/* ------------------------------------------------------- Server() ------------------------------------------------------- */ void ServerProc() { PCBType *Client; while (1) { if (MyQLength() == 0) { Passivate(0, 0); // Wait for a client to arrive } /* ----------------------------------------------------- We execution reaches/resumes here, we know there is a client in the server's queue ! ----------------------------------------------------- */ Dequeue(HEAD, &Client, NULL, NULL, NULL); // Get client at head of queue Hold( RateExponential(SERVICERATE, 1) ); // ***** Expon. distr. departure rate ! ReActivate(Client, 0, 0); // Make client resume (further processing)... } } |
int main(int argc, char **argv) { double SimLength; .... /* ----------------------------------------- Allocate space for 1000 Prosim processes ----------------------------------------- */ InitProsim(1000, 1000, 1000); /* ----------------------------------------- Create the "client generator" process (now you have 999 PCB's left) ----------------------------------------- */ Generator = Generate("Gen", GeneratorProc, 0); /* ----------------------------------------- Create one "server" process (now you have 998 PCB's left) ----------------------------------------- */ Server = Generate("Serv", ServerProc, 0); /* ---------------------------------------------- Run simulation for "SimLength" virtual seconds ---------------------------------------------- */ StartSimulate(SimLength); printf("Done.\n"); } |
Compile with:
cc -g -O -o main3 -I /home/cs558000/lib main3.c \ -L /home/cs558000/lib -lProSim4 -lOnAbort -lm |