*** 作系统 Linux 大作业 保姆级教程(3)

 *** 作系统 Linux 大作业 保姆级教程(3),第1张

*** 作系统 Linux 大作业 保姆级教程(3)

3、在Linux环境下编写C或C++程序实现几种CPU调度算法:FCFS、SJF和优先权调度。在Linux下进行编译和运行,可使用Makefile文件实现程序的编译、安装和卸载。并比较这几种CPU调度算法的性能,给出等待时间、周转时间及其平均值。(报告中给出源代码、Makefile文件、make运行结果以及程序运行结果)。

 

实验所用测试数据如下表(算法均默认为非抢占)(30分)

 在本地vscode编写c.c和makefile.mk

c.c

#include 
#include 
typedef struct {
	int id;
	int level;
	int cometime;
	int servertime;
	int waittime;
	int finishtime;
	int turnovertime;
}JOB;
//存数据
JOB jobs[7] = {
1,0,800,50,0,0,0,
2,1,815,30,0,0,0,
 3,2,830,25,0,0,0,
4,2,835,20,0,0,0,
5,2,845,15,0,0,0,
6,1,700,10,0,0,0,
7,0,820,5,0,0,0,
};
int compare1(const void* a, const void* b) {
	return ((JOB*)a)->cometime - ((JOB*)b)->cometime;
}
void FCFS() {
	qsort(jobs, 7, sizeof(JOB), compare1);
	int i = 0;
	int totalwait = 0, totalturnover = 0;
	float averagewait = 0, averageturnover = 0;
	for (; i < 7; i++) {
		//两种情况 一种就是来了就能运行
		if (i == 0 || (i > 0 && jobs[i - 1].finishtime <= jobs[i].cometime)) {
			jobs[i].waittime = 0;
			jobs[i].turnovertime = jobs[i].servertime;
			jobs[i].finishtime = jobs[i].cometime + jobs[i].servertime;
		}
		//来了需要等待
		else {
			jobs[i].waittime = jobs[i - 1].finishtime - jobs[i].cometime;
			jobs[i].turnovertime = jobs[i].waittime + jobs[i].servertime;
			jobs[i].finishtime = jobs[i - 1].finishtime + jobs[i].servertime;
		}
		totalwait += jobs[i].waittime;
		totalturnover += jobs[i].turnovertime;
	}
	averagewait = totalwait / 7;
	averageturnover = totalturnover / 7;
	printf("FCFS:n total_wait_time: %dn total_turnover_time: %dnaverage_wait_time: % .1fn average_turnover_time : % .1fn",totalwait,totalturnover, averagewait, averageturnover);
}
void SJF() {
	int vis[7] = { 0 };
	int stop = 0;
	qsort(jobs, 7, sizeof(JOB), compare1);
	int totalwait = 0, totalturnover = 0;
	float averagewait = 0, averageturnover = 0;
	int finish = 0;
	while (stop < 7) {
		int i = 0;
		for (; i < 7; i++)
			if (jobs[i].cometime > finish)
				break;
		int j = i - 1;
		int flag = 0;
		int idx = -1, shortest = 10000;
		for (; j >= 0; j--) {
			if (vis[j])
				continue;
			else {
				flag = 1;
				//比较能否插队
				if (jobs[j].servertime < shortest) {
					shortest = jobs[j].servertime;
					idx = j;
				}
			}
		}
		if (!flag) {
			vis[i] = 1;
			jobs[i].waittime = 0;
			jobs[i].turnovertime = jobs[i].servertime;
			finish = jobs[i].cometime + jobs[i].servertime;
			jobs[i].finishtime = finish;
			totalwait += jobs[i].waittime;
			totalturnover += jobs[i].turnovertime;
		}
		else {
			vis[idx] = 1;
			jobs[idx].waittime = finish - jobs[idx].cometime;
			jobs[idx].turnovertime = jobs[idx].waittime + jobs[idx].servertime;
			finish += jobs[idx].servertime;
			jobs[idx].finishtime = finish;
			totalwait += jobs[idx].waittime;
			totalturnover += jobs[idx].turnovertime;
		}
		//乱序 完成一个stop+1
		stop = vis[0] + vis[1] + vis[2] + vis[3] + vis[4] + vis[5] + vis[6];
	}
	averagewait = totalwait / 7;
	averageturnover = totalturnover / 7;
	printf("SJF: n total_wait_time: %dn total_turnover_time: %dnaverage_wait_time: % .1fn average_turnover_time : % .1fn", totalwait,totalturnover, averagewait, averageturnover);
}
void Priority() {
	int vis[7] = { 0 };
	int stop = 0;
	qsort(jobs, 7, sizeof(JOB), compare1);
	int totalwait = 0, totalturnover = 0;
	float averagewait = 0, averageturnover = 0;
	int finish = 0;
	//运行完一个+1 到7就没了
	while (stop < 7) {
		int i = 0;
		for (; i < 7; i++)
			if (jobs[i].cometime > finish)
				break;
		int j = i - 1;
		int flag = 0;
		int idx = -1, pri = -1;
		for (; j >= 0; j--) {
			if (vis[j])
				continue;
			else {
				flag = 1;
				//优先级比较 大的抢占
				if (jobs[j].level > pri) {
					pri = jobs[j].level;
					idx = j;
				}
				else if (jobs[j].level == pri) {
					idx = jobs[j].cometime < jobs[idx].cometime ? j : idx;
				}
			}
		}
		if (!flag) {
			vis[i] = 1;
			jobs[i].waittime = 0;
			jobs[i].turnovertime = jobs[i].servertime;
			finish = jobs[i].cometime + jobs[i].servertime;
			jobs[i].finishtime = finish;
			totalwait += jobs[i].waittime;
			totalturnover += jobs[i].turnovertime;
		}
		else {
			vis[idx] = 1;
			jobs[idx].waittime = finish - jobs[idx].cometime;
			jobs[idx].turnovertime = jobs[idx].waittime + jobs[idx].servertime;
			finish += jobs[idx].servertime;
			jobs[idx].finishtime = finish;
			totalwait += jobs[idx].waittime;
			totalturnover += jobs[idx].turnovertime;
		}
		stop = vis[0] + vis[1] + vis[2] + vis[3] + vis[4] + vis[5] + vis[6];
	}
	averagewait = totalwait / 7;
	averageturnover = totalturnover / 7;
	printf("Pri: n total_wait_time: %dn total_turnover_time: %dnaverage_wait_time: % .1fn avergae_turnover_time : % .1fn", totalwait,totalturnover, averagewait, averageturnover);
}
int main()
{
	FCFS();
	SJF();
	Priority();
	return 0;
}

makefile.mk

c:c.c
	gcc -o c c.c

移动文件到虚拟机 如何在win和虚拟机之间数据传递请看这里

脚本优先级取大

把写好的脚本文件c.c移动到文件夹

运行

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5703012.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存