(操作系统)C++ 先来先服务调度

该文介绍了一段C++代码,用于模拟和计算进程调度中的先来先服务(FCFS)算法。程序读取进程的到达时间和运行时间,然后按照到达时间顺序进行调度,输出每个进程的开始时间、结束时间、周转时间和带权周转时间,并计算平均值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题描述】编程实现进程调度中的先来先服务调度算法。(括号内为相应的数字说明,不需要实现)
【输入形式】
【输出形式】
【样例输入】

3(表示进程个数)

process1   0    10(分别表示进程名称,到达时间和运行时间)

process2    2    5

process3    1    4   
【样例输出】(按照调度顺序输出,下列各列分别表示进程名,到达时间,开始时间,运行时间,完成时间,周转时间,带权周转时间)

(每列之间用一个空格分割)

process1 0 0 10 10 10 1

process3 1 10 4 14 13 3.25

process2 2 14 5 19 17 3.4

13.3333(表示平均周转时间)

2.55(表示平均带权周转时间)

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class PCB{
	public:
		PCB(){
			cin>>pname>>arrivetime>>runtime;
			begintime=0;
			endtime=0;
			turntime=0;
			weight_turn=0;
		}
		void display(){
			cout<<pname<<" "<<arrivetime<<" "<<begintime<<" "<<runtime<<" "<<endtime<<" "<<turntime<<" "<<weight_turn<<endl;
		}//分别表示进程名,到达时间,开始时间,运行时间,完成时间,周转时间,带权周转时间)
		int get_arrivetime(){
			return arrivetime;
		}
		int get_runtime(){
			return runtime;
		}
		int get_begintime(){
			return begintime;
		}
		int get_endtime(){
			return endtime;
		}
		int get_turntime(){
			return turntime;
		}
		float get_weighturn(){
			return weight_turn;
		}
		void setbegintime(int begintime){
			this->begintime=begintime;
		}
		void setendtime(int endtime){
			this->endtime=endtime;
		}
		void setturntime(int turntime){
			this->turntime=turntime;
		}
		void setweight(float weight_turn){
			this->weight_turn=weight_turn;
		}
		private:
			string pname;
			int arrivetime;//到达时间
			int begintime;//开始时间
			int runtime;//运行时间
			int endtime;//完成时间
			int turntime;//周转时间
			float weight_turn;//带权周转时间
};
void FCFS(vector<PCB>p[]){
	vector<PCB>::iterator it=p->begin();
	it->setbegintime(it->get_arrivetime());
	it->setendtime(it->get_begintime()+it->get_runtime());
	it->setturntime(it->get_endtime()-it->get_arrivetime());
	it->setweight(it->get_turntime()/it->get_runtime());
	for(it++;it!=p->end();it++){
		it->setbegintime((it-1)->get_endtime());
		it->setendtime(it->get_begintime()+it->get_runtime());
		it->setturntime(it->get_endtime()-it->get_arrivetime());
		it->setweight(float(it->get_turntime())/it->get_runtime());
	}
}
void Avg_turn(vector<PCB>*p,float &avgturn,float &weighturn){
	for(vector<PCB>::iterator it=p->begin();it!=p->end();it++){
		avgturn+=it->get_turntime();
		weighturn+=it->get_weighturn();
	}
	avgturn/=p->size();
	weighturn/=p->size();
}
bool compareByArr(PCB p1,PCB p2){
	int t1=p1.get_arrivetime();
	int t2=p2.get_arrivetime();
	return t1<t2;
}
int main()
{
	vector<PCB>*p;
	int n;
	float avgturn=0.0,weighturn=0.0;
	cin>>n;//输入进程数
	p=new vector<PCB>[n];
	for(int i=0;i<n;i++){
		PCB temp;
		p->push_back(temp);
	}
	sort(p->begin(),p->end(),compareByArr);//按照进入时间排序
	FCFS(p);
	for(vector<PCB>::iterator it=p->begin();it!=p->end();it++)
	(*it).display();
	Avg_turn(p,avgturn,weighturn);
	cout<<avgturn<<endl;//输出平均周转时间
	cout<<weighturn<<endl;//输出平均带权周转时间
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值