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

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

实验所用测试数据如下表(算法均默认为非抢占)(30分)操作系统 Linux 大作业 保姆级教程(3)_第1张图片

 在本地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: %d\n total_turnover_time: %d\naverage_wait_time: % .1f\n average_turnover_time : % .1f\n",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: %d\n total_turnover_time: %d\naverage_wait_time: % .1f\n average_turnover_time : % .1f\n", 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: %d\n total_turnover_time: %d\naverage_wait_time: % .1f\n avergae_turnover_time : % .1f\n", totalwait,totalturnover, averagewait, averageturnover);
}
int main()
{
	FCFS();
	SJF();
	Priority();
	return 0;
}

makefile.mk

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

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

脚本优先级取大

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

运行

操作系统 Linux 大作业 保姆级教程(3)_第2张图片

=======================================

Tip:

虚拟机和Win10 互相传递数据:

VMware16Pro 与win10传送数据方法_不知名做题家丶的博客-CSDN博客_win10虚拟机和主机文件传输

你可能感兴趣的:(操作系统Linux,linux,运维,服务器)