Demo entry 6338156

短进程优先调度算法

   

Submitted by anonymous on Dec 11, 2016 at 07:01
Language: C++. Code size: 3.3 kB.

#include "stdio.h"
#include "conio.h"
struct sjf {
	char name[10];     //进程名
	float arrivetime;		//到达时间
	float servicetime;		//服务时间
	float starttime;		//开始时间
	float finishtime;		//完成时间
	float zztime;		//周转时间
	float dqzztime;		//带权周转时间
	float cckj;		//存储空间
	float yxtime;		//运行时间
};
sjf a[100];
void input(sjf *p, int N)
{
	int i;
	printf("请输入作业名  到达时间 服务时间 存储空间:\n 例如: p1 0 100 30\n");
	for (i = 0; i <= N - 1; i++)
	{
		printf("请输入第 %d作业信息:\n", i + 1);
		scanf("%s%f%f%f", &p[i].name, &p[i].arrivetime, &p[i].servicetime, &p[i].cckj);
	}

}

void Print(sjf *p, float arrivetime, float servicetime, float starttime, float finishtime, float zztime, float dqzztime, float cckj, int N)
{
	int k;

	printf("执行顺序:");
	printf("%s", p[0].name);
	for (k = 1; k<N; k++)
	{
		printf("-->%s", p[k].name);
	}
	printf("\n作业信息:\n");
	printf("\nname\t arrive\t service\t start\t finish\t zz\t dqzz\t cckj\t ddsj\t yxtime\n");
	for (k = 0; k <= N - 1; k++)
	{
		printf("%s\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\n", p[k].name, p[k].arrivetime, p[k].servicetime, p[k].starttime, p[k].finishtime, p[k].zztime, p[k].dqzztime, p[k].cckj, p[k].finishtime - p[k].servicetime, p[k].servicetime);
	}

}
//pai xu
void sort(sjf *p, int N)
{
	for (int i = 0; i <= N - 1; i++)
		for (int j = 0; j <= i; j++)
			if (p[i].arrivetime<p[j].arrivetime)
			{
				sjf temp;
				temp = p[i];
				p[i] = p[j];
				p[j] = temp;
			}
}

//yun xing jieduan
void deal(sjf *p, float arrivetime, float servicetime, float starttime, float finishtime, float &zztime, float &dqzztime, float cckj, int N)
{
	int k;
	for (k = 0; k <= N - 1; k++)
	{
		if (k == 0)
		{
			p[k].starttime = p[k].arrivetime;
			p[k].finishtime = p[k].arrivetime + p[k].servicetime;
		}
		else
		{
			p[k].starttime = p[k - 1].finishtime;
			p[k].finishtime = p[k - 1].finishtime + p[k].servicetime;
		}
	}

	for (k = 0; k <= N - 1; k++)
	{
		p[k].zztime = p[k].finishtime - p[k].arrivetime;
		p[k].dqzztime = p[k].zztime / p[k].servicetime;
	}
}



void sjff(sjf *p, int N)
{
	float arrivetime = 0, servicetime = 0, starttime = 0, finishtime = 0, zztime = 0, dqzztime = 0, cckj = 0;

	sort(p, N);

	for (int m = 0; m<N - 1; m++)
	{
		if (m == 0)
			p[m].finishtime = p[m].arrivetime + p[m].servicetime;
		else
			p[m].finishtime = p[m - 1].finishtime + p[m].servicetime;

		int i = 0;
		for (int n = m + 1; n <= N - 1; n++)
		{
			if (p[n].arrivetime <= p[m].finishtime)
				i++;
		}
		float min = p[m + 1].servicetime;
		int next = m + 1;//m+1=n
		for (int k = m + 1; k<m + i; k++)
		{
			if (p[k + 1].servicetime<min)
			{
				min = p[k + 1].servicetime;
				next = k + 1;
			}

		}
		sjf temp;
		temp = p[m + 1];
		p[m + 1] = p[next];
		p[next] = temp;
	}

	deal(p, arrivetime, servicetime, starttime, finishtime, zztime, dqzztime, cckj, N);

	Print(p, arrivetime, servicetime, starttime, finishtime, zztime, dqzztime, cckj, N);
}


int main()
{
	int N;
	printf("------***短作业优先调度算法***------\n");
	printf("输出作业序号  运行时间  存储空间  等待时间\n");
	printf("\n");
	printf("请输入作业个数:\n");
	scanf("%d", &N);
	input(a, N);
	sjf *b = a;
	sjff(b, N);
	getch();
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).