Example #1
0
int main(int argc, char * argv[]) {
	//so many variables :S
	process * waitQueue = NULL; //process waiting to be loaded
	process * memQueue = NULL; //list of processes in memory
	process * endQueue = NULL; //processes that have been laoded three times; if any
	process * workingProc = NULL; //process currently being operated on
	process * tempProc = NULL; //temp variable for swapping
	int i,k; //iterators
	int numLoads = 0; //number of loads completed
	int processes = 0; //number of process currently loaded
	int totalProcesses = 0; //cumulative process loaded across all operations
	int insertIndex = -1; //the index where the process will be put into memory
	int holes = 0; //current number of holes
	int currentHole = 0; //currently selected hole
	int mostSuitedHole = -1; //hole most suited for a process, used only by best/worst fit
	int totalHoles = 0; //total number of holes across all operations
	int memory[128]; //array representing memory
	int memUsage = 0; //amount of memory being used
	int totalMemUsage = 0; //cumulative usage across all operations
	FILE * fp; //file pointer is a file pointer

	for( k = 0; k < 4; k++ ) { //loop once for each memory management method
		//k = 0: first fit; 1: best fit; 2: next fit; 3: worst fit

		for( i = 0; i < 128; i++ ) { //memory initialisation
			memory[i] = 0;
		}
		fp = fopen( argv[1], "r" );
		if( fp == NULL ) { //teriminate if no file was entered on the command line
			printf("No file entered\n");
			return 0;
		}
		waitQueue = readFile(fp);

		if( k == 0 ) { //DEBUG
			print(waitQueue);
			printf("\n");
		} //END DEBUG

		while( waitQueue != NULL && k != 2 ) { //loop until all processes are in memory or have been completed
			workingProc = popFromQueue( &waitQueue ); //pop the next process from the ready queue
			while( insertIndex == -1 ) { //loop until a suitable hole has been found
				if( k == 0 && workingProc->size <= holeSize(memory, currentHole) ) { //first fit, simply if hole fits the process
					insertIndex = currentHole;
				} else if ( k == 1 && workingProc->size <= holeSize(memory, currentHole) ) { //best fit, will rotate throguh all of memory first
					if( mostSuitedHole == -1 ) { //first hole that meets criteria
						mostSuitedHole = currentHole;
					} else if (holeSize(memory, currentHole) < holeSize(memory, mostSuitedHole)) { //curhole is a better fit
						mostSuitedHole = currentHole;
					}
					if( currentHole == nextHole(memory, currentHole)) {
						insertIndex = mostSuitedHole;
					}
				} else if ( k == 3 && workingProc->size <= holeSize(memory, currentHole) ) { //best fit, will rotate throguh all of memory first
					if( mostSuitedHole == -1 ) { //first hole that meets criteria
						mostSuitedHole = currentHole;
					} else if (holeSize(memory, currentHole) > holeSize(memory, mostSuitedHole)) { //curhole is a better fit
						mostSuitedHole = currentHole;
					}
					if( currentHole == nextHole(memory, currentHole)) {
						insertIndex = mostSuitedHole;
					}
				}

				if( k != 2 && currentHole == nextHole(memory, currentHole) && insertIndex == -1 ) { //if no valid hole is found above
					//swap oldest process out
					tempProc = popFromQueue(&memQueue);
					if( tempProc->loaded != 3 ) { //not the third time its been swapped
						waitQueue = addToQueue(waitQueue, tempProc); //send it to the wait list
					} else {
						endQueue = addToQueue(endQueue, tempProc);
					}
					CLEARMEMORY(tempProc->start, tempProc->end); //unset its memory
					processes--;
					memUsage -= tempProc->size;
				}
				if( currentHole == nextHole(memory, currentHole) ) {
					currentHole = 0;
				} else {
					currentHole = nextHole(memory, currentHole);
				}				
			}
			if( k != 2 ) //only if not working under next fit, we reset the chosen hole everytime
				currentHole = 0;
			//a spot has been found
			
			//data tracking
			numLoads++;
			processes++;
			holes = countHoles(memory);
			totalProcesses += processes;
			totalHoles += holes;
			memUsage += workingProc->size;
			totalMemUsage += memUsage;

			//annoucement
			printf("%s loaded, #processes = %d, #holes = %d, %%memusage = %.0f, cumulative %%memusage = %.0f\n", workingProc->name, processes, holes, (float)memUsage / 128.0 * 100, \
				 ((float) totalMemUsage / (float)numLoads) / 128.0 * 100 );


			//inserting process
			workingProc->start = insertIndex;
			workingProc->end = workingProc->start + workingProc->size;
			workingProc->loaded++;
			SETMEMORY(workingProc->start, workingProc->end);
			memQueue = addToQueue( memQueue, workingProc );
			mostSuitedHole = -1;
			insertIndex = -1;
		}

		switch(k) {
			case 0: printf("\nFirst Fit \n"); break;
			case 1: printf("\nBest Fit\n"); break;
			case 2: printf("\nNext Fit - not implemented\n"); break;
			case 3: printf("\nWorst Fit\n"); break;
		}

		printf("Total loads = %d, average #processes = %.1f, average #holes = %.1f, cumulative %%memory = %.0f\n\n", numLoads, (float)totalProcesses / (float)numLoads, \
			(float)totalHoles / (float)numLoads, ((float) totalMemUsage / (float)numLoads / 128.0 * 100) );

		fclose(fp);
		destroyQueue(waitQueue);
		destroyQueue(memQueue);
		destroyQueue(endQueue);

		waitQueue = NULL;
		memQueue = NULL;
		endQueue = NULL;

		numLoads = 0;
		processes = 0;
		totalProcesses = 0; 
		insertIndex = -1; 
		holes = 0;
		currentHole = 0;
		mostSuitedHole = -1; 
		totalHoles = 0;
		memUsage = 0; 
		totalMemUsage = 0;
	}
 
	return 0;
}
Example #2
0
/**
 * Recognizes the character.
 * @return the character
 */
