#include #include #define NUM_SIM_CYCLES 1000000 // returns 1 with probability T int toss( float T ) { float foo = (float) rand(); float randmax = (float) RAND_MAX ; float bar = foo / randmax; return ( bar < T ) ? 1 : 0; } int main(){ int simCycles; int Q11; int Q12; int Q21; float prob_11 = 0.48; float prob_12; float prob_21; for ( prob_12 = 0.2; prob_12 < 0.6; ) { prob_12 += 0.01; prob_21 = prob_12; Q11 = 0; Q12 = 0; Q21 = 0; for ( simCycles = 0; simCycles < NUM_SIM_CYCLES; simCycles++ ) { // model arrivals Q11 += toss( prob_11 ); Q12 += toss( prob_12 ); Q21 += toss( prob_21 ); // model maximum matching scheduler if ( Q11 == 0 ) { // no contention for outputs Q12 = (Q12==0) ? 0 : Q12 - 1; Q21 = (Q21==0) ? 0 : Q21 - 1; } else { if ( Q12 == 0 && Q21 == 0 ) { // no contention for outputs Q11--; } else if ( Q12 > 0 && Q21 > 0 ) { // max match dictates we remove pkts from Q12 and Q21 Q12--; Q21--; } else if ( Q12 > 0 && Q21 == 0 ) { // pkts only at Q11 and Q21; randomly choose between them if ( toss(0.5) ) { Q11--; } else { Q12--; } } else { // must be that Q12 == 0 && Q21 > 0 // contention for output 1, resolve randomly if ( toss(0.5) ) { Q11--; } else { Q21--; } } } } printf("p_12=p_21 = %.2f, queues lengths %d : %d : %d\n", prob_12, Q11, Q12, Q21 ); } }