Beispiel #1
0
struct Simulation initSimulation(double arrivalRate, double serviceTime, double simTime)
{
    struct Simulation res;
    res.currTime = 0;
    res.arrivalRate = arrivalRate;
    res.serviceTime = serviceTime;
    res.timeForNextArrival = getRandTime(res.arrivalRate);
    res.timeForNextDeparture = res.timeForNextArrival + res.serviceTime;
    res.totalSimTime = simTime;
    res.buffer = initQueue();
    res.eventQueue = initQueue();
    return res;
}
Beispiel #2
0
struct Simulation initSimulation(double arrivalRate, double serviceTime, double simTime)
{

	struct Simulation sim;
	sim.currTime = 0;
	sim.arrivalRate = arrivalRate;
	sim.serviceTime = serviceTime;
	sim.timeForNextArrival = getRandTime(arrivalRate);
	sim.timeForNextDeparture = sim.timeForNextArrival + serviceTime;
	sim.totalSimTime = simTime;
	sim.buffer = initQueue();
	sim.eventQueue = initQueue();

	return sim;
}
Beispiel #3
0
double runSimulation(double arrivalRate, double serviceTime, double simTime)
{
	struct Simulation sim;
	struct Data temp;
	int e;

	sim = initSimulation(arrivalRate, serviceTime, simTime);

	while (sim.timeForNextDeparture < simTime) {
			if (sim.timeForNextArrival > sim.timeForNextDeparture && sim.buffer.currSize != 0) {
			sim.e = 1;
		}
		else {
			sim.e = 0;
		}
		e = sim.e;
		switch (e) {
/*Packet arriving*/
			case ARRIVAL:
				temp.arrivalTime = sim.timeForNextArrival;
				enqueue(&(sim.buffer), temp);
				sim.currTime = sim.timeForNextArrival;
				sim.timeForNextArrival += getRandTime(arrivalRate);
				break;
/*Packet departing*/						
			case DEPARTURE:
				temp = dequeue(&(sim.buffer));
				temp.departureTime = sim.timeForNextDeparture;
				sim.currTime = sim.timeForNextDeparture;
				if (sim.buffer.currSize != 0) {
					sim.timeForNextDeparture += serviceTime;
				}
				else {
					sim.timeForNextDeparture = sim.timeForNextArrival + serviceTime;
				}
				enqueue(&(sim.eventQueue), temp);
				break;
			}
		}
		
/*Freeing what was not departed from the queue*/
		while(sim.buffer.currSize > 0) {
			temp = dequeue(&(sim.buffer));
		}
/*Return average wait time*/
		return calcAverageWaitingTime(&sim);
		
}
Beispiel #4
0
void serviceClient() {

    p_sem_wait( &shared->ordered );
    println( "A cashier is speaking with client %d. ", client_id );

    sleep( getRandTime( service_time ) );
    p_sem_wait( &shared->orders_mutex );

    int item_id = shared->orders[ client_id ];
    println( "A cashier is placing order (item %d) for client %d.", item_id, client_id );
    p_sem_post( &shared->orders_mutex );

    // log client's order in system
    p_sem_wait( &shared->db_mutex );
    FILE *fp = fopen( DB_FILE, "ab+" );
    fprintf( fp, DB_PRINT_FORMAT, client_id, item_id, getDescription( item_id ), getPrice( item_id ) );
    fprintf( fp, "\n" );
    fclose( fp );
    p_sem_post( &shared->db_mutex );

    // on successful payment, move client to waiting area (out of cashiers queue)
    p_sem_wait( &shared->order_queue_mutex );
    enqueue( &shared->order_queue, client_id ); // order_queue must populate before server gets called or will be nil
    p_sem_post( &shared->order_queue_mutex );

    // signal client the order went through
    p_sem_post( &shared->cashier_order_placed );

    log( "A cashier is awaiting payment from client %d.", client_id );
    p_sem_wait( &shared->payment );
    log( "A cashier has received payment from client %d.", client_id );

    log( "A cashier is posting receipt for client %d.", client_id );
    p_sem_post( &shared->receipt );

    // signal to server that there is a new order to be filled
    log( "A cashier is posting client %d's new_order for server.", client_id );
    p_sem_post( &shared->new_order );

    logOrder( item_id );
}
Beispiel #5
0
double runSimulation(double arrivalRate, double serviceTime, double simTime)
{
    struct Simulation sim = initSimulation(arrivalRate, serviceTime, simTime);
    //int i = 0;
    while(sim.currTime < sim.totalSimTime)
    {
        if(sim.timeForNextArrival < sim.timeForNextDeparture)
        {
            sim.currTime = sim.timeForNextArrival;
            struct Data pktdata;
            pktdata.arrivalTime = sim.currTime;
            /* pktdata.departureTime = -1; */
            enqueue(&sim.buffer, pktdata);
            //printf("%d, arrival: %f \n", ++i, sim.currTime);
            sim.timeForNextArrival = sim.currTime + getRandTime(sim.arrivalRate);
        }
        else if(sim.buffer.currSize == 0)
        {
            sim.currTime = sim.timeForNextArrival;
            //printf("%d, skip: %f \n", i, sim.currTime);
            sim.timeForNextDeparture = sim.currTime + sim.serviceTime;
        }
        else if(sim.timeForNextArrival >= sim.timeForNextDeparture)
        {
            sim.currTime = sim.timeForNextDeparture;
            struct Data tempdata = dequeue(&sim.buffer);
            tempdata.departureTime = sim.currTime;
            enqueue(&sim.eventQueue, tempdata);
            //printf("%d, departure: %f \n", i, sim.currTime);
            if(sim.buffer.currSize == 0) sim.timeForNextDeparture = sim.timeForNextArrival + sim.serviceTime;
            else if(sim.buffer.currSize != 0) sim.timeForNextDeparture = sim.currTime + sim.serviceTime;
        }
    }
    double res = calcAverageWaitingTime(&sim);
    freeQueue(&sim.buffer);
    return res;
}
Beispiel #6
0
double runSimulation(double arrivalRate, double serviceTime, double simTime)
{


	struct Simulation sim = initSimulation(arrivalRate, serviceTime, simTime);


	while(sim.currTime < sim.totalSimTime){

	if((sim.timeForNextArrival < sim.timeForNextDeparture) || (sim.buffer.currSize == 0)){
	sim.e = ARRIVAL;
	}
	else{
	sim.e = DEPARTURE;
	}

	//printf("Curr: %lf\n",sim.currTime);
	
	if(sim.e == ARRIVAL){


		sim.currTime = sim.timeForNextArrival;
		
		struct Data d;
		d.arrivalTime = sim.currTime;

		enqueue(&(sim.buffer), d); 

		sim.timeForNextArrival = sim.currTime + getRandTime(sim.arrivalRate);

		if(sim.buffer.currSize == 1){

		sim.timeForNextDeparture = sim.currTime + serviceTime;
		//printf("ok");
		}

	}

	else if(sim.e == DEPARTURE){

		sim.currTime = sim.timeForNextDeparture;
		
		struct Data packet = dequeue(&(sim.buffer));

		//printf("Depart: %lf\n", packet.arrivalTime);
		
		packet.departureTime = sim.currTime;
		//printf("Arrival: %lf\n", packet.arrivalTime);
		//printf("Depart: %lf\n", packet.departureTime);

		sim.timeForNextDeparture = sim.currTime + serviceTime;

		enqueue(&(sim.eventQueue), packet);
	}


	}
	
	return calcAverageWaitingTime(&sim);


}