#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
typedef struct PCB
{
string Pro_name;
PCB *next;
float R_time;//估计运行时间
float A_time;//到达时间
float S_time;//开始时间
float E_time;//程序终止时间
float total_time;//进程周转时间
double W_time;//带权周转时间
float con_time;//进程仍需运行时间
float n;//进程运行次数
char states;//进程状态
}PCB;
#define MAX_NUM 15
int Pro_num;
float total;//记录总时间;
double weight;//记录所有进程带权周转时间
PCB *CreatePCB(PCB *head);//创建进程PCB
void sort(PCB *head);//按照到达顺序排序
void deal(PCB *head);//对就绪队列中进程进程大体上计算,比如平均周转时间,加权周转时间的计算
void fcfs(PCB *head);//先来先服务
void P(PCB *head);//打印出每执行一次就绪队列的结果
PCB *CreatePCB(PCB *head)
{
PCB *p1,*p2;
p1=p2=new PCB;
head=p1;
cout<<"请输入进程数目:";
cin>>Pro_num;
for(int i=0;i<Pro_num;i++)
{
p2=p1;
p1=new PCB;
p1->next=NULL;
cout<<"请输入第"<<i+1<<"个进程信息:";
cout<<"进程名 运行时间 到达时间";
cin>>p1->Pro_name;
cin>>p1->R_time;
cin>>p1->A_time;
p1->states='R';
p1->con_time=p1->A_time;
p2->next=p1;
}
return head;
}//创建进程队列
void sort(PCB *head)
{
PCB *p,*q,*r,*s;
if(head->next!=NULL)
{
p=head->next->next;
head->next->next=NULL;
}
while(p)
{
q=p;
p=p->next;
r=head;
s=head->next;
while(s&&s->A_time<=q->A_time)
{
r=s;
s=s->next;
}
r->next=q;
q->next=s;
}
}
void deal(PCB *head)
{
sort(head);
PCB *p,*q;
q=head->next;
q->S_time=q->A_time;
q->E_time=q->A_time+q->R_time;
q->total_time=q->E_time-q->A_time;
q->W_time=q->total_time/(double)q->R_time;
total+=q->total_time;
weight+=q->W_time;
p=q->next;
while(p!=NULL)
{
p->S_time=q->E_time;
p->E_time=p->S_time+p->R_time;
p->total_time=p->E_time-p->A_time;
p->W_time=p->total_time/(double)p->R_time;
total+=p->total_time;
weight+=p->W_time;
q=p;
p=p->next;
}
}
void fcfs(PCB *head)
{
deal(head);
PCB *p,*q,*s;
p=head->next;
while(p!=NULL)
{
cout<<"--"<<p->Pro_name;
p=p->next;
}
cout<<endl;
cout<<"进程名 提交时间 开始时间 结束时间 周转时间 带权周转时间\n";
s=head->next;
while(s!=NULL)
{
cout<<setw(4)<<s->Pro_name<<setw(7)<<s->A_time<<setw(10)<<s->S_time<<setw(11)<<s->E_time<<setw(10)<<s->total_time<<setw(10)<<s->W_time<<endl;
s=s->next;
}
cout<<endl;
cout<<"平均周转时间"<<total/(double)Pro_num<<endl;
cout<<"平均带权周转时间"<<weight/(double)Pro_num<<endl;
total=0;
weight=0;
}
void P(PCB *head)
{
PCB *p,*q;
p=head;
q=head->next;
for(int i;i<10;i++){
while(p->con_time!=0)
{
p->con_time-=1;
}
p->states='C';
cout<<"进程名 提交时间 开始时间 剩余时间 进程状态\n";
while(q!=NULL)
{
// cout<<"进程名 提交时间 开始时间 剩余时间 进程状态\n";
cout<<setw(4)<<q->Pro_name<<setw(7)<<q->A_time<<setw(10)<<q->S_time<<setw(10)<<q->con_time<<setw(11)<<q->states<<endl;
q=q->next;
}
q=head->next;
p=p->next;
}
}
int main()
{
PCB *head=NULL;
head=CreatePCB(head);
fcfs(head);
P(head);
}
FCFS优化.rar_先来先服务算法c++实现_算法
版权申诉
100 浏览量
2022-09-24
22:09:13
上传
评论
收藏 467KB RAR 举报
邓凌佳
- 粉丝: 64
- 资源: 1万+
最新资源
- 基于JAVA-JSP的现代数字化电子政务网管理系统论文-20240403更新
- 西部钻探工程有限公司安全环保问责管理办法.doc
- Using Model-Based Design to Develop SOA Applications
- PHP Decrypt跨平台解密PHP源码的扩展
- SimHei MAC字体资源
- 2024全国行政区域编码
- 数据源-数据可视化(七):Pandas香港酒店数据高级分析,涉及相关系数,协方差,数据离散化,透视表等精美可视化展示
- linux常用命令大全.doc
- 格拉斯哥大学空缺职位申请详细介绍Applicant Guide.pdf
- mmexport1702953347189.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0