进程调度之HRN

进程调度之HRN

HRN算法,即优先权高先执行。本文优先权R = (执行时间 + 等待时间 )/执行时间

每次在进程队列中找到已到达的优先权最高的进程执行,并标记为已执行。

#include
#include
#define N 20

struct JOB{
	char name[5] ;
	int atime ;
	int runtime ;
	int ftime ;
	int total ;	
	float welght ;
	int arun ;
	float order;

	int flag;
};

int Runtime = 0;
int Total = 0;
float Welght = 0;

void print(struct JOB f[], int i);

void main()
{
	struct JOB f[N] ;
	int i,j;
	int amount;
	char n[5];
	int a,r;

	int forder = 0;
	int order;
	int findex = -1;

	printf("请输入进程数(2-19)\n");
	scanf("%d", &amount);
	for(i = 0; i < amount; i++)
	{
		printf("请输入进程名,线程到达时间,线程运行时间\n");
		scanf("%s %d %d",&f[i].name, &f[i].atime, &f[i].runtime);
		f[i].flag = 0 ;
	}

	for(i = 0; i < amount - 1; i++)
	{
		for(j = i+1; j < amount; j++)
		{
			if(f[j].atime >f[i].atime || (f[j].atime == f[i].atime  && f[j].runtime > f[i].runtime))
			{
				continue;
			}
			strcpy(n, f[i].name);
			a = f[i].atime;
			r = f[i].runtime;

			strcpy(f[i].name, f[j].name);
			f[i].atime = f[j].atime;
			f[i].runtime = f[j].runtime;

			strcpy(f[j].name, n);
			f[j].atime = a;
			f[j].runtime = r;
			
		}
	}


	printf("\n进程名\t到达时间\t开始运行时间\t运行时间\t结束时间\t周转时间\t周转系数\t优先权\n");
	for(j = 0; j < amount; j++)
	{
		for(i = 0; i < amount; i++)
		{
			if(f[i].flag == 1) continue;
			if(Runtime < f[i].atime && findex == -1)
			{
				f[i].arun = f[i].atime;
				findex = i ;
				break;
			}
			else
			{
				if(Runtime < f[i].atime)
				{
					break;
				}
				else
				{
					if((order = (Runtime - f[i].atime + f[i].runtime)/f[i].runtime) > forder)
					{
						findex = i ;
						forder = order ;
						f[i].arun = Runtime;

					}
				}
			}
		}
		print(f,findex);
		f[findex].flag = 1;
		findex = -1 ;
		forder = 0 ;
	}
	


}

void print(struct JOB f[], int i)
{
	f[i].ftime = f[i].arun + f[i].runtime ;
	f[i].total = f[i].ftime - f[i].atime ;
	f[i].welght = (float)f[i].total/(float)f[i].runtime ;
	f[i].order = (float)(f[i].arun - f[i].atime + f[i].runtime)/(float)f[i].runtime;

	printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%f\t%f\n",f[i].name, f[i].atime, 
			f[i].arun, f[i].runtime, f[i].ftime, f[i].total, f[i].welght, f[i].order);
	Runtime = f[i].ftime ;
	Total += f[i].total ;
	Welght += f[i].welght ;
}


 

你可能感兴趣的:(c/c++)