Exemplo n.º 1
0
void uni(FILE* ifp, FILE* ofp, FILE* rfp, int verbose, int dispRand){

	int failsafe = false;
	int failsafeT = 5;
	int test     = false;
	
	int allTerminated = false;
	int t = 0; 						// Time
	process* current;
	
	process* root = (process*) malloc(sizeof(process));
	root = parse(ifp, root);
	
	char* origInput   = (char*) malloc(sizeof(char*)); 				// Original Input
	char* sortedInput = (char*) malloc(sizeof(char) * INPUT_SIZE);	// Sorted Input

	origInput = getInput(root);
	root = sortLL(root, root->numProcesses, artm);
	sortedInput = getInput(root);

	fprintf(ofp,"The original Input was: %s\nThe (sorted) Input is:  %s\n\n", origInput, sortedInput);

	if(verbose == true)
		fprintf(ofp,"%s\n\n", "This detailed printout gives the state and remaining burst for each process");

	while(!allTerminated){
		current = root->next;
		while(current != NULL){
			// Sets state and the printInt you need to print
			setState(root, -1);

			// Verbose Section
			if(verbose == true){
				process* printCurrent = root->next;
				fprintf(ofp, "Before Cycle%5d:",  t);
				while(printCurrent != NULL){
					fprintf(ofp, "%11s  %d", printCurrent->stateString, printCurrent->printInt);
					printCurrent = printCurrent->next;
				}
				fprintf(ofp, "%s", ".\n");
			}
			
			// Sets unstarted programs to ready if t == their arrival time
			setReady(root, t);

			if(current->state == READY){
				if(test == true){
					fprintf(ofp,"%s\n", "Ready->Running");
				}
				current->state = RUNNING;
				current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);				
			}
			else if(current->state == RUNNING){
				if(test == true){
					fprintf(ofp,"%s", "Running: ");
					fprintf(ofp,"%s ", "CPUBurstTime--");
					fprintf(ofp,"%s\n", "totalCPUTime--");
				}
				current->CPUBurstTime--;
				current->totalCPUTime--;
				root->totalTimeRunning++;
				if(current->totalCPUTime == 0){
					if(test == true){
						fprintf(ofp,"%s\n", "Running->Terminated");
					}
					current->state = TERMINATED;
					current->finishingTime = t;
					current->turnaroundTime = current->finishingTime - current->arrivalTime;
				}
				else if(current->CPUBurstTime == 0){
					if(test == true)
						fprintf(ofp,"%s\n", "Running->blocked");
					current->state = BLOCKED;
					current->IOBurstTime = randomOS(current->maxIOBurstTime, rfp, ofp, dispRand, IO);
				}
			}
			else if(current->state == BLOCKED){
				if(test == true){
					fprintf(ofp,"%s", "Blocked: ");
					fprintf(ofp,"%s\n", "IOBurstTime--");
				}
				current->IOBurstTime--;
				current->timeBlocked++;
				root->totalTimeBlocked++;
				if(current->IOBurstTime == 0){
					if(test == true){
						fprintf(ofp,"%s\n", "Blocked->Running");
					}
					current->state = RUNNING;
					current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);
				}
			}
			if(current->state == TERMINATED){
				current=current->next;
				if(current != NULL){
					current->state = RUNNING;
					current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);
				}
			}

			// Calculates waiting time
			uniWaitingTime(root);

			t++;

			// Initializes failsafe
			if(failsafe == true && t > failsafeT){
				fprintf(ofp,"%s\n", "FAILSAFE HIT");
				break;
			}

		}
		// Check if all processes are terminated, failsafe for testing purposes
		if(failsafe == false){
			current = root->next;
			allTerminated = true;
			while(current != NULL){
				if(current->state != TERMINATED)
					allTerminated = false;
				current = current->next;
			}
		}
		else
			allTerminated = true;
	}

	fprintf(ofp,"%s\n", "The scheduling algorithm used was Uniprogramming");

	// Summary Data
	int i = 0;
	process* tempRoot    = (process*) malloc(sizeof(process));
	tempRoot = parse(ifp,tempRoot);
	process* tempCurrent = tempRoot->next;
	current = root->next;
	while(current != NULL){
		fprintf(ofp, "\n%s %d:\n", "Process", i);
		fprintf(ofp, "\t%s = (%d,%d,%d,%d)\n", "(A,B,C,IO)", tempCurrent->arrivalTime, tempCurrent->maxCPUBurstTime, tempCurrent->totalCPUTime, tempCurrent->maxIOBurstTime);
		fprintf(ofp, "\t%s: %d\n", "Finishing time", current->finishingTime);
		fprintf(ofp, "\t%s: %d\n", "Turnaround time", current->turnaroundTime);
		root->totalTurnaroundTime += current->turnaroundTime;
		fprintf(ofp, "\t%s: %d\n", "I/O time", current->timeBlocked);
		fprintf(ofp, "\t%s: %d\n", "Waiting Time", current->waitingTime);
		root->totalWaitingTime += current->waitingTime;
		i++;
		current = current->next;
		tempCurrent = tempCurrent->next;
	}
	fprintf(ofp, "\n%s:\n", "Summary Data");
	fprintf(ofp, "\t%s: %d\n", "Finishing time", t-1);
	fprintf(ofp, "\t%s: %f\n", "CPU Utilization", (double) root->totalTimeRunning / (double) (t-1));
	fprintf(ofp, "\t%s: %f\n", "I/O Utilization", (double) root->totalTimeBlocked / (double) (t-1));
	fprintf(ofp, "\t%s: %f processes per hundred cycles\n", "Throughput", ((double) root->numProcesses / (double) (t-1)) * 100);
	fprintf(ofp, "\t%s: %f\n", "Average turnaround time", (double) root->totalTurnaroundTime / (double) root->numProcesses);
	fprintf(ofp, "\t%s: %f\n", "Average waiting time", (double) root->totalWaitingTime / (double) root->numProcesses);
}
Exemplo n.º 2
0
void roundrobin(int interval_length, int state[], process pool[], int pool_sz)
{
	int term_count = 0;
	int p_ind;	
	int cycle = 0;
	process p;
	process * running = NULL;
	process * temp = NULL;
	//process * preempted = NULL;

	double sum_cpu = 0;
	double sum_io = 0;

	process sorted_pool[pool_sz];
	memcpy(sorted_pool, pool, sizeof(process) * pool_sz);
	qsort(sorted_pool, pool_sz, sizeof(process), process_cmp);


	queue ready = init();
	queue ties = init();
	int process_blocked = 0;
	while (term_count < pool_sz)
	{

		if (verbose) print_prev_cycle(state, sorted_pool, pool_sz, cycle);
		if (process_blocked)
		{
			process_blocked = 0;
			for (p_ind = 0; p_ind < pool_sz; p_ind ++) 
			{
				temp = &pool[p_ind];
				if (state[temp->pid] == BLOCKED)
				{
					process_blocked = 1;
					temp->io_burst = temp->io_burst - 1;
					temp->time_blocked += 1;
					if (temp->io_burst == 0)
					{
						state[temp->pid] = READY;
						temp->started_wait = cycle;
						enqueue(&ties, *temp);
					}
					sorted_pool[temp->pid] = *temp;
				}
			}

			if (process_blocked) sum_io += 1;
		}

		if (running)
		{
			if (state[running->pid] == RUNNING)
			{
				running->cpu_burst -= 1;
				running->cpu_time_left -= 1;
				sum_cpu += 1;
				sorted_pool[running->pid] = *running;
				pool[running->pid] = *running;
			}

			if (running->cpu_time_left == 0) {
				state[running->pid] = TERMINATED;
				term_count = term_count + 1;
				running->finished_at = cycle;
				running->turnaround = cycle - running->arrival_time;
				pool[running->pid] = *running;
				running = NULL;
			} 
			else if (running->cpu_burst == 0 && running->cpu_burst_remainder == 0)
			{
				running->io_burst = randomOS(running->io_burst_gen);
				state[running->pid] = BLOCKED;
				sorted_pool[running->pid] = *running;
				pool[running->pid] = *running;
				running = NULL;
				process_blocked = 1;
			}	
			else if (running->cpu_burst == 0 && running->cpu_burst_remainder != 0)
			{
				running->started_wait = cycle;
				state[running->pid] = READY;
				pool[running->pid] = *running;
				enqueue(&ties, *running);
				running = NULL;
			}					
		}

		for (p_ind = 0; p_ind < pool_sz; p_ind ++)
		{
			temp = &pool[p_ind];

			if (temp->arrival_time == cycle)
			{		
				state[temp->pid] = READY;
				temp->started_wait = cycle;
				enqueue(&ties, *temp);
			}
		}

		break_ties(&ready, &ties);

		if (!empty(&ready) && !running)
		{
			
				p = dequeue(&ready);
				running = &p;
				running->time_waited += cycle - running->started_wait;	
				state[running->pid] = RUNNING;

				if (!running->cpu_burst_remainder)
				{
					//Get a new cpu_burst
					running->cpu_burst_remainder = randomOS(running->cpu_burst_gen);
				}

				if (running->cpu_burst_remainder > interval_length)
				{
					running->cpu_burst_remainder -= 2;
					running->cpu_burst = 2;
				} 
				else
				{
					running->cpu_burst = running->cpu_burst_remainder;
					running->cpu_burst_remainder = 0;
				}

				if (running->cpu_burst > running->cpu_time_left)
				{
					running->cpu_burst_remainder = 0;
					running->cpu_burst = running->cpu_time_left;
				}
				sorted_pool[running->pid] = *running;
				pool[running->pid] = *running;			
		}
				
		cycle += 1;
	}


	temp = NULL;
	running = NULL;
	printf("The scheduling algorithm used was Round Robin, with q = 2\n\n");
	print_stats(pool, pool_sz, cycle - 1, sum_cpu, sum_io);
	clear(&ready);
}
Exemplo n.º 3
0
void uni(int state[], process pool[], process sorted_pool[], int pool_sz)
{
	int term_count = 0;
	int p_ind;	
	int cycle = 0;
	process p;
	process * running = NULL;
	process * temp = NULL;

	double sum_cpu = 0;
	double sum_io = 0;

	queue ready = init();

	for (p_ind = 0; p_ind < pool_sz; p_ind ++)
	{
		temp = &sorted_pool[p_ind];
		temp->sorted_ind = p_ind;
	}

	temp = NULL;
	while (term_count < pool_sz)
	{

		if (verbose) print_prev_cycle(state, sorted_pool, pool_sz, cycle);

		if (running)
		{

			if (state[running->pid] == BLOCKED)
			{
				running->io_burst = running->io_burst - 1;
				running->time_blocked += 1;
				sum_io += 1;
				pool[running->pid] = *running;
				sorted_pool[running->sorted_ind] = *running;
			} 
			else if (state[running->pid] == RUNNING)
			{
				running->cpu_burst -= 1;
				running->cpu_time_left -= 1;
				pool[running->pid] = *running;
				sum_cpu += 1;
				sorted_pool[running->sorted_ind] = *running;
			}

			if (running->cpu_time_left == 0) {
				state[running->pid] = TERMINATED;
				term_count = term_count + 1;
				running->finished_at = cycle;
				running->turnaround = cycle - running->arrival_time;
				pool[running->pid] = *running;
				sorted_pool[running->sorted_ind] = *running;

				running = NULL;
			} 
			else if (running->cpu_burst == 0 && state[running->pid] == RUNNING)
			{
				running->io_burst = randomOS(running->io_burst_gen);
				state[running->pid] = BLOCKED;
				pool[running->pid] = *running;
				sorted_pool[running->sorted_ind] = *running;

			} 
			else if (running->io_burst == 0 && state[running->pid] == BLOCKED)
			{
				running->cpu_burst = randomOS(running->cpu_burst_gen);
				if (running->cpu_burst > running->cpu_time_left)
				{
					running->cpu_burst = running->cpu_time_left;
				}
				state[running->pid] = RUNNING;
				sorted_pool[running->sorted_ind] = *running;

				pool[running->pid] = *running;
			}
		}

		for (p_ind = 0; p_ind < pool_sz; p_ind ++)
		{
			temp = &sorted_pool[p_ind];

			if (temp->arrival_time > cycle) break;

			if (temp->arrival_time == cycle)
			{
				if (running) {
					state[temp->pid] = READY;
					temp->started_wait = cycle;
					pool[temp->pid] = *temp;
					sorted_pool[temp->sorted_ind] = *temp;

					enqueue(&ready, *temp);
				} 
				else 
				{
					running = temp;
					state[running->pid] = RUNNING;
					running->cpu_burst = randomOS(running->cpu_burst_gen);

					if (running->cpu_burst > running->cpu_time_left)
					{
						running->cpu_burst = running->cpu_time_left;
					}

					pool[running->pid] = *running;
					sorted_pool[running->sorted_ind] = *running;
				}
			}
		}	

		if (!empty(&ready))
		{
			if (!running) 
			{
				p = dequeue(&ready);	
				running = &p;
				running->time_waited = cycle - running->started_wait;
				state[running->pid] = RUNNING;
				running->cpu_burst = randomOS(running->cpu_burst_gen);

				if (running->cpu_burst > running->cpu_time_left)
				{
					running->cpu_burst = running->cpu_time_left;
				}

				pool[running->pid] = *running;
				sorted_pool[running->sorted_ind] = *running;

			}
		}
		cycle += 1;
	}

	temp = NULL;
	running = NULL;
	printf("The scheduling algorithm used was Uniprogrammed\n\n");
	print_stats(pool, pool_sz, cycle - 1, sum_cpu, sum_io);
	clear(&ready);
}
Exemplo n.º 4
0
void fcfs(int state[], process pool[], int pool_sz)
{
	int term_count = 0;
	int p_ind;	
	int cycle = 0;
	process p;
	process * running = NULL;
	process * temp = NULL;

	queue ready = init();
	queue ties = init();
	int process_blocked = 0;

	double sum_cpu = 0;
	double sum_io = 0;

	//Maintain a sorted pool for verbose printout
	process sorted_pool[pool_sz];
	memcpy(sorted_pool, pool, sizeof(process) * pool_sz);
	qsort(sorted_pool, pool_sz, sizeof(process), process_cmp);

	while (term_count < pool_sz)
	{

		if (verbose) print_prev_cycle(state, sorted_pool, pool_sz, cycle);

		if (process_blocked)
		{
			process_blocked = 0;
			for (p_ind = 0; p_ind < pool_sz; p_ind ++) 
			{
				temp = &pool[p_ind];
				if (state[temp->pid] == BLOCKED)
				{
					process_blocked = 1;
					temp->io_burst = temp->io_burst - 1;
					temp->time_blocked += 1;
					if (temp->io_burst == 0)
					{
						state[temp->pid] = READY;
						temp->started_wait = cycle;
						enqueue(&ties, *temp);
					}
					sorted_pool[temp->pid] = *temp;
				}
			}

			if (process_blocked) sum_io += 1;
		}

		if (running) 
		{

			if (state[running->pid] == RUNNING)
			{
				running->cpu_burst = running->cpu_burst - 1;
				running->cpu_time_left = running->cpu_time_left - 1;
				sum_cpu += 1;
				sorted_pool[running->pid] = *running;
				pool[running->pid] = *running;
			}

			if (running->cpu_time_left == 0) {
				state[running->pid] = TERMINATED;
				term_count = term_count + 1;
				running->finished_at = cycle;
				running->turnaround = cycle - running->arrival_time;
				pool[running->pid] = *running;
				running = NULL;
			} 
			else if (running->cpu_burst == 0)
			{
				running->io_burst = randomOS(running->io_burst_gen);
				state[running->pid] = BLOCKED;
				sorted_pool[running->pid] = *running;
				pool[running->pid] = *running;
				running = NULL;
				process_blocked = 1;
			} 
		}

		for (p_ind = 0; p_ind < pool_sz; p_ind ++)
		{
			temp = &pool[p_ind];			

			if (temp->arrival_time == cycle)
			{
				state[temp->pid] = READY;
				temp->started_wait = cycle;
				enqueue(&ties, *temp);
			}
		}	

		break_ties(&ready, &ties);

		if (!empty(&ready) && !running)
		{
			p = dequeue(&ready);	
			running = &p;
			state[running->pid] = RUNNING;
			running->time_waited += cycle - running->started_wait;
			running->cpu_burst = randomOS(running->cpu_burst_gen);

			if (running->cpu_burst > running->cpu_time_left)
			{
				running->cpu_burst = running->cpu_time_left;
			}
			sorted_pool[running->pid] = *running;
			pool[running->pid] = *running;
		}
		cycle += 1;
	}


	temp = NULL;
	running = NULL;
	printf("The scheduling algorithm used was First Come First Served\n\n");
	print_stats(pool, pool_sz, cycle - 1, sum_cpu, sum_io);
	clear(&ready);
}
Exemplo n.º 5
0
Arquivo: rr.c Projeto: bkach/scheduler
void rr(FILE* ifp, FILE* ofp, FILE* rfp, int verbose, int dispRand){

	int failsafe = false;
	int failsafeT = 50;
	int test = false;

	int quantumCounter = 0;

	int allTerminated = false;
	int t = 0; 						// Time
	process* current;
	process* termCheck;

	process* root = (process*) malloc(sizeof(process));
	qi* rqRoot = (qi*) malloc(sizeof(qi));
	rqRoot->next = NULL;
	rqRoot->last = NULL;
	rqRoot->rqTail = rqRoot;

	root = parse(ifp, root);

	char* origInput = (char*) malloc(sizeof(char*)); 				// Original Input
	char* sortedInput = (char*) malloc(sizeof(char) * INPUT_SIZE);	// Sorted Input

	origInput = getInput(root);
	root = sortLL(root, root->numProcesses, artm);
	sortedInput = getInput(root);

	fprintf(ofp,"The original Input was: %s\nThe (sorted) Input is:  %s\n\n", origInput, sortedInput);

	if(verbose == true)
		fprintf(ofp,"%s\n\n", "This detailed printout gives the state and remaining burst for each process");


	while(!allTerminated){
		
		// Sets state and the printInt you need to print
		setState(root, quantumCounter);

		// Verbose Section
		if(verbose == true){
			process* printCurrent = root->next;
			fprintf(ofp, "Before Cycle%5d:",  t);
			while(printCurrent != NULL){
				fprintf(ofp, "%11s  %d", printCurrent->stateString, printCurrent->printInt);
				printCurrent = printCurrent->next;
			}
			fprintf(ofp, "%s", ".\n");
		}
	
		// Increments all counters related to waiting processes
		incrWaiting(rqRoot);
		// Sets unstarted programs to ready if t == their arrival time
		rrSetReady(root, t, rqRoot);
		// Sets blocked processes to ready if their IOBurstTime == 0
		// Also increments all counters related to blocked processes
		blocked(t, root, rqRoot, ofp);
		// Switch if any in the ready queue have the same arrival time
		tieBreaker(rqRoot);


		if(current == NULL || t == 0){
			if(test){
				fprintf(ofp,"%s\n", "Queue->Running");
			}		
			current = dequeue(rqRoot);
			if(current != NULL){
				current->state = RUNNING;
				if(current->CPUBurstTime == 0){
					current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);
				}
				if(test)
					fprintf(ofp, "%s%d\n", "current == Null or t == NULL, CPUBurstTime: ", current->CPUBurstTime);
			}
		}
		else if(current->state == RUNNING){
			current->CPUBurstTime--;
			current->totalCPUTime--;
			root->totalTimeRunning++;
			quantumCounter++;
			if(current->totalCPUTime == 0){
				quantumCounter = 0;
				if(test == true){
					fprintf(ofp,"%s\n", "Running->Terminated");
				}
				current->state = TERMINATED;
				current->finishingTime = t;
				current->turnaroundTime = current->finishingTime - current->arrivalTime;
				current = dequeue(rqRoot);
				if(current != NULL){
					current->state = RUNNING;
					if(current->CPUBurstTime == 0)
						current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);
					if(test)
						fprintf(ofp, "%s%d\n", "Terminated, CPUBurstTime: ", current->CPUBurstTime);
				}
			}
			else if(current->CPUBurstTime == 0){
				if(test){
					fprintf(ofp,"%s\n", "Running->Blocked");
				}
				quantumCounter = 0;
				current->state = BLOCKED;
				if(current->IOBurstTime == 0)
					current->IOBurstTime = randomOS(current->maxIOBurstTime, rfp, ofp, dispRand, IO);
				current = dequeue(rqRoot);
				if(current != NULL){
					current->state = RUNNING;
					if(current->CPUBurstTime == 0)
						current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);
					if(test)
						fprintf(ofp, "%s%d\n", "CPUBurstOut, CPUBurstTime: ", current->CPUBurstTime);
				}
			}
			else if(quantumCounter == quantum){
				if(test){
					fprintf(ofp,"%s\n", "Running->Preempted (quantum)");
				}
				quantumCounter = 0;

				current->state = READY;
				enqueue(t, current,rqRoot);
				if(test){
					fprintf(ofp,"%s\n", "Queue->Running");
				}		
				current = dequeue(rqRoot);
				if(current != NULL){
					current->state = RUNNING;
					if(current->CPUBurstTime <= 0){
						current->CPUBurstTime = randomOS(current->maxCPUBurstTime, rfp, ofp, dispRand, CPU);
					}
					if(test)
						fprintf(ofp, "%s%d\n", "quantum hit, CPUBurstTime: ", current->CPUBurstTime);
				}
			}
		}

		t++;

		if(failsafe == true && t >= failsafeT){
			fprintf(ofp,"%s\n", "FAILSAFE");
			break;
		}

		// Check if all processes are terminated
		termCheck = root->next;
		allTerminated = true;
		while(termCheck != NULL){
			if(termCheck->state != TERMINATED)
				allTerminated = false;
			termCheck = termCheck->next;
		}
	}

	fprintf(ofp,"\n%s\n", "The scheduling algorithm used was Round Robin");

	// Summary Data
	int i = 0;
	process* tempRoot    = (process*) malloc(sizeof(process));
	tempRoot = parse(ifp,tempRoot);
	process* tempCurrent = tempRoot->next;
	current = root->next;
	while(current != NULL){
		fprintf(ofp, "\n%s %d:\n", "Process", i);
		fprintf(ofp, "\t%s = (%d,%d,%d,%d)\n", "(A,B,C,IO)", tempCurrent->arrivalTime, tempCurrent->maxCPUBurstTime, tempCurrent->totalCPUTime, tempCurrent->maxIOBurstTime);
		fprintf(ofp, "\t%s: %d\n", "Finishing time", current->finishingTime);
		fprintf(ofp, "\t%s: %d\n", "Turnaround time", current->turnaroundTime);
		root->totalTurnaroundTime += current->turnaroundTime;
		fprintf(ofp, "\t%s: %d\n", "I/O time", current->timeBlocked);
		fprintf(ofp, "\t%s: %d\n", "Waiting Time", current->waitingTime);
		// Waiting Time needs to be edited
		root->totalWaitingTime += current->waitingTime;
		i++;
		current = current->next;
		tempCurrent = tempCurrent->next;
	}

	fprintf(ofp, "\n%s:\n", "Summary Data");
	fprintf(ofp, "\t%s: %d\n", "Finishing time", t-1);
	fprintf(ofp, "\t%s: %f\n", "CPU Utilization", (double) root->totalTimeRunning / (double) (t-1));
	fprintf(ofp, "\t%s: %f\n", "I/O Utilization", (double) root->totalTimeBlocked / (double) (t-1));
	fprintf(ofp, "\t%s: %f processes per hundred cycles\n", "Throughput", ((double) root->numProcesses / (double) (t-1)) * 100);
	fprintf(ofp, "\t%s: %f\n", "Average turnaround time", (double) root->totalTurnaroundTime / (double) root->numProcesses);
	fprintf(ofp, "\t%s: %f\n", "Average waiting time", (double) root->totalWaitingTime / (double) root->numProcesses);

}