FCFS先来先服务算法C++

FCFS简单的讲就是先到达的先服务,比如A进程3秒到达,B进程2秒到达的话,那么就让B一直执行到满足它所需的服务时间。它是非抢占式的。
**

以下是部分代码讲解:

1.定义一个PCB的结构体,结构体就没什么好说的了

struct PCB {
	string Name;//进程名字
	double arriveTime;//进程到达时间
	double serviceTime;//进程需要服务时间
	double endTime;//已服务时间
	double finishTime;//完成时间点记录
	double RunTime;//进程周转时间
	string State;//进程状态
};

2.数据的录入并排序
①先写好一个排序规则,因为先来先服务,不用考虑服务时间,只考虑先到达的时间,按从小到大的规则排序

bool myCompare_ArriveTime(PCB P1, PCB P2) {
	//按到达时间排序
	return P1.arriveTime < P2.arriveTime;
}

②创建PCB,初始化PCB并把PCB数据放入vector中,并按到达从小到达的方式进行排序,排序完后放入queue中,相当于一个就绪队列

vector<PCB>v;
	cout << "请输入进程个数:";
	int sum;
	int increatment = 0;
	cin >> sum;
	increatment = sum - 1;
	PCB p;
	queue<PCB>readyList;
	for (int i = 1; i <= sum; i++) {
		cout << "请输入第 " << i << " 个进程名字:";
		cin >> p.Name;
		cout << "请输入第 " << i << " 个进程到达时间:";
		cin >> p.arriveTime;
		cout << "请输入第 " << i << " 个进程需要服务的时间:";
		cin >> p.serviceTime;
		p.State = "就绪状态";
		p.endTime = 0;
		p.finishTime = 0;
		p.RunTime = 0;
		//把进程放入容器中
		v.push_back(p);
	}
	//按进程到达的时间排序
	sort(v.begin(), v.end(), myCompare_ArriveTime);
	//展示当前情况
	showData_Vector(v);

	//把数据放入队列中
	for (vector<PCB>::iterator it = v.begin(); it != v.end(); it++) {
		readyList.push(*it);
	}

3.算法执行流程
①tempTime是一个时间片的记录,记录第几秒有进程执行,第几秒有进程到达等。
②如果tempTime的时间记录刚好跟就绪队列的第一个PCB到达的时间相同就让它一直执行,直到服务时间满足就让它弹出放到另一个queue中。
③因为没有设立别的队列来作为运行队列,所以这里执行完第一个后要去扫描队列在当前秒有哪些进程进来了,如果有那么可以直接执行第一个数据,如果没有那么就要等时间片增加到第一个PCB的到达时间才能执行。
④直到就绪队列为空那么所有进程便执行完毕。

int tempTime = 0;
	queue<PCB>endlist;
	PCB temp;
	//获取第一个数据前的操作:
	while (1) {
		if (tempTime == v.front().arriveTime) {
			cout << "-------------------------------------------------------" << endl;
			cout << "第" << tempTime << "秒" << readyList.front().Name << "进程到达" << endl;
			cout << "-------------------------------------------------------" << endl;
			break;
		}
		else {
			cout << "第" << tempTime << "秒没有进程被执行" << endl;
			tempTime++;
		}
	}

	//拿到第一个开始执行下一步操作:
	while (readyList.size() != 0)
	{
		if (tempTime < readyList.front().arriveTime) {
			tempTime++;
			if (tempTime == readyList.front().arriveTime) {
				cout << "-------------------------------------------------------" << endl;
				cout << "第" << tempTime << "秒" << readyList.front().Name << "进程到达" << endl;
				cout << "-------------------------------------------------------" << endl;
			}
		}

		if (readyList.front().serviceTime != 0) {
			//如果时间还需服务时间不为0则做相关的加或者减
			cout << "第" << tempTime << "秒 -" << readyList.front().Name << "- 正在执行" << endl;
			tempTime++;
			readyList.front().endTime++;
			readyList.front().serviceTime--;
		}

		if (readyList.front().serviceTime == 0) {
			//如果第一个数据的所需服务时间为0了那么这个数据所需执行的时间够了则交给下一个程序
			cout << "-------------------------------------------------------" << endl;
			cout << "第" << tempTime << "秒 -" << readyList.front().Name << "- 已执行完毕" << endl;
			cout << "-------------------------------------------------------" << endl;
			//把结束时间标记上
			readyList.front().finishTime = tempTime;
			//把状态进行修改
			readyList.front().State = "执行完毕";
			//计算周转时间
			readyList.front().RunTime = tempTime - readyList.front().arriveTime;
			//放入另一个队列中保存起来
			endlist.push(readyList.front());
			//弹出第一个数据让下一个开始执行
			readyList.pop();
			//满足这个条件的话就输出进程到达的时间
			if (readyList.size() != 0) {
				if (tempTime >= readyList.front().arriveTime) {
					cout << "-------------------------------------------------------" << endl;
					cout << "第" << readyList.front().arriveTime << "秒" << readyList.front().Name << "进程到达" << endl;
					cout << "-------------------------------------------------------" << endl;
				}
			}
		}
	}