char Grapheme::Read()
{
	pareDown();

	theSymbol.holes = countHoles();

	// Get height/width ratio
	theSymbol.proportion = part.height() / (double) part.width();

	// Find density
	theSymbol.density.total = areaDensity(part);

	// Find density of border
	unsigned pixelCount = 0;
	unsigned foregroundCount = 0;
	for (Box::edge_iterator i(part); !i; ++i, ++pixelCount)
		if (isForeground(image(i->x, i->y)))
			++foregroundCount;
	theSymbol.density.border = foregroundCount / (double) pixelCount;

	// Find density of each quadrant
	unsigned midWidth = part.low.x + ((part.high.x - part.low.x) / 2);
	unsigned midHeight = part.low.y + ((part.high.y - part.low.y) / 2);
	theSymbol.density.q2 = areaDensity(Box(
	        part.low.x,
	        part.low.y,
	        midWidth,
	        midHeight));
	theSymbol.density.q1 = areaDensity(Box(
	        midWidth,
	        part.low.y,
	        part.high.x,
	        midHeight));
	theSymbol.density.q3 = areaDensity(Box(
	        part.low.x,
	        midHeight,
	        midWidth,
	        part.high.y));
	theSymbol.density.q4 = areaDensity(Box(
	        midWidth,
	        midHeight,
	        part.high.x,
	        part.high.y));

	// Find density of diagonals
	/*theSymbol.density.diag1 = checkLine(part.high, atan(part.height()
	 / (-part.width())));
	 theSymbol.density.diag2 = checkLine(Point(part.low.x, part.high.y), atan(
	 part.height() / part.width()));*/

	// Find density of middle points
	theSymbol.density.mid1 = areaDensity(Box(
	        midWidth,
	        part.low.y,
	        midWidth,
	        part.high.y));
	theSymbol.density.mid2 = areaDensity(Box(
	        part.low.x,
	        midHeight,
	        part.high.x,
	        midHeight));

	// Find best match
	return font.bestMatch(theSymbol);
}