シミュレーションプロセススケジューリングアルゴリズム

7153 ワード

短い作業優先アルゴリズム、時間スライスの回転法と優先数アルゴリズムの実行状況をシミュレーションし、そのプロセス実行のGantt図を動的に描き、上記アルゴリズムの各プロセスの応答時間と回転時間を計算する.
/*
name:geting
time:2014-12-10
*/
#include
using namespace std;

const int MAX = 100;

//       
struct process{
	char p_name[10];	//   
	int reach_time;		//    
	int run_time;		//      
	int priority;		//   
	int response_time;	//    
	int turnaround_time;//    
};
struct gantt{
	char p_name[10];	//   
	int run_time;		//    
};

process p[MAX];			//    
gantt g[MAX];			//Gantt   
int p_Num;				//     
char FileName[20];		//       
int MaxReachTime;		//      
int BeReached[MAX];		//      
int reach_Num;			//     

//   
int ReadFile()
{
	FILE *fp; 
	int i; 
	char c;
	
	cout << "            (  :         ): "; 
	cin >> FileName; 

	if((fp = fopen(FileName,"r")) == NULL)
	{ 
		cout << "  :   " << FileName << "    ,         !!" << endl; 
		exit(0);
	} 
	else
	{
		c = fgetc(fp);
		while(c == ' ' || c == 10 || c == 9)	//    、   TAB  
		{
			c = fgetc(fp);
		}
		if(c == EOF)
		{
			printf("  :  :%s   !!
", FileName); exit(0); } else { fseek(fp, -1, SEEK_CUR); while(!feof(fp)) { fscanf(fp, "%s %d %d %d", p[p_Num].p_name, &p[p_Num].reach_time, &p[p_Num].run_time, &p[p_Num].priority); p_Num++; } // cout << endl << " " << FileName << " :" << endl << endl; cout << " " << endl; for(i = 0; i < p_Num; i++) { cout << " " << p[i].p_name << " " << p[i].reach_time << " " << p[i].run_time << " " << p[i].priority << endl; } cout << endl << " :" << p_Num << endl << endl; return 0; } } } // void GetMaxReachTime() { for(int i = 0; i < p_Num; i++) { if(p[i].reach_time > MaxReachTime) MaxReachTime = p[i].reach_time; } } // void Init() { for(int i = 0; i < MAX; i++) { strcpy(p[i].p_name, ""); p[i].reach_time = -1; p[i].run_time = -1; p[i].priority = -1; p[i].response_time = -1; p[i].turnaround_time = -1; strcpy(g[i].p_name, ""); g[i].run_time = 0; BeReached[i] = -1; } p_Num = 0; MaxReachTime = 0; reach_Num = 0; ReadFile(); GetMaxReachTime(); } // bool CheckFinish() { for(int i = 0; i < p_Num; i++) { if(p[i].run_time > 0) return false; } return true; } // Gantt void ShowGantt() { int i = 0; cout << "Gantt :" << endl << endl; cout << " " << endl; while(true) { if(g[i].run_time == 0) break; cout << " " << g[i].p_name << " " << g[i].run_time << endl; i++; } cout << endl; } // void ShowTime() { int i = 0; cout << " " << endl; while(i < p_Num) { cout << " " << p[i].p_name << " " << p[i].response_time << " " << p[i].turnaround_time << endl; i++; } cout << endl; } // void UpdateReachArray(int t) { if(t <= MaxReachTime) { for(int i = 0; i < p_Num; i++) { if(p[i].reach_time - t == 0) { BeReached[reach_Num++] = i; } } } } // , void SPN() { int t = 0, i, j = -1; int ilMin; // int iMin = -1; // int iMinRuntime; // while(true) { UpdateReachArray(t); // ilMin = iMin; iMinRuntime = 100000; for(i=0; i 0 && p[BeReached[i]].run_time < iMinRuntime) { iMin = BeReached[i]; iMinRuntime = p[BeReached[i]].run_time; } } if(ilMin != iMin) // , j++ { j++; strcpy(g[j].p_name, p[iMin].p_name); } g[j].run_time += 1; p[iMin].run_time -= 1; if(p[iMin].response_time == -1) p[iMin].response_time = t - p[iMin].reach_time; // if(p[iMin].run_time == 0) p[iMin].turnaround_time = t + 1 - p[iMin].reach_time; // if(CheckFinish()) break; t++; // 1 } ShowGantt(); ShowTime(); } // , ,BTime void RR(int BTime) { int t = 0, j = 0, k = 0; int FBTime = 0; // while(true) { UpdateReachArray(t); if(FBTime == BTime || p[BeReached[k]].run_time == 0) // { j++; k++; FBTime = 0; // } if(k == reach_Num) k = 0; // , while(p[BeReached[k]].run_time == 0) // , { k++; if(k == reach_Num) k = 0; } strcpy(g[j].p_name, p[BeReached[k]].p_name); g[j].run_time += 1; p[BeReached[k]].run_time -= 1; if(p[BeReached[k]].response_time == -1) p[BeReached[k]].response_time = t - p[BeReached[k]].reach_time; // if(p[BeReached[k]].run_time == 0) p[BeReached[k]].turnaround_time = t + 1 - p[BeReached[k]].reach_time; // if(CheckFinish()) break; t++; // 1 FBTime++; // 1 } ShowGantt(); ShowTime(); } // void Priority() { int t = 0, i, j = -1; int ilMin; // int iMin = -1; // int iMinPriority; // ( ) while(true) { UpdateReachArray(t); // ilMin = iMin; iMinPriority = 100; for(i = 0; i < reach_Num; i++) // { if(p[BeReached[i]].run_time > 0 && p[BeReached[i]].priority < iMinPriority) { iMin = BeReached[i]; iMinPriority = p[BeReached[i]].priority; } } if(ilMin != iMin) // , j++ { j++; strcpy(g[j].p_name, p[iMin].p_name); } g[j].run_time += 1; p[iMin].run_time -= 1; if(p[iMin].response_time == -1) p[iMin].response_time = t - p[iMin].reach_time; // if(p[iMin].run_time == 0) p[iMin].turnaround_time = t + 1 - p[iMin].reach_time; // if(CheckFinish()) break; t++; // 1 } ShowGantt(); ShowTime(); } void Information() { cout << "*********************************** ***********************************" << endl; cout << " :" << endl; cout << " " << endl << endl; } void main() { Information(); cout << "******************************** ********************************" << endl << endl; Init(); SPN(); cout << "**************************** ( 1)***************************" << endl << endl; Init(); RR(1); cout << "**************************** ( 2)***************************" << endl << endl; Init(); RR(2); cout << "**************************** ( 3)***************************" << endl << endl; Init(); RR(3); cout << "********************************** **********************************" << endl << endl; Init(); Priority(); }