进程调度算法

本文介绍了实现的六个进程调度算法:FCFS(先来先服务)、SRR(短进程优先)、RR(时间片轮转)、SR(静态优先)、SJF(短进程优先)和QSJF(强占式短进程优先),展示了每个算法的原理与代码实现。
摘要由CSDN通过智能技术生成

实现了六个进程调度算法,即取即用。ps:程序只是为了通过验收,可能仍然存在问题,不保证完全正确。

//主控选择算法
//ConsoleApplication7.cpp
#include <iostream>
#include "SRR.h"
#include"fcfs.h"
#include"RR.h"
#include"SR.h"
#include"SJF.h"
#include"QSJF.h"
using namespace std;
int sel;
int main()
{
	while (true) {
		cout << "请选择算法:";
		cin >> sel;
		switch (sel)
		{
		case 1:
			fcfs();
			break;
		case 2:
			SRR();
			break;
		case 3:
			RR();
			break;
		case 4:
			SR();
			break;
		case 5:
			SJF();
			break;
		case 6:
			QSJF();
			break;
		}
	}
    
}
//进程父结构体头文件
//fa_pro.h
#pragma once
#pragma once
#include <string>
#ifndef fa_pro
#define fa_pro
class process
{
public:
	int num;
	std::string name;
	int start;
	int tim;
	int nst;
};
#endif // !HEAD_h

先来先服务

//定义头文件
//fcfs.h
#pragma once
#include <string>
#ifndef HEAD_h
#define HEAD_h
void fcfs();
#endif // !HEAD_h
//fcfs.cpp
#include<iostream>
#include<queue>
#include"fcfs.h"
#include"fa_pro.h"
using namespace std;
static int num;
static int tim = 0;
static int done;
static int stim;
static double sqtim;
static process ndo;
static struct processt
{
	process t;
	bool operator<(const processt& dir)const {
		return t.start > dir.t.start;//我们以val值为键值进行建堆。自己可以定义
	}
};
static struct processt1
{
	process t;
	bool operator<(const processt& dir)const {
		return t.start+t.tim > dir.t.start+t.tim;//我们以val值为键值进行建堆。自己可以定义
	}
};
static priority_queue<processt> pq;
static priority_queue<processt1> tpq;
void fcfs() {
	cin >> num;
	for (int i = 0; i < num; i++) {
		processt tmp;
		cin >> tmp.t.name >>tmp.t.start>> tmp.t.tim;
		pq.push(tmp);
	}
	done = true;
	do {
		if (!done && ndo.nst + ndo.tim == tim) {
			done = true;
			cout << "时间" << tim << "完成进程" << ndo.name << endl;
			cout << "开始时间" << ndo.nst << "周转时间" << tim - ndo.start << "带权周转时间" << (double)(tim - ndo.start) / (double)(ndo.tim) << endl;
			stim += tim - ndo.start;
			sqtim += (double)(tim - ndo.start) / (double)(ndo.tim);
		}
		if (done&&!pq.empty()&&pq.top().t.start <= tim) {
			done = false;
			ndo = pq.top().t;
			ndo.nst = tim;
			pq.pop();
		}
		tim++;
	} while (!done||!pq.empty());
	cout << "平均周转时间" << stim / (double)num << "平均带权周转时间" << sqtim / (double)num;
	cout << endl;
}

线性优先法

