操作系统——进程调度算法【FCFS:先到先服务,SJF:短进程优先,HRRN:高响应比优先】

#include
#include
using namespace std;

#define N 4


class Program {
public:
	char id;//程序号
	int arr_time;
	int run_time;//进程的到达时间和运行时间
	int rou_time;//周转时间
	int fin_time;//完成时间
	int wait_time;//等待时间
};

void getin(Program *p) {
    srand((unsigned)time(NULL));//设置随机数种子
	int a, b;
	
	for (int i = 0; i <= 3; i++)
	{
		a = rand() % 10 + 1;
	    b = rand() % 20 + 1;
		p[i].arr_time = a;
		p[i].run_time = b;
	}
}//随机获取时间


void Buddsort(Program *s) {
	int tem = 0, cem = 0;
	char men;
	for (int i = 0; i < N - 1; i++) {
		for (int j = 0; j < N - 1 - i; j++) {
			if (s[j].arr_time > s[j + 1].arr_time) {
				tem = s[j].arr_time;
				cem = s[j].run_time;
				men = s[j].id;

				s[j].arr_time = s[j + 1].arr_time;
				s[j].run_time = s[j + 1].run_time;
				s[j].id = s[j + 1].id;

				s[j + 1].arr_time = tem;
				s[j + 1].run_time = cem;
				s[j + 1].id = men;
			}
		}
	}
}//冒泡排序

void Buddsort1(Program *s) {
	int tem = 0, cem = 0;
	char men;
	for (int i = 0; i < N -1; i++) {
		for (int j = 0; j < N - 1 - i; j++) {
			if (s[j].arr_time > s[j + 1].arr_time) {
				tem = s[j].arr_time;
				cem = s[j].run_time;
				men = s[j].id;

				s[j].arr_time = s[j + 1].arr_time;
				s[j].run_time = s[j + 1].run_time;
				s[j].id = s[j + 1].id;

				s[j + 1].arr_time = tem;
				s[j + 1].run_time = cem;
				s[j + 1].id = men;
			}
			if (s[j].arr_time == s[j + 1].arr_time)
			{
				if (s[j].run_time > s[j + 1].run_time)
				{
					cem = s[j].run_time;
					men = s[j].id;

					s[j].run_time = s[j + 1].run_time;
					s[j].id = s[j + 1].id;

					s[j + 1].run_time = cem;
					s[j + 1].id = men;


				}


		     }
		}
	}
}//冒泡排序(符合SJF)

void BuddsortSJ(Program *s,int fl,int a) {
	int tem = 0, cem = 0;
	char men;
	int mun = a + fl - 1;
	int x = 0;
	for (int i = a ; i < mun ; i++) {
		for (int j = a ; j < mun - x; j++) {
			if (s[j].run_time > s[j + 1].run_time) {
				tem = s[j].arr_time;
				cem = s[j].run_time;
				men = s[j].id;

				s[j].arr_time = s[j + 1].arr_time;
				s[j].run_time = s[j + 1].run_time;
				s[j].id = s[j + 1].id;

				s[j + 1].arr_time = tem;
				s[j + 1].run_time = cem;
				s[j + 1].id = men;
			}
		}
		x++;
	}
}//冒泡排序



void FCFS(Program p[]) {
	int i;
	float a[4];
	Buddsort(p);
	for (i = 0; i <= 3; i++)
	{
		if (i == 0)
		{
			p[i].rou_time = p[i].run_time;
			p[i].fin_time = p[i].arr_time + p[i].run_time;
		}
		else
		{
			if (p[i].arr_time >= p[i - 1].fin_time)
			{
				p[i].rou_time = p[i].run_time;
				p[i].fin_time = p[i].arr_time + p[i].run_time;
			}
			else
			{
				p[i].fin_time = p[i - 1].fin_time + p[i].run_time;
				p[i].rou_time = p[i].fin_time - p[i].arr_time;
			}
		}
	}
		cout << "输出序列\t" << "到达时间\t" << "运行时间\t" << "周转时间\t" <<"带权周转时间"<< endl;
		for (i = 0; i <= N-1; i++)
		{
			a[i] = (float)p[i].rou_time / (float)p[i].run_time;
			cout << p[i].id << "\t\t" << p[i].arr_time << "\t\t" << p[i].run_time << "\t\t" << p[i].rou_time << "\t\t" << a[i] << endl;
		}
		cout << "算法平均周转时间\t" << "算法的平均带权周转时间" << endl;
		cout << (float)(p[0].rou_time + p[1].rou_time + p[2].rou_time + p[3].rou_time) / 4 << "\t\t\t" << (a[0] + a[1] + a[2] + a[3]) / 4 << endl;
}//先来先服务

