示例#1
0
/******************************* queueUp ***************************************
void queueUp(Simulation sim, Widget *pWidget, Queue queue)
Purpose:
	Handles the queueUp event for a given widget.
	In this case, create a new queue element, insert the current widget 
	and queueing time in said element, and insert the element in the given 
	queue.
	This function also increment the widget count of the queue and prints 
	the event if verbose mode was activated.
Parameters:
	I	Simulation sim		Simulation running the event.
	I	Widget *pWidget		Current widget.
	O	Queue queue			Queue to insert the widget in.
Returns:
	queue parm - the queue the widget was inserted in.
Notes:
*******************************************************************************/
void queueUp(Simulation sim, Widget *pWidget, Queue queue)
{
	//populate a QElement variable with the current widget and the current time
	QElement element;
	element.widget = *pWidget;
	element.iEnterQTime = sim->iClock;
	//insert the element in the queue
	insertQ(queue, element);
	//increment the queue count
	queue->lQueueWidgetTotalCount++;
	
	printEvent(sim, " %6d %8ld %s %s", sim->iClock, pWidget->lWidgetNr, "Enter", 
				queue->szQName);
}
示例#2
0
/*************************** queueUp **********************************
 queueUp(Simulation sim, Queue queueTeller, Widget *widget)
 Purpose:
     This function inserts a widget into a queue based on 
     the queueTeller.
 Parameters:
     I  Simulation sim          // Simulation we are running on
     I  Queue queueTeller       // Current queue we are dealing with
                                //      - Queue 1 or Queue 2 
     I  Widget *widget          // The current widget we are processing
 Returns:
 Notes:
     1. Updates statistics about the queue
        a. iEnterQTime
        b. lQueueWaitSum
        c. lQueueWidgetTotalCount
***********************************************************************/
void queueUp(Simulation sim, Queue queueTeller, Widget *widget) 
{
    QElement qElement;          // newly created node to store into queue
    
    // update node with widget information
    qElement.widget= *widget;
    qElement.iEnterQTime = sim->iClock;
   
    // inserts the newly created node into the queue
    insertQ(queueTeller, qElement);

    if(sim->bVerbose == TRUE)
        printf("%4d %6ld Enter %7s\n", sim->iClock, widget->lWidgetNr, queueTeller->szQName);
}
void roundRobin(int map[N][5],int proc_count,char filename[])
{
	QUEUE q;
	int cur_time=0;
	int new_proc[N];
	int i,k,quantum_used,p,temp,c,cpu_util=0;
	int blocked[N]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
	int turn_around[N];
	int proc_added=0;
	q.rear=q.front=-1;
	
	FILE *fp=fopen(filename,"w");
	fclose(fp);
	
	while(1)
	{
		int process_state[N]={0};		
		k=0;
		for(i=0;i<proc_count;i++)
		{
			if(cur_time==map[i][3])
			{
				new_proc[k++]=i;
				proc_added++;
			}
		}
		
		for(i=0;i<proc_count;i++)
		{
			if(blocked[i]==cur_time)
			{
				new_proc[k++]=i;
				blocked[i]=-1;
			}
		}
		
		sort_proc(new_proc,k);
		
		for(i=0;i<k;i++)
			insertQ(&q,new_proc[i]);
		
		if(q.rear!=q.front)
		{
			p=peekFront(q);
			process_state[p]=1;
			cpu_util++;
			quantum_used=(quantum_used+1)%2;
			map[p][4]++;
			
			if(map[p][4]==map[p][1])
			{
				removeQ(&q);
				quantum_used=0;
				turn_around[p]=cur_time;
			}
			if(quantum_used==0 && map[p][4]!=map[p][1] && map[p][4]!=(map[p][1])/2)
			{
				p=removeQ(&q);
				quantum_used=0;
				insertQ(&q,p);
			}
			
			if(map[p][4]==(map[p][1])/2)
			{
				p=removeQ(&q);
				quantum_used=0;
				blocked[p]=map[p][2]+cur_time+1;
			}
		}
			
		temp=q.front;
		while(temp!=q.rear)
		{
			temp=(temp+1)%N;
			if(q.arr[temp]!=p)
				process_state[q.arr[temp]]=2;
		}
		for(i=0;i<proc_count;i++)	
		{
			if(blocked[i]!=-1 && i!=p)
			{
				process_state[i]=3;
			}
		}	
		
		printProcessStateToFile(filename,process_state,proc_count,cur_time);
		
		c=checkBlocked(blocked,proc_count);
			
		if(q.front==q.rear && c && proc_added==proc_count)
			break;
		
		cur_time++;
		p=-1;
	}
	fp=fopen(filename,"a");
	fprintf(fp,"\n");
	fprintf(fp,"Finishing Time : %d\n",cur_time);
	fprintf(fp,"CPU utilization : %3.2f\n",(float)cpu_util/(cur_time+1));
	for(i=0;i<proc_count;i++)
	{
		temp=turn_around[i]-map[i][3]+1;
		fprintf(fp,"Turnaround process %d : %d\n",i,temp);
	}			
	fclose(fp);
}
示例#4
0
/*
Running time of algorithm
=================================================================
struct digraph
- int *inDegree; - 4 bytes * n    
- int nVertices; - 4 bytes
- ListEDGE *L;   - 4 bytes * n

  ListEDGE
  ----------------------------------------------------------
	nodeEdgeType *head; (data, next)  8 bytes *  n
	int size;  (outdegree)            4 bytes * (1 to n)
Total
===============================================================
n+m = 8n + 4 + + 8n + 4(n-1) = 20n = O(n) 

Matrix = O(n^2)

Therefore, this graph representation is much better for sparse digraph
- A single link list will speed up runtime at the expense of writing more difficult functions
- nEdge s is not within the struct will save 4 bytes in exchange for a longer calculation in the indegree function
- 
*/
int topological_sort( DiGraph DG )
{
   int Lcount = 0;
   vertex x, tempResult;
   Tqueue *Q;
   
   Q = (Tqueue*) malloc(sizeof(Tqueue));
   if (Q == NULL || DG == NULL)
   {
        return -1;
   }
   else
   {
       vertex D[DG -> nVertices]; //array of integers indexed by the vertices - in degree array
       vertex L[DG -> nVertices]; //sorted link list of vertices
       /* Compute the in-degrees D[x] of the vertices in DG */
       for (x = 0; x < DG -> nVertices; x++) //for vertices
       {
          D[x] = 0; //initialize D
          L[x] = -1; //initialize L
       }
       
       for (x = 0; x < DG -> nVertices; x++) //for vertices
       { 
          nodeEdgeType *temp = DG -> L[x].head;
          while (temp) //while there is a successor
          {
              ++D[temp -> data];
              temp = temp -> next;
          }
       }
       
       /* Initialize queue Q */
       initialize(Q);
       
       for (x = 0; x < DG -> nVertices; x++) //for vertices   
       { 
          //enqueue any vertices with indegree 0
          if ( D[x]==0 ) // insert x into Q
          {
              tempResult = insertQ(x, Q); 
          }
       }      
       
       while (isEmpty(Q) != 1) // while Q not empty
       {
          tempResult = removeQ(Q, &x);//dequeue x from Q (top)
          L[Lcount] = x;//insert x to list
          ++Lcount;
          int w;
          for (w = 0; w < DG -> L[x].size; w++) //for each successor DG -> L[x].size
          {
              --D[w]; //Indegree of vertex w
              
              if ( D[w]== 0 ) //enqueue w into Q
              {
                 tempResult = insertQ(w, Q);       
              }
          }
       }        
       //check if all the vertices got transfered to the list (no cycles?)
       //if (Lcount == DG -> nVertices)
          printf("topological order ->");   
          int p;
          for (p = 0; p < Lcount; p++)
          {
              printf("[%i], ", L[p]);   
          }
          printf("THE SIZE IS %i\n", Lcount);
       
       
       if (Lcount== DG -> nVertices && Lcount != 0)
       {  
          return 1;
       }
       else //cyclic
       {
          return 0;
       }
   }
}