操作系统 先来先服务算法 (C++实现 操作系统实验)

先来先服务算法思想:

进程按到达顺序执行,先来的进程只要开始执行就会执行到结束,这在过程中到达的进程需要等待。

代码如下:

#include<iostream>
#include<queue>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
struct shuxing{
	char name;   // 进程名字
	double arriveTime;  // 进程到达时间
	double serviceTime;  // 进程服务时间
	friend bool operator>(shuxing n1,shuxing n2){ // 自定义比较函数 
		 return n1.arriveTime > n2.arriveTime;
	}
};
 
priority_queue<shuxing, vector<shuxing>, greater<shuxing> > jincheng;
 // priority_queue greater: from small to large

vector<char> seq; // 存进程调度的顺序 
vector<double> ks; // 存开始时间 
vector<double> wc;	//存完成时间 
vector<double> zz;	// 存周转时间 
vector<double> dqzz;	// 存带权周转时间 

double avezz, avedqzz;  // 平均周转时间和平均带权周转时间 

int n;  // 进程的数量 

int main() {	
	
	cout << "请输入进程的数量:" << "\n";
	cin >> n;
	
	char x;
	double y, z;
	for(int i = 0; i < n; i ++) {
		cout << "请输入第" << i + 1 << "个进程的名字,到达时间,服务时间 <例如:A,2,1>:" << "\n";
		cin >> x >> y >> z;
		shuxing temp;
		temp.name = x;
		temp.arriveTime = y;
		temp.serviceTime = z;
		jincheng.push(temp);
	}
	
	double start = -1; 
	double finish = -1;
	
	while(!jincheng.empty()) {  // 优先队列模拟 
		shuxing temp = jincheng.top();
		jincheng.pop();
		seq.push_back(temp.name); // 存调度的进程 
		start = max(finish, temp.arriveTime);
		ks.push_back(start); // 存开始时间 
		finish = start + temp.serviceTime;
		wc.push_back(finish);	//存完成时间 
		zz.push_back(finish - temp.arriveTime); // 存周转时间 
		avezz += (finish - temp.arriveTime);	// 平均周转时间
		dqzz.push_back((finish - temp.arriveTime) / temp.serviceTime); // 存带权周转时间 
		avedqzz += ((finish - temp.arriveTime) / temp.serviceTime);  //平均带权周转时间  
	}	
	
	cout << "\n执行顺序:\t";	
	for(int i = 0; i < n; i ++)
		cout << seq[i] << "\t"; 
		cout << "\n";
	
	cout << "\n开始时间:\t";
	for(int i = 0; i < n; i ++)
		cout << ks[i] << "\t";
		cout << "\n";
	 
	cout << "\n完成时间:\t";
	for(int i = 0; i < n; i ++)
		cout << wc[i] << "\t";
		cout << "\n";	

	cout << "\n周转时间:\t";
	for(int i = 0; i < n; i ++) 
		cout << zz[i] << "\t"; 
		cout << "\n";
		
	cout << "\n带权周转时间:\t";
	for(int i = 0; i < n; i ++)
		printf("%.2lf\t", dqzz[i]);
		cout << "\n";
		
	cout << "\n平均周转时间:";
	printf("\t\t%.2lf\n", avezz / n);
	
	cout << "\n平均带权周转时间:";
	printf("\t%.2lf", avedqzz / n);

	return 0;
} 

  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
先来先服务算法 #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); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值