#include<stdio.h>
#include<string.h>
#include<iostream.h>
const int block_time=10; //定义时间片的长度为10秒
const int MAXPCB=100; //定义最大进程数
//定义进程结构体
typedef struct node{
char name[20];
int status;
int time;
int privilege;
int finished;
int wait_time;
}pcb;
pcb pcbs[MAXPCB];
int quantity;
//初始化函数
void initial(){
int i;
for(i=0;i<MAXPCB;i++){
strcpy(pcbs[i].name,"");
pcbs[i].status=0;
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantity=0;
}
//读数据函数
int readData(){
FILE *fp;
char fname[20];
int i;
cout<<"请输入进程流文件名:";
cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
cout<<"错误,文件打不开,请检查文件名"<<endl;
}
else{
while(!feof(fp)){ fscanf(fp,"%s%d%d%d",pcbs[quantity].name,&pcbs[quantity].status,&pcbs[quantity].time,&pcbs[quantity].privilege);
quantity++;
}
//输出所读入的数据
cout<<"输出所读入的数据"<<endl;
cout<<"进程名 进程状态
所需时间
优先数"<<endl;
for(i=0;i<quantity;i++){
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].status<<" "<<pcbs[i].time<<"
"<<pcbs[i].privilege<<endl;
}
return(1);
}
return(0);
}
//重置数据,以供另一个算法使用
void init()
{
int i;
for(i=0;i<MAXPCB;i++){
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
}
//先进先出算法
void FIFO()
{
int i,j;
int total;//输出FIFO算法执行流
cout<<endl<<"-------------------------------------------------------------"<<endl;
cout<<"FIFO算法执行流:"<<endl;
cout<<"进程名
等待时间"<<endl;
for(i=0;i<quantity;i++){
cout<<"
"<<pcbs[i].name<<"
"<<pcbs[i].wait_time<<endl;
for(j=i+1;j<quantity;j++){
pcbs[j].wait_time+=pcbs[i].time;
}
}
total=0;
for(i=0;i<quantity;i++){
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<"
平均等待时间:"<<total/quantity<<endl;
}
//优先数调度算法
void privilege()
{
int i,j,p;
int passed_time=0;
int total;
int queue[MAXPCB];
int current_privilege=1000;
for(i=0;i<quantity;i++){
current_privilege=1000;
for(j=0;j<quantity;j++){
if((pcbs[j].finished==0)&&(pcbs[j].privilege<current_privilege)){
p=j;
current_privilege=pcbs[j].privilege;
}
}
queue[i]=p;
pcbs[p].finished=1;
pcbs[p].wait_time+=passed_time;
passed_time+=pcbs[p].time;
}
//输出优先数调度执行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"优先数调度执行流:"<<endl;
cout<<"进程名
等待时间"<<endl;
for(i=0;i<quantity;i++){
cout<<"
"<<pcbs[queue[i]].name<<"
"<<pcbs[queue[i]].wait_time<<endl;
}
total=0;
for(i=0;i<quantity;i++){
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<"
平均等待时间:"<<total/quantity<<endl;
}
//显示版权信息函数
void version()
{
cout<<endl<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 进程调度模拟系统 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃
(c)All Right Reserved
Neo
┃"<<endl;
cout<<" ┃ sony006@ ┃"<<endl;
cout<<" ┃ version 2004
build 1122
┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}
//主函数
void main()
{
int flag;
version();
initial();
flag=readData();
if(flag==1){
FIFO();
init();
privilege();
init();
timer();
}
}
拿去看看吧