以上便是一些对本次代码需要理解的一些要点
以下是全代码:

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct PCB {
	string Name;//进程名字
	double arriveTime;//进程到达时间
	double serviceTime;//进程需要服务时间
	double endTime;//已服务时间
	double finishTime;//完成时间点记录
	double RunTime;//进程周转时间
	string State;//进程状态

};

bool myCompare_ArriveTime(PCB P1, PCB P2) {
	//按到达时间排序
	return P1.arriveTime < P2.arriveTime;

}


void showData_Vector(vector<PCB>& v) {
	cout << "-------------------------------------------------当前进程情况-------------------------------------------------" << endl;
	cout << "进程名\t" << "进程到达时间\t" << " 进程服务时间\t" << "进程已服务时间\t" << " 进程完成时间\t" << " 进程周转时间\t" << "进程当前状态\t" << endl;
	for_each(v.begin(), v.end(), [](PCB p) {cout << p.Name << "\t    " << p.arriveTime
		<< "\t\t\t" << p.serviceTime << "\t\t" << p.endTime << "\t\t" << p.finishTime << "\t\t" << p.RunTime << "      \t" << p.State << endl; });
	cout << "--------------------------------------------------------------------------------------------------------------" << endl;
}

void showData(queue<PCB>& q) {
	cout << "-------------------------------------------------当前进程情况-------------------------------------------------" << endl;
	cout << "进程名\t" << "进程到达时间\t" << " 进程服务时间\t" << "进程已服务时间\t" << " 进程完成时间\t" << " 进程周转时间\t" << "进程当前状态\t" << endl;
	while (q.size() != 0) {
		cout << q.front().Name <<"\t    " << q.front().arriveTime <<  "\t\t\t"
			<< q.front().serviceTime << "\t\t"  << q.front().endTime <<"\t\t"
			<< q.front().finishTime << "\t\t" << q.front().RunTime << "      \t" <<q.front().State << endl;
		q.pop();
	}
	
}
/*
	先来先服务算法
*/
void FCFS() {
	vector<PCB>v;
	cout << "请输入进程个数:";
	int sum;
	int increatment = 0;
	cin >> sum;
	increatment = sum - 1;
	PCB p;
	queue<PCB>readyList;
	for (int i = 1; i <= sum; i++) {
		cout << "请输入第 " << i << " 个进程名字:";
		cin >> p.Name;
		cout << "请输入第 " << i << " 个进程到达时间:";
		cin >> p.arriveTime;
		cout << "请输入第 " << i << " 个进程需要服务的时间:";
		cin >> p.serviceTime;
		p.State = "就绪状态";
		p.endTime = 0;
		p.finishTime = 0;
		p.RunTime = 0;
		//把进程放入容器中
		v.push_back(p);
	}
	//按进程到达的时间排序
	sort(v.begin(), v.end(), myCompare_ArriveTime);
	//展示当前情况
	showData_Vector(v);

	//把数据放入队列中
	for (vector<PCB>::iterator it = v.begin(); it != v.end(); it++) {
		readyList.push(*it);
	}

	int tempTime = 0;
	queue<PCB>endlist;
	PCB temp;
	//获取第一个数据前的操作:
	while (1) {
		if (tempTime == v.front().arriveTime) {
			cout << "-------------------------------------------------------" << endl;
			cout << "第" << tempTime << "秒" << readyList.front().Name << "进程到达" << endl;
			cout << "-------------------------------------------------------" << endl;
			break;
		}
		else {
			cout << "第" << tempTime << "秒没有进程被执行" << endl;
			tempTime++;
		}
	}

	//拿到第一个开始执行下一步操作:
	while (readyList.size() != 0)
	{
		if (tempTime < readyList.front().arriveTime) {
			tempTime++;
			if (tempTime == readyList.front().arriveTime) {
				cout << "-------------------------------------------------------" << endl;
				cout << "第" << tempTime << "秒" << readyList.front().Name << "进程到达" << endl;
				cout << "-------------------------------------------------------" << endl;
			}
		}

		if (readyList.front().serviceTime != 0) {
			//如果时间还需服务时间不为0则做相关的加或者减
			cout << "第" << tempTime << "秒 -" << readyList.front().Name << "- 正在执行" << endl;
			tempTime++;
			readyList.front().endTime++;
			readyList.front().serviceTime--;
		}

		if (readyList.front().serviceTime == 0) {
			//如果第一个数据的所需服务时间为0了那么这个数据所需执行的时间够了则交给下一个程序
			cout << "-------------------------------------------------------" << endl;
			cout << "第" << tempTime << "秒 -" << readyList.front().Name << "- 已执行完毕" << endl;
			cout << "-------------------------------------------------------" << endl;
			//把结束时间标记上
			readyList.front().finishTime = tempTime;
			//把状态进行修改
			readyList.front().State = "执行完毕";
			//计算周转时间
			readyList.front().RunTime = tempTime - readyList.front().arriveTime;
			//放入另一个队列中保存起来
			endlist.push(readyList.front());
			//弹出第一个数据让下一个开始执行
			readyList.pop();
			//满足这个条件的话就输出进程到达的时间
			if (readyList.size() != 0) {
				if (tempTime >= readyList.front().arriveTime) {
					cout << "-------------------------------------------------------" << endl;
					cout << "第" << readyList.front().arriveTime << "秒" << readyList.front().Name << "进程到达" << endl;
					cout << "-------------------------------------------------------" << endl;
				}
			}
		}
	}
	queue<PCB>result;
	result = endlist;
	showData(endlist);
	//平均周转时间
	double avgTime = 0;
	double sumTime = 0;
	int size = result.size();
	while (result.size() != 0) {
		sumTime += result.front().RunTime;
		result.pop();
	}
	avgTime = sumTime / size;
	cout << "平均周转时间:" << avgTime << endl;
	cout << "--------------------------------------------------------------------------------------------------------------" << endl;
}

