FCFS进程调度算法

FCFS进程调度算法,第1张

FCFS进程调度算法

运行结果:

#include
#include
#include
#include
#include
using namespace std;

class PCB{
public:
	string name;// 进程名称
	int priority;//进程优先级
	double arrive_time;//到达时间
	double service_time;//服务时间
	string state;//进程状态 run:运行 finish:完成

	double finish_time;//完成时间
	double turnround_time;//周转时间
	double w_turnround_time;//带权周转时间
	double aver_turnround_time;// 平均周转时间
	double aver_w_turnround_time;// 平均带权周转时间
};

int n_process;//进程数量
vector PCB_vector;//pcb数组 负责排序,,给queue传参数
queue PCB_queue;//pcb队列 核心算法

void Pri(){
	cout << endl << "*******************************" << endl;
	cout << "进程名   ";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.name;
	}
	cout << endl << "到达时间:";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.arrive_time;
	}
	cout << endl << "服务时间:";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.service_time;
	}
	cout << endl << "*******************************" << endl;
}

void Pri(int time_slice){
	cout << endl << "********************" << "时间片: " << time_slice << " ********************";
	cout << endl << endl;
	cout << "进程名   ";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.name;
	}
	cout << endl << "完成时间:";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.finish_time;
	}
	cout << endl << "周转时间:";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.turnround_time;
	}
	cout << endl << "带权周转时间:";
	for (int i = 0; i < PCB_vector.size(); i++){
		PCB t_pcb = PCB_vector.at(i);
		cout << " " << t_pcb.w_turnround_time;
	}
	cout << endl << "****************************************" << endl;
}

bool static mysort(PCB a, PCB b){
	return a.arrive_time < b.arrive_time;
}

int main(){

	cout << "请输入进程数量: ";
	cin >> n_process;
	cout << "请输入进程(name,arrive_time,service_time)" << endl;

	PCB t_pcb;
	for (int i = 0; i < n_process; i++){
		cin >> t_pcb.name >> t_pcb.arrive_time >> t_pcb.service_time;
		t_pcb.aver_turnround_time = 0;//初始化
		t_pcb.aver_w_turnround_time = 0;
		t_pcb.finish_time = 0;
		t_pcb.turnround_time = 0;
		t_pcb.w_turnround_time = 0;
		PCB_queue.push(t_pcb);
		PCB_vector.push_back(t_pcb);
	}

	Pri();

	int runtime = 0;//运行时间
	PCB_vector.clear();//清空数组
	while (!PCB_queue.empty()){
		PCB c_pcb = PCB_queue.front();
		runtime += c_pcb.service_time;
		c_pcb.finish_time = runtime;
		c_pcb.turnround_time = c_pcb.finish_time - c_pcb.arrive_time;
		c_pcb.w_turnround_time = c_pcb.turnround_time / c_pcb.finish_time;
		PCB_vector.push_back(c_pcb);
		PCB_queue.pop();//出队
	}

	Pri(1);//输出该时间片结果

	return 0;
}

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存