void SJF(Program p[]) {
	int i, j, a;
	float b[4];
	int flag = 0;//记载前一个进程运行完等待时间结束的进程数
	Buddsort1(p);
	for (i = 0; i <= N - 1; i++)
	{
		flag = 0;
		a = i;
		if (i == 0)
		{
			p[i].rou_time = p[i].run_time;
			p[i].fin_time = p[i].arr_time + p[i].run_time;
		}
		else {
			for (j = a; j <= N - 1; j++)
			{
				if (p[j].arr_time <= p[i - 1].fin_time)
					flag++;
			}
			if (flag >= 1)
			{
				BuddsortSJ(p, flag, a);
				p[i].fin_time = p[i - 1].fin_time + p[i].run_time;
				p[i].rou_time = p[i].fin_time - p[i].arr_time;
			}
			if (flag == 0)
			{
				p[i].rou_time = p[i].run_time;
				p[i].fin_time = p[i].arr_time + p[i].run_time;

			}
		}
	}

	cout << "输出序列\t" << "到达时间\t" << "运行时间\t" << "周转时间\t" << "带权周转时间" << endl;
	for (i = 0; i <= N - 1; i++)
	{
		b[i] = (float)p[i].rou_time / (float)p[i].run_time;
		cout << p[i].id << "\t\t" << p[i].arr_time << "\t\t" << p[i].run_time << "\t\t" << p[i].rou_time << "\t\t" << b[i] << endl;
	}
	cout << "算法平均周转时间\t" << "算法的平均带权周转时间" << endl;
	cout << (float)(p[0].rou_time + p[1].rou_time + p[2].rou_time + p[3].rou_time) / 4 << "\t\t\t" << (b[0] + b[1] + b[2] + b[3]) / 4 << endl;


}//短作业优先

int max(float r[],int j,int fl) {
	float tem ;
	int cem,flag=0;
	int maxn[N];
	int a = j,b=j;
	for (int m = a; m <= N- 1;m++) {
		maxn[m] = b;
		b++;
	}
	for (int m = 0; m < a; m++) {
		maxn[m] = 0;
	}
	for (j; j< fl + a-1; j++) {
		
			if (r[j] > r[j + 1])
			{
				cem = maxn[j];
				tem = r[j];
				r[j] = r[j + 1];
				maxn[j] = maxn[j + 1];
				r[j + 1] = tem;
				maxn[j + 1] = cem;

			}
		
	}
	return maxn[a+fl-1];
}


void HRRN(Program p[]) {
	int i,j,flag,m;
	Program a;
	float rp[4],b[4];
	Buddsort(p);

	for (i = 0; i <= N - 1; i++)
	{
		flag = 0;
		if (i == 0)
		{
			p[i].rou_time = p[i].run_time;
			p[i].fin_time = p[i].arr_time + p[i].run_time;

		}
		for (j = i+1; j <= N - 1; j++)
		{
			if (p[j].arr_time <= p[i].fin_time)
				flag++;
		}
		if (flag >= 1)
		{
			for (j = i+1; j <= i+ flag; j++)
			{
				p[j].wait_time = p[i].fin_time - p[j].arr_time;
				rp[j] = (float)p[j].wait_time / p[j].run_time + 1;
			}
			j = i + 1;
			m = max(rp, j, flag);
			if(m!=0){
			a = p[i+1];
			p[i + 1] = p[m];
			p[m] = a;
			
			p[i+1].fin_time = p[i].fin_time + p[i+1].run_time;
			p[i+1].rou_time = p[i+1].fin_time - p[i+1].arr_time;
			}
			if(m==0){
			p[i + 1].fin_time = p[i].fin_time + p[i + 1].run_time;
			p[i + 1].rou_time = p[i + 1].fin_time - p[i + 1].arr_time;
			}
		}
		if (flag == 0) {
			p[j].rou_time = p[j].run_time;
			p[j].fin_time = p[j].arr_time + p[j].run_time;

		}

	}
	cout << "输出序列\t" << "到达时间\t" << "运行时间\t" << "周转时间\t" << "带权周转时间" << endl;
	for (i = 0; i <= N - 1; i++)
	{
		b[i] = (float)p[i].rou_time / (float)p[i].run_time;
		cout << p[i].id << "\t\t" << p[i].arr_time << "\t\t" << p[i].run_time << "\t\t" << p[i].rou_time << "\t\t" << b[i] << endl;
	}
	cout << "算法平均周转时间\t" << "算法的平均带权周转时间" << endl;
	cout << (float)(p[0].rou_time + p[1].rou_time + p[2].rou_time + p[3].rou_time) / 4 << "\t\t\t" << (b[0] + b[1] + b[2] + b[3]) / 4 << endl;

}//高响应比

int main() {
	int s;
	Program a[N];
	for (int i = 0; i <= 3; i++)
	{
		a[i].id = (char)(65+i);
		
	}//给予四个进程号
	getin(a);
	while (1) {
		cout << "+---------------------------------------+" << endl;
		cout << "|\t>>> 算法清单 <<<\t\t|" << endl;
		cout << "|\t1.先来先服务算法(FCFS)\t\t|" << endl << "|\t2.最短作业优先算法(SJF)\t\t|" << endl << "|\t3.最高响应比优先算法(HRN)\t|" << endl;
		cin >> s;
		switch (s)
		{
		case 1:
			FCFS(a);
			break;
		case 2:
			SJF(a);
			break;
		case 3:
			HRRN(a);
			break;

		}
	}
	system("pause");
	return 0;
}

这个代码比较直白,就采用多次冒泡算法,进行排序,在计算周转时间和其他时间,但我发现好多其他人的代码在SJF的代码中出现问题(并未考虑到达时间和作业长度的孰前孰后)。

你可能感兴趣的:(算法,蓝桥杯,c++)