//SRR.h
#pragma once
#include <string>
#ifndef SRR_h
#define SRR_h
void SRR();
#endif // !HEAD_h
//SRR.cpp
#include<iostream>
#include<queue>
#include"SRR.h"
#include"fa_pro.h"
using namespace std;
static int num;
static int tim;
static int a, b;
static int unittim;
static int stim;
static double sqtim;
static class processs {
public:
	process t;
	int nowtim;
	int Round() {
		int k;
		if (nowtim == -1) {
			k=a * (tim - this->t.start);
		}
		else {
			k=a * (this->t.nst - this->t.start) + b * (tim - this->t.nst);
		}
		return k;
	}
};
static class processt
{
public:
	processs t;
	bool operator<(const processt& dir)const {
		return t.t.start > dir.t.t.start;//我们以val值为键值进行建堆。自己可以定义
		
	}
};
static priority_queue<processt> pq;
static queue<processs> waitq;
static queue<processs> q;
void SRR() {
	stim = 0; sqtim = 0;
	tim = 0;
	cout << "进程数";
	cin >> num;
	cout << "增长速率";
	cin >> a >> b;
	cout << "时间片长度";
	cin >> unittim;
	processt tmp[100];
	for (int i = 0; i < num; i++) {
		tmp[i].t.nowtim = -1;
		cin >> tmp[i].t.t.name >> tmp[i].t.t.start >> tmp[i].t.t.tim;
		tmp[i].t.t.num = i;
		pq.push(tmp[i]);
	}
	while (!pq.empty() || !q.empty()||!waitq.empty()) {
		cout << "时间" << tim<<endl;
		while (!pq.empty()&&pq.top().t.t.start == tim) {
			waitq.push(pq.top().t);
			pq.pop();
			cout << "进程" << waitq.back().t.name << "进入等待队列" << endl;
		}
		while (!waitq.empty()&&(q.empty() || q.back().Round() <= waitq.front().Round())) {
			waitq.front().t.nst = tim;
			waitq.front().nowtim = 0;
			q.push(waitq.front());
			waitq.pop();
			cout << "进程" << q.back().t.name << "进入享受队列" << endl;
			cout << "进程" << q.back().t.name << "开始执行" << endl;
		}
		if (!q.empty()&&q.front().nowtim == q.front().t.tim) {
			processs tp = q.front();
			cout << "进程" << tp.t.name << "执行完毕" << endl;
			cout << "开始时间" << tp.t.nst << "周转时间" << tim - tp.t.start << "带权周转时间" << (double)(tim - tp.t.start) / (double)(tp.t.tim) << endl;
			stim += tim - tp.t.start;
			sqtim += (double)(tim - tp.t.start) / (double)(tp.t.tim);
			q.pop();
			if(!q.empty())cout << "进程" << q.front().t.name << "开始执行"<<endl;
		}
		if (!q.empty() && q.front().nowtim!=0&& q.front().nowtim % unittim == 0) {
			processs tp = q.front();
			cout << "进程" << tp.t.name << "轮转"<<endl;
			q.pop();
			q.push(tp);
			cout << "进程" << q.front().t.name << "开始执行" << endl;
		}
		if (!q.empty()) {
			q.front().nowtim++;
		}
		tim++;
	}
	cout << "平均周转时间" << stim / (double)num << "平均带权周转时间" << sqtim / (double)num;
	cout << endl;
}

非抢占式短进程优先

//头文件
//SJF.h
#pragma once
#include <string>
#ifndef SJF_h
#define SJF_h
void SJF();
#endif // !HEAD_h

//SJF.cpp
#include<iostream>
#include<queue>
#include<vector>
#include"fa_pro.h"

using namespace std;
static int num;
static int tim;
static int a, b;
static int unittim;
static int stim, sqtim;
static bool done;
static class priprocess {
public:
	process t;
};
struct cmpt {
	bool operator()(priprocess a, priprocess b) {
		if (a.t.start == b.t.start)
			return a.t.name > b.t.name;///时间相等时按照id升序排列
		return a.t.start > b.t.start;///按照时间升序排列,成为最小堆
	}
};
struct cmpp {
	bool operator()(priprocess a, priprocess b) {
		return a.t.tim > b.t.tim;///按照时间升序排列,成为最小堆
	}
};
static priority_queue<priprocess, vector<priprocess>, cmpt> qt;
static priority_queue<priprocess, vector<priprocess>, cmpp> qp;
void SJF() {
	done = true;
	stim = 0; sqtim = 0;
	tim = 0;
	cout << "进程数";
	cin >> num;
	priprocess tmp[100];
	for (int i = 0; i < num; i++) {
		cin >> tmp[i].t.name >> tmp[i].t.start >> tmp[i].t.tim;
		tmp[i].t.num = i;
		qt.push(tmp[i]);
	}
	while (!qt.empty() || !qp.empty() || !done) {
		cout << "时间" << tim << endl;
		while (!qt.empty() && qt.top().t.start == tim) {
			tmp[0] = qt.top();
			qp.push(tmp[0]);
			qt.pop();
			cout << "进程" << tmp[0].t.name << "进入等待队列" << endl;
		}
		if (done && !qp.empty()) {
			done = false;
			tmp[99] = qp.top();
			tmp[99].t.nst = tim;
			qp.pop();
			cout << "进程" << tmp[99].t.name << "开始执行" << endl;
		}
		if (!done && tmp[99].t.tim + tmp[99].t.nst == tim) {
			done = true;
			cout << "进程" << tmp[99].t.name << "执行完毕" << endl;
			cout << "开始时间" << tmp[99].t.nst << "周转时间" << tim - tmp[99].t.start << "带权周转时间" << (double)(tim - tmp[99].t.start) / (double)(tmp[99].t.tim) << endl;
			stim += tim - tmp[99].t.start;
			sqtim += (double)(tim - tmp[99].t.start) / (double)(tmp[99].t.tim);
		}
		tim++;
	}
	cout << "平均周转时间" << stim / (double)num << "平均带权周转时间" << sqtim / (double)num;
	cout << endl;
}

