⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 switching.c

📁 This is a Switch simulation... with 4 types of switches... and also we have average simulation tim
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************************//*	Programm : Switch simulation using No Queuing, Input Queuing, Output Queueing/*		   and Iterative matching Queueing/*	/*	Author   : Sarang Deshpande(CS08S026)/*		   Balamuralikrishna K(CS08M006)/*	/*	Date     : 11th March 2009./*****************************************************************************************/#include<stdio.h>#include<stdlib.h>#include <stdbool.h>#include <math.h>#define MAX 5000000							//Max simulation is 50,000(no of simulations)*100(no Of ports) times#define QLENGTH 50000							//since atmost 50000 simulations#define MAXPORT 1000int indexVec[MAXPORT];double intermediateVal1[MAX],intermediateVal2[MAX];int outSend[MAXPORT];/******************************************************************************************	Structures required *******************************************************************************************/struct pkt{	int inputPort;	int outputPort;	double startTime;	long startDelay;		long endDelay;	long delay;	int slotNo;	bool discarded;	bool stateCreated;}packet[MAX];struct queue{	long int packetSlot[QLENGTH];                        //because slot is unique	int rear;	int front;}inputQ[MAXPORT],outputQ[MAXPORT];struct linkUtil{	int activeLink[MAXPORT];	double averageLink[MAXPORT];}linkUtilization[MAXPORT];/******************************************************************************************	Queue operation's for INPUT QUEUE *******************************************************************************************/void initializeInputQueue(int portNo){	inputQ[portNo].rear = 0;	inputQ[portNo].front = 0;}void enInputQueue(int portNo,long int index1, int bufferLength){		if(inputQ[portNo].rear < bufferLength)	{			inputQ[ portNo ].packetSlot[ inputQ[portNo].rear ] = index1;		inputQ[ portNo ].rear++;	}	else{}		//printf("\nQueue Overflow in INPUT QUEUE\n");		}long int deInputQueue(int portNo){	if(inputQ[portNo].rear > inputQ[portNo].front )	{			//printf("\nDeleted the Item form INPUT Queue\n");		return inputQ[ portNo ].packetSlot[ inputQ[portNo].front++ ];	}		if(inputQ[portNo].rear == inputQ[portNo].front || inputQ[portNo].rear < inputQ[portNo].front)	{		//printf("\nCant delete the Item form INPUT Queue\n");		inputQ[portNo].rear = inputQ[portNo].front = 0;		return -1; 	}} /******************************************************************************************	Queue operation's for OUTPUT QUEUE *******************************************************************************************/void initializeOutputQueue(int portNo){	outputQ[portNo].rear = 0;	outputQ[portNo].front = 0;}void enOutputQueue(int portNo,long int index1, int bufferLength){		if(outputQ[portNo].rear < bufferLength)	{			outputQ[ portNo ].packetSlot[ outputQ[portNo].rear ] = index1;		outputQ[ portNo ].rear++;	}	else{}		//printf("\nQueue Overflow in OUTPUT Queue\n");		}long int deOutputQueue(int portNo){	if(outputQ[portNo].rear > outputQ[portNo].front)	{		//printf("\nDeleted the Item form OUTPUT Queue\n");		return outputQ[ portNo ].packetSlot[ outputQ[portNo].front++ ];	}		if(outputQ[portNo].rear <= outputQ[portNo].front)	{		//printf("\nCant delete the Item form OUTPUT Queue\n");		outputQ[portNo].rear = outputQ[portNo].front = 0;		return -1; 	}}/********************************************************************************************************************	sortAllPackets()- will sort all packets came on the perticular slot depending on startTime value *********************************************************************************************************************/sortAllPackets(int index, int noOfPorts){	int i,j;	double startTime[MAXPORT],tempd;	int tempi;		for(j = 0,i = (index-noOfPorts);i < index; i++,j++)	{		startTime[j]=packet[i].startTime;		indexVec[j]=i;		}	for(i = 0;i < noOfPorts;i++)	{		for(j = i;j < noOfPorts;j++)		{			if(startTime[i] > startTime[j])			{							tempd = startTime[i];				startTime[i] = startTime[j];				startTime[j] = tempd;				tempi=indexVec[i];				indexVec[i]=indexVec[j];				indexVec[j]=tempi;				}		}	}}/******************************************************************************************	nowTime()-to get the current system time which is used to calculate delay *******************************************************************************************/long nowTime() {	struct timeval tv;	//struct timezone tz=NULL;	long usec;  	gettimeofday(&tv,NULL);  	usec = tv.tv_usec;	return usec;}/******************************************************************************************	Main function. *******************************************************************************************/int main(int argc ,char *argv[]){	int i;	int noOfPorts=8;	int bufferSize=4;	int depth=2;	double packetGenProb=0.5;	char *queue="NOQ";	char *outputFile;	FILE *of=NULL;	int maxTimeSlot=10000;	int inputPort,outputPort,packetOutputport,itter,packetOut[100];	long int index1,index2,tempindex;	int k;	int i_QType;	long startTime, endTime, totalTime;	bool tempindexFrmQueue = false;	int vertexI, vertexJ, portN, retValue;	double averageDelay, stdDevDelay, averageLinkUtilization, stdDevLinkUtilization;	double tempDelay = 0 ,tempDelayStd = 0, tempLinkUtilization = 0;	double packetGenerateRandom;	double devIntermediate;	int index, count, indexcount, N1, N2;	FILE *graphFile1=NULL,*graphFile2=NULL,*graphFile3=NULL,*graphFile4=NULL,*graphFile5=NULL,*graphFile6=NULL,*graphFile7=NULL,*graphFile8=NULL;/******************************************************************************************	Opening file to write output and opening graph files. *******************************************************************************************/	of=fopen(outputFile,"a");	graphFile1=fopen("GraphFiles/NOQ-AvgpktdlyVsNoofports.graph","w");		graphFile2=fopen("GraphFiles/NOQ-SwitchutilizationVsNoofports.graph","w");	graphFile3=fopen("GraphFiles/INQ-AvgpktdlyVsNoofports.graph","w");		graphFile4=fopen("GraphFiles/INQ-SwitchutilizationVsNoofports.graph","w");	graphFile5=fopen("GraphFiles/INI-AvgpktdlyVsNoofports.graph","w");		graphFile6=fopen("GraphFiles/INI-SwitchutilizationVsNoofports.graph","w");	graphFile7=fopen("GraphFiles/OUQ-AvgpktdlyVsNoofports.graph","w");		graphFile8=fopen("GraphFiles/OUQ-SwitchutilizationVsNoofports.graph","w");	for( i=1;i<argc;i++)	{		if(strcmp(argv[i],"-N")==0)			noOfPorts=atoi(argv[i+1]);		if(strcmp(argv[i],"-B")==0)			bufferSize=atoi(argv[i+1]);		if(strcmp(argv[i],"-L")==0)			depth=atoi(argv[i+1]);		if(strcmp(argv[i],"-p")==0)			packetGenProb=atof(argv[i+1]);						if(strcmp(argv[i],"-queue")==0)			queue=argv[i+1];		if(strcmp(argv[i],"-out")==0)			outputFile=argv[i+1];		if(strcmp(argv[i],"-T")==0)			maxTimeSlot=atoi(argv[i+1]);	}			fprintf(of,"\nN 	p		 Q Type 	Avg PD 			  Std Dev of PD		Avg LU 		Std Dev of LU		Switch Util");	/******************************************************************************************	Initilizing variables for switch case *******************************************************************************************/	if(strcmp(queue,"NOQ")==0)		i_QType=0;	if(strcmp(queue,"INQ")==0)		i_QType=1;	if(strcmp(queue,"INI")==0)		i_QType=2;	if(strcmp(queue,"OUQ")==0)		i_QType=3;												/*Initalizing no of active links strcuture....*/for(inputPort = 0;inputPort < noOfPorts; inputPort++)	for(outputPort = 0;outputPort < noOfPorts; outputPort++)		linkUtilization[inputPort].activeLink[outputPort]=0;/******************************************************************************************		Logic for No Queueing - NOQ *******************************************************************************************/switch (i_QType){	case 0 :		for(noOfPorts=4;noOfPorts<101;noOfPorts++)	{		for(N1=0;N1<noOfPorts;N1++)			for(N2=0;N2<noOfPorts;N2++)			{				linkUtilization[N1].activeLink[N2]=0;				linkUtilization[N1].averageLink[N2]=0.0;			}		tempDelay=0;		tempLinkUtilization=0;		devIntermediate=0;			index1=0;		index2=0;		index=0;		for(itter = 0;itter < maxTimeSlot;itter++)			{												for(i=0;i<noOfPorts;i++)				outSend[i]=-1;				/*Packet Generation */			for(inputPort = 0;inputPort < noOfPorts; inputPort++)			{					/*Every time packet is created but its state is CREATED or NOTCREATED 				i.e. if created then process otherwise dont and depends on the probability				of the generation i.e. random value created..*/				/* IF(CREATED) packet[index1].stateCreated=true				   ELSE packet[index1].stateCreated=false*/						packetGenerateRandom = (rand()%100)*0.01;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -