//Class Project. EE465 Probabilistic Methods in Computer Systems Modeling [Fall 2004] //File: slottedAloha.c //Simulation of the Slotted ALOHA System //Simulation Created by: Nithin Dinker Kamath //nkamath@usc.edu //--- //Files Required: arrivals.dat and transmit.dat #include "stdio.h" #include "stdlib.h" #include "math.h" #include "time.h" //Function to the factorial of a number int factorial(int number) { int i = 1; int factorial = 1; if(number = 0) { return (1); } else { while (i < number) { factorial = factorial * number; } return (factorial); } } //Function to generate random number between 0 and 0.99999 float randomProbability() { float returnValue; returnValue = ((float) rand()/RAND_MAX); return(returnValue); } //Function which, randomly returns 1 or 0 int packet(float probability) { if (randomProbability() <= probability) { return(1); } else { return(0); } } //Function to generate Poission arrivals int poisson(double alpha, float p) { int i; double a[1000]; double f = -1*alpha; float total = 0.0; for (i=0;i<1000;i++) { a[i] = 0; } for(i=0;i<1000;i++) { a[i] =(((pow(alpha,i))/factorial((long)i))*exp(f)); total = total + a[i]; if (randomProbability() < (float) total) { return(i); break; } } } //Main function int main(int argc, char *argv[]) { int choice; int i, j, k; int timeSlot; int state = 0; int arrivals = 0, transmissions = 0; int transmissionsAttempted = 0; int S = 0 , P = 0, G = 0; int slot = 0; //User Interface printf("***** Simulation of the Slotted Aloha System ***** \n"); printf("Enter 1 to simulate the system by reading form the arrival and transmission data files \n"); printf("Enter 2 to simulate the system using poission arrivals \n"); printf("Enter 3 to simulate the system using bernoulli arrivals \n"); printf("Enter 4 to simulate the system using general arrivals \n"); printf("Enter 0 to exit \n"); choice = getchar() - 48; printf("Option selected: %i \n", choice); //Data files if (choice == 0) { printf("\nSimulation Exited \n"); return (0); } FILE *arrival; FILE *transmission; FILE *myLog; int buffer; int junk; arrival = fopen("arrivals.dat", "r"); transmission = fopen("transmit.dat", "r"); if (arrival == NULL || transmission == NULL) { printf("arrival.dat and/or transmit.dat file not found\n"); return (0); } myLog = fopen("log.dat", "w"); if (choice == 1) { while (state < 100) { if(feof(arrival) != 0 || feof(arrival) != 0) { break; } if (state == 0) { S = 0; P = 0; G = 0; } printf("\n%i ", state); fprintf(myLog, "%i ", state); arrivals = fgetc(arrival) - 48; junk = fgetc(arrival); printf(" %i ", arrivals); fprintf(myLog, " %i ", arrivals); for (j = 0; j < state; j++) { buffer = fgetc(transmission) - 48; transmissionsAttempted = transmissionsAttempted + buffer; junk = fgetc(transmission); } state = state + arrivals; for (k = 0; k < arrivals; k++) { transmissionsAttempted = transmissionsAttempted + 1; } G = G + transmissionsAttempted; if (transmissionsAttempted == 1 && arrivals <= 1) { state = state - 1; P = P + 1; } printf(" %i ", transmissionsAttempted); fprintf(myLog, " %i\n", transmissionsAttempted); S = S + 1; slot = slot + 1; transmissionsAttempted = 0; } fclose(arrival); fclose(transmission); if (myLog != NULL) { printf("\nLog file written \n"); } fclose(myLog); printf("\n\nlambda = %f", (float) P/S); printf("\ngamma = %f", (float) G/S); printf("\nE[N] = %f", (float) state/S); printf("\nE[T] = %f", (float) state/P); printf("\nE[S] = %f", (float) S/100); printf("\n"); } //Poission Arrivals else if (choice == 2) { state = 0; int slot; float arrivalProbability = 0, transmissionProbability = 0; double alpha = 1.15; float p = 0.03; while (state < 100) { transmissionProbability = randomProbability(); if (state == 0) { S = 0; P = 0; G = 0; } printf("\n%i ", state); arrivals = poisson(alpha, p); printf(" %i ", arrivals); for (j = 0; j < state; j++) { transmissionsAttempted = transmissionsAttempted + packet(transmissionProbability); } state = state + arrivals; for (k = 0; k < arrivals; k++) { transmissionsAttempted = transmissionsAttempted + 1; } if (transmissionsAttempted == 1 && arrivals <= 1) { state = state - 1; P = P +1; } S = S + 1; printf(" %i ", transmissionsAttempted); transmissionsAttempted = 0; } printf("\n\nlambda = %f", (float) P/S); printf("\ngamma = %f", (float) G/S); printf("\nE[N] = %f", (float) state/S); printf("\nE[T] = %f", (float) state/P); printf("\nE[S] = %f", (float) S/100); printf("\n"); } //Bernouilli Arrivals else if (choice == 3) { state = 2; float arrivalProbability = 0, transmissionProbability = 0; printf("\nEnter the initial state \n"); scanf("%d", &state); getchar(); if (state == 0) { printf("\nThis simulation will enter infinite loop. Simulation Exited\n"); return (0); } while (state < 100) { arrivalProbability = randomProbability(); transmissionProbability = randomProbability(); if (state == 0) { S = 0; P = 0; G = 0; } printf("\n%i ", state); arrivals = packet(arrivalProbability); printf(" %i ", arrivals); for (j = 0; j < state; j++) { transmissionsAttempted = transmissionsAttempted + packet(transmissionProbability); } state = state + arrivals; for (k = 0; k < arrivals; k++) { transmissionsAttempted = transmissionsAttempted + 1; } if (transmissionsAttempted == 1 && arrivals <= 1) { state = state - 1; P++; } printf(" %i ", transmissionsAttempted); S = S + 1; slot = slot + 1; transmissionsAttempted = 0; } fclose(arrival); fclose(transmission); printf("\n\nlambda = %f", (float) P/S); printf("\ngamma = %f", (float) G/S); printf("\nE[N] = %f", (float) state/S); printf("\nE[T] = %f", (float) state/P); printf("\nE[S] = %f", (float) S/100); printf("\n"); } //General arrivals else if (choice == 4) { state = 0; float arrivalProbability = 0, transmissionProbability = 0; printf("\nEnter the initial state \n"); scanf("%d", &state); getchar(); for (i = 0; i < 100; i++) { arrivalProbability = (float) (1/pow(2, i)); printf("Probability : %f", arrivalProbability); transmissionProbability = 0.4; if (state == 0) { S = 0; P = 0; G = 0; } printf("\n%i ", state); arrivals = packet(arrivalProbability); printf(" %i ", arrivals); for (j = 0; j < state; j++) { transmissionsAttempted = transmissionsAttempted + packet(transmissionProbability); } state = state + arrivals; for (k = 0; k < arrivals; k++) { transmissionsAttempted = transmissionsAttempted + 1; } if (transmissionsAttempted == 1 && arrivals <= 1) { state = state - 1; P = P + 1; } printf(" %i ", transmissionsAttempted); S = S + 1; slot = slot + 1; transmissionsAttempted = 0; } printf("\n\nlambda = %f", (float) P/S); printf("\ngamma = %f", (float) G/S); printf("\nE[N] = %f", (float) state/S); printf("\nE[T] = %f", (float) state/P); printf("\nE[S] = %f", (float) S/100); printf("\n"); } else { printf("Wrong choice: Simulation Exited \n"); } return 0; }