强占式短进程优先
//QSJF.h
#pragma once
#include <string>
#ifndef QSJF_h
#define QSJF_h
void QSJF();
#endif // !HEAD_h

//QSJF.cpp
#include<iostream>
#include<queue>
#include<vector>
#include"fa_pro.h"

using namespace std;
static int num;
static int tim;
static int a, b;
static int unittim;
static int stim;
static double sqtim;
static string name;
static bool done;
static class priprocess {
public:
	process t;
	int nowtime;
	int stt;
};
struct cmpt {
	bool operator()(priprocess a, priprocess b) {
		if (a.t.start == b.t.start)
			return a.t.name > b.t.name;///时间相等时按照id升序排列
		return a.t.start > b.t.start;///按照时间升序排列,成为最小堆
	}
};
struct cmpp {
	bool operator()(priprocess a, priprocess b) {
		return a.stt > b.stt;///按照时间升序排列,成为最小堆
	}
};
static priority_queue<priprocess, vector<priprocess>, cmpt> qt;
static priority_queue<priprocess, vector<priprocess>, cmpp> qp;
void QSJF() {
	done = true;
	stim = 0; sqtim = 0;
	tim = 0;
	cout << "进程数";
	cin >> num;
	priprocess tmp[100];
	for (int i = 0; i < num; i++) {
		cin >> tmp[i].t.name >> tmp[i].t.start >> tmp[i].t.tim;
		tmp[i].nowtime = 0;
		tmp[i].t.num = i;
		tmp[i].t.nst = -1;
		tmp[i].stt = tmp[i].t.tim;
		qt.push(tmp[i]);
	}
	while (!qt.empty() || !qp.empty() ||!done) {
		cout << "时间" << tim << endl;
		if (!qp.empty()) {
			name = qp.top().t.name;
		}
		while (!qt.empty() && qt.top().t.start == tim) {
			tmp[0] = qt.top();
			qt.pop();
			cout << "进程" << tmp[0].t.name << "进入等待队列" << endl;
			if (!done && tmp[99].t.tim-tmp[99].nowtime > tmp[0].t.tim) {
				cout << "进程" << tmp[99].t.name << "被抢占" << endl;
				qp.push(tmp[99]);
				tmp[99] = tmp[0];
				if (tmp[99].t.nst == -1)tmp[99].t.nst = tim;
				cout << "进程" << tmp[99].t.name << "开始执行" << endl;
			}
			else {
				qp.push(tmp[0]);
			}
		}
		if (done && !qp.empty()) {
			done = false;
			tmp[99] = qp.top();
			tmp[99].t.nst = tim;
			qp.pop();
			cout << "进程" << tmp[99].t.name << "开始执行" << endl;
		}
		if (!done && tmp[99].nowtime == tmp[99].t.tim) {
			done = true;
			cout << "进程" << tmp[99].t.name << "执行完毕" << endl;
			cout << "开始时间" << tmp[99].t.nst << "周转时间" << tim - tmp[99].t.start << "带权周转时间" << (double)(tim - tmp[99].t.start) / (double)(tmp[99].t.tim) << endl;
			stim += tim - tmp[99].t.start;
			sqtim += (double)(tim - tmp[99].t.start) / (double)(tmp[99].t.tim);
			if (!qp.empty()) {
				done = false;
				tmp[99] = qp.top();
				qp.pop();
				tmp[99].t.nst = tim;
				cout << "进程" << tmp[99].t.name << "开始执行" << endl;
			}
		}
		if (!done) {
			tmp[99].nowtime++;
			tmp[99].stt--;
		}
		tim++;
	}
	cout << "平均周转时间" << stim / (double)num << "平均带权周转时间" << sqtim / (double)num;
	cout << endl;
}

时间片轮转法

