Beispiel #1
0
void ShortestJobFirst(){
	out << alg.name << std::endl;

	int timer = 0;

	//who is ready at 0 seconds, add to priority
	while(!master.empty()){
		struct Qu t = master.front();
		if(t.arrival_time == 0) priority.push_back(t);	
		else	waiting.push(t);
		master.pop();
	}

	while(completed.size() != alg.number_of_proc){
		int flag_inner = 0;

		//sort lowest cpu first
		while(!priority.empty()){
			flag_inner = 1;
			priority_sort("sjf");	
		}
		//now in ready, run all, set var, timer
		while(!ready.empty()){
			flag_inner = 1;	
			if(ready.front().flag_touched == 0){
				ready.front().flag_touched = 1;
				ready.front().time_start = timer;
			}
			out << timer << "\t" << ready.front().proc_num << std::endl;

			while(ready.front().cpu_burst != 0){
				--ready.front().cpu_burst;
				++timer;
			}
			ready_to_completed(timer);	
			
			//add ready and waiting to priority
			int i;
			for(i=0; i<ready.size(); ++i){
				priority.push_back(ready.front());
				ready.pop();
				--i;
			}
			for(i=0; i<waiting.size(); ++i){
				if(waiting.front().arrival_time <= timer){
					priority.push_back(waiting.front());
					waiting.pop();
					--i;
				} }
		}

		//increment timer
		if(flag_inner == 1) flag_inner = 0;
		else	++timer;
	}

	//calculate waiting time, started - arrival 
	int i;
	for(i=0; i<completed.size(); ++i){ //started - arrival
		completed.front().time_waiting = completed.front().time_start - completed.front().arrival_time;
		cycle(&completed);
	}

	double total_w = 0;
	while(!completed.empty()){
		total_w += completed.front().time_waiting;
		completed.pop();
	}
	total_w /= alg.number_of_proc;
	out << std::setprecision(3)  << "AVG Waiting Time: " << total_w << std::endl;
}
Beispiel #2
0
/*
**++
**  ROUTINE NAME:           rpc__codesets_read_registry_file
**
**  SCOPE:                  PRIVATE
**
**  DESCRIPTION:
**
**  Internal routine to allocate OSF code set registry data into a memory.
**  rpc__codesets_really_read_file() is called to actually perform
**  reading data from a registry file.  Code set registry is sorted by
**  code set id and code set name.  Local global pointers point to each
**  sorted memory.
**
**--
*/
PRIVATE
rpc__codesets_read_registry_file
(
	error_status_t	*status
)
{
	entry_t		**sort_name_codesets;
	entry_t		**sort_name_save;
	entry_t		**sort_id_codesets;
	entry_t		**sort_id_save;
	entry_t		**sort_priority_codesets;
	entry_t		**sort_priority_save;
	int		i;
	entry_t		*ep;

	if (!rpc_g_codesets_did_read)
	{
		dcethread_once_throw(&rpc_g_codesets_once_block,
		(dcethread_initroutine)rpc__codesets_really_read_file);

		if (rpc_g_codesets_status != rpc_s_ok)
		{
			*status = rpc_g_codesets_status;
			return;
		}
	}

	/*
	** Sort the code set registry file by code set name
	**/
	if ((sort_name_save = (entry_t **)malloc(sizeof(entry_t *) * rpc_g_codesets_entry_count)) == NULL)
	{
		*status = dce_cs_c_cannot_allocate_memory;
		return;
	}

	ep = rpc_g_codesets_list;
	i = rpc_g_codesets_entry_count;
	sort_name_codesets = sort_name_save;
	while (i--)
	{
		*sort_name_codesets++ = ep++;
	}
	rpc_g_codesets_sort_by_name = sort_name_save;
	sort_name_codesets = sort_name_save;
	name_sort(sort_name_codesets, rpc_g_codesets_entry_count);

	/*
	** Sort the effective code set by priority
	**/
	if ((sort_priority_save = (entry_t **)malloc(sizeof(entry_t *) * rpc_g_codesets_effective_count)) == NULL)
	{
		*status = dce_cs_c_cannot_allocate_memory;
		return;
	}
	ep = rpc_g_codesets_effective_ids;
	i = rpc_g_codesets_effective_count;
	sort_priority_codesets = sort_priority_save;
	while (i--)
	{
		*sort_priority_codesets++ = ep++;
	}
	rpc_g_codesets_sort_by_priority = sort_priority_save;
	sort_priority_codesets = sort_priority_save;
	priority_sort(sort_priority_codesets, rpc_g_codesets_effective_count);

	/*
	** Allocate an array for pointers to entry_t.  Code set registry
	** is already sorted by code set id value.
	*/
	if ((sort_id_save = (entry_t **)malloc(sizeof(entry_t *) * rpc_g_codesets_entry_count)) == NULL)
	{
		*status = dce_cs_c_cannot_allocate_memory;
		return;
	}
	ep = rpc_g_codesets_list;
	i = rpc_g_codesets_entry_count;
	sort_id_codesets = sort_id_save;
	while (i--)
	{
		*sort_id_codesets++ = ep++;
	}
	rpc_g_codesets_sort_by_id = sort_id_save;

	*status = rpc_s_ok;

}
Beispiel #3
0
void PriorityWithPre(){
	out << alg.name << std::endl;

	int timer = 0;

	//who is ready at 0 seconds, add to priority
	while(!master.empty()){
		struct Qu t = master.front();
		if(t.arrival_time == 0) priority.push_back(t);	
		else	waiting.push(t);
		master.pop();
	}
	while(completed.size() != alg.number_of_proc){
		int flag_inner = 0;
		//sort lowest cpu first
		while(!priority.empty()){
			flag_inner = 1;
			priority_sort("priority");
		}
		//now in ready, run all, set var, timer
		while(!ready.empty()){
			flag_inner = 1;

			if(ready.front().flag_touched == 0){
				ready.front().flag_touched = 1;
				ready.front().time_start = timer;
			}
			out << timer << "\t" << ready.front().proc_num << std::endl;

			//is front highest priority? check every time timer incs
			bool stillWorking = true;
			while(ready.front().cpu_burst != 0){
				--ready.front().cpu_burst;
				++timer;

				//if proc not finished, prepare for possible preempt
				if(ready.front().cpu_burst != 0)
					stillWorking = true;
				else
					stillWorking = false;

				//CHECK waiting IF NEW PRC
				int i;
				for(i=0; !waiting.empty() && i<waiting.size(); ++i){
					if(waiting.front().arrival_time <= timer){
						priority.push_back(waiting.front());
						waiting.pop();
						--i;
					}
					else cycle(&waiting);
				}

				//add all from ready to priority list, if front not highest priority
				// then break
				struct Qu tt = ready.front();
				while(!ready.empty()){
					priority.push_back(ready.front());
					ready.pop();
				}
				priority_sort("priority");	
				if(tt.proc_num != ready.front().proc_num) break; 
			}
			if(!stillWorking) ready_to_completed(timer);	
		}

		//increment timer
		if(flag_inner == 1) flag_inner = 0;
		else ++timer;  
		int i;
		for(i=0; i<waiting.size(); ++i){
			if(waiting.front().arrival_time <= timer){
				priority.push_back(waiting.front());
				waiting.pop();
				--i;
			} }
	}

	//calculate waiting time, wt = complete time - cpuburst - arrival 
	int i;
	for(i=0; i<completed.size(); ++i){
		completed.front().time_waiting = completed.front().time_completed - completed.front().total_burst - completed.front().arrival_time;
		cycle(&completed);
	}

	double total_w = 0;
	while(!completed.empty()){
		total_w += completed.front().time_waiting;
		completed.pop();
	}
	total_w /= alg.number_of_proc;
	out << std::setprecision(3)  << "AVG Waiting Time: " << total_w << std::endl;
}