int main() {
	FCFS();
	return 0;
}

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
先来先服务算法 #include "stdio.h" #include #define max 100 #define pfree 0 /*process end*/ #define running 1 /*process running status*/ #define aready 2 /*process aready status */ #define blocking 3 /*process aready blocking status*/ typedef struct node { char name; int status; int ax,bx,cx,dx; int pc; int psw; struct node *next; /*pcb define*/ }pcb; pcb *createprocess(pcb *head) { pcb *p,*q; int a,b,c,d,m,n; char ID; q=NULL; printf("input the first seven status pcb:"); scanf("\n%c%d%d%d%d%d%d",&ID,&a,&b,&c,&d,&m,&n); while(1) { p=(pcb*)malloc(sizeof(pcb)); p->name=ID; p->ax=a; p->bx=b; p->cx=c; p->dx=d; p->pc=m; p->psw=n; p->status=aready; if(head==NULL) head=p; else q->next=p; q=p; printf("input the next seven status pcb: "); scanf("\n%c",&ID); if (ID == '*') break; scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n); } if(q!=NULL) q->next=NULL; q=head; while(q) { printf("\n process name. status.ax. bx. cx. dx. pc. psw.\n "); printf("%10c%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw); q=q->next; } return head;/*createprocess end*/ } void processfcfs(pcb *head) /*use fifo */ { pcb *p; p=head; printf("\n the process use fcfs method.\n"); printf("running the frist process:\n"); while(p!=NULL) { p->status=running; printf("\nprocess name status. ax. bx. cx. dx. pc. psw."); printf("\n%10c%5d%8d%5d%5d%5d%5d%5d",p->name,p->status,p->ax,p->bx,p->cx,p->dx,p->pc,p->psw); /*check process running status */ p->status=0; p=p->next; } printf("\n检查进程是否结束:"); p=head; while(p) { printf("\n%3c%3d",p->name,p->status); p=p->next; } printf("\ngame is over!\n"); } main() { pcb *head; head=NULL; head=createprocess(head); processfcfs(head); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值