//RR.h
#pragma once
#pragma once
#include <string>
#ifndef RR_h
#define RR_h
void RR();
#endif // !HEAD_h
//RR.cpp
#include<iostream>
#include<queue>
#include"RR.h"
#include"fa_pro.h"
using namespace std;
static int num;
static int tim;
static int a, b;
static int unittim;
static int stim;
static double sqtim;
static class processs {
public:
	process t;
	int nowtim;
};
static class processt
{
public:
	processs t;
	bool operator<(const processt& dir)const {
		return t.t.start > dir.t.t.start;//我们以val值为键值进行建堆。自己可以定义

	}
};
static priority_queue<processt> pq;
static queue<processs> q;
void RR() {
	stim = 0; sqtim = 0;
	tim = 0;
	cout << "进程数";
	cin >> num; 
	cout << "时间片长度";
	cin >> unittim;
	processt tmp[100];
	for (int i = 0; i < num; i++) {
		tmp[i].t.nowtim = -1;
		cin >> tmp[i].t.t.name >> tmp[i].t.t.start >> tmp[i].t.t.tim;
		tmp[i].t.t.num = i;
		pq.push(tmp[i]);
	}
	while (!pq.empty() || !q.empty()) {
		cout << "时间" << tim << endl;
		while (!pq.empty() && pq.top().t.t.start == tim) {
			q.push(pq.top().t);
			q.back().nowtim = 0;
			pq.pop();
			cout << "进程" << q.back().t.name << "进入队列" << endl;
		}
		if (!q.empty() && q.front().nowtim != 0 && q.front().nowtim % unittim == 0) {
			processs tp = q.front();
			cout << "进程" << tp.t.name << "轮转" << endl;
			q.pop();
			q.push(tp);
			cout << "进程" << q.front().t.name << "开始执行" << endl;
		}
		if (!q.empty() && q.front().nowtim == q.front().t.tim) {
			processs tp = q.front();
			cout << "进程" << tp.t.name << "执行完毕" << endl;
			cout << "开始时间" << tp.t.nst << "周转时间" << tim - tp.t.start << "带权周转时间" << (double)(tim - tp.t.start) / (double)(tp.t.tim) << endl;
			stim += tim - tp.t.start;
			sqtim += (double)(tim - tp.t.start) / (double)(tp.t.tim);
			q.pop();
			if (!q.empty()) {
				cout << "进程" << q.front().t.name << "开始执行" << endl;
			}
		}
		if (!q.empty()) {
			if (q.front().nowtim == 0) {
				q.front().t.nst = tim;
			}
			q.front().nowtim++;

		}
		tim++;
	}
	cout << "平均周转时间" << stim / (double)num << "平均带权周转时间" << sqtim / (double)num;
	cout << endl;
}

静态优先法

//SR.h
#pragma once
#pragma once
#include <string>
#ifndef SR_h
#define SR_h
void SR();
#endif // !HEAD_h
//SR.cpp
#include<iostream>
#include<queue>
#include<vector>
#include"fa_pro.h"

using namespace std;
static int num;
static int tim;
static int a, b;
static int unittim;
static int stim, sqtim;
static bool done;
static class priprocess {
public:
	process t;
	int pri;
};
struct cmpt {
    bool operator()(priprocess a, priprocess b) {
        if (a.t.start == b.t.start)
            return a.t.name > b.t.name;///时间相等时按照id升序排列
        return a.t.start > b.t.start;///按照时间升序排列,成为最小堆
    }
};
struct cmpp {
    bool operator()(priprocess a, priprocess b) {
        return a.pri > b.pri;///按照时间升序排列,成为最小堆
    }
};
static priority_queue<priprocess, vector<priprocess>, cmpt> qt;
static priority_queue<priprocess, vector<priprocess>, cmpp> qp;
void SR() {
	done = true;
	stim = 0; sqtim = 0;
	tim = 0;
	cout << "进程数";
	cin >> num;
	priprocess tmp[100];
	for (int i = 0; i < num; i++) {
		cin >> tmp[i].t.name >> tmp[i].t.start >> tmp[i].t.tim >> tmp[i].pri;
		tmp[i].t.num = i;
		qt.push(tmp[i]);
	}
	while (!qt.empty() || !qp.empty()||!done) {
		cout << "时间" << tim << endl;
		while (!qt.empty() && qt.top().t.start == tim) {
			tmp[0]=qt.top();
			qp.push(tmp[0]);
			qt.pop();
			cout << "进程" << tmp[0].t.name << "进入等待队列" << endl;
		}
		if (done&&!qp.empty()) {
			done = false;
			tmp[99] = qp.top();
			tmp[99].t.nst = tim;
			qp.pop();
			cout << "进程" << tmp[99].t.name << "开始执行" << endl;
		}
		if (!done && tmp[99].t.tim + tmp[99].t.nst == tim) {
			done = true;
			cout << "进程" << tmp[99].t.name << "执行完毕" << endl;
			cout << "开始时间" << tmp[99].t.nst << "周转时间" << tim - tmp[99].t.start << "带权周转时间" << (double)(tim - tmp[99].t.start) / (double)(tmp[99].t.tim) << endl;
			stim += tim - tmp[99].t.start;
			sqtim += (double)(tim - tmp[99].t.start) / (double)(tmp[99].t.tim);
		}
		tim++;
	}
	cout << "平均周转时间" << stim / (double)num << "平均带权周转时间" << sqtim / (double)num;
	cout << endl;
}
​```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值