//设计一个有 N个进程并发运行的进程调度程序,进程调度算法 :最高优先数优先的调度算法 。本实验模拟在单处理机环境下处理机的调度,了解处理机调度的过程。
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#define MAX_PROGRAM 50 //系统可承受最大进程数量
char pname[MAX_PROGRAM][5]={"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10",
"P11","P12","P13","P14","P15","P16","P17","P18","P19","P20",
"P21","P22","P23","P24","P25","P26","P27","P28","P29","P30",
"P31","P32","P33","P34","P35","P36","P37","P38","P39","P40",
"P41","P42","P43","P44","P45","P46","P47","P48","P49","P50"};
struct PCB //进程控制块
{
char name[5]; //进程名
int m_time; //要求运行时间
int r_time; //已经运行时间
int a_time; //总运行时间
int value; //优先数
char condition; //状态
struct PCB *next; //指针
}*head,*p,*q,*run,System_PCB[MAX_PROGRAM],center,*headc;
int NO_OF_PROGRAM; //系统实际运行进程数量,由用户从键盘输入
char iname[5];
void Initiate() //初始化
{
int i;
char ch;
cout<<"请选择初始化信息方式(y--键盘输入n--默认值):";
cin>>ch;
for(i=0;i<NO_OF_PROGRAM;i++)
{
if(ch=='y')
{
cout<<"第"<<i+1<<"个进程名:";
cin>>iname;
strcpy(System_PCB[i].name,iname);
cout<<"运行时间:";
cin>>System_PCB[i].m_time ;
cout<<"优先数:";
cin>>System_PCB[i].value ;
System_PCB[i].a_time =0;
System_PCB[i].r_time =0;
}
if(ch=='n')
{
strcpy(System_PCB[i].name ,pname[i]);
System_PCB[i].m_time =(int)(rand()) % 10 +1;
System_PCB[i].value =(int)(rand()) % 10 +1;
System_PCB[i].a_time =0;
System_PCB[i].r_time =0;
}
System_PCB[i].condition ='R';
}
}
void Sortness() //按优先数高-->低排序
{
int i,j;
for(i=0;i<NO_OF_PROGRAM;i++)
{
for(j=i;j<NO_OF_PROGRAM;j++)
{
if(System_PCB[i].value < System_PCB[j].value )
{
center=System_PCB[i];
System_PCB[i]=System_PCB[j];
System_PCB[j]=center;
}
}
}
head=(struct PCB*) malloc (sizeof(struct PCB));
headc=(struct PCB*) malloc (sizeof(struct PCB));
headc->next =NULL;
q=head;
for(i=0;i<NO_OF_PROGRAM;i++)
{
p=(struct PCB*) malloc (sizeof(struct PCB));
strcpy(p->name ,System_PCB[i].name );
p->m_time =System_PCB[i].m_time ;
p->condition =System_PCB[i].condition ;
p->value =System_PCB[i].value ;
p->a_time =System_PCB[i].a_time ;
p->r_time =System_PCB[i].r_time ;
p->next =NULL;
q->next =p;
q=p;
}
}
void Outhead() //输出就绪队列信息
{
q=head->next;
if(q!=NULL)
{
cout<<"就绪队列信息:"<<endl;
cout<<"进程名 优先数 已运行时间 还需时间 总运行时间 进程状态"<<endl;
while(q!=NULL)
{
cout<<q->name<<" ";
cout<<q->value<<" ";
cout<<q->r_time<<" ";
cout<<q->m_time <<" ";
cout<<q->a_time <<" ";
cout<<q->condition<<endl;
q=q->next;
}
cout<<"-----------------------------------------------------------------"<<endl;
}
}
void Outheadc() //输出就绪队列信息
{
q=headc->next;
if(q!=NULL)
{
cout<<"完成队列信息:"<<endl;
cout<<"进程名 优先数 已运行时间 还需时间 总运行时间 进程状态"<<endl;
while(q!=NULL)
{
cout<<q->name<<" ";
cout<<q->value<<" ";
cout<<q->r_time<<" ";
cout<<q->m_time <<" ";
cout<<q->a_time <<" ";
cout<<q->condition<<endl;
q=q->next;
}
cout<<"-----------------------------------------------------------------"<<endl;
}
}
void Insert()
{
int ok=0;
q=head;
p=head->next ;
while(p!=NULL&&ok==0)
{
if(run->value >p->value )
{
q->next =run;
run->next =p;
q=q->next ;
ok=1;
}
else
{
q=p;
p=p->next ;
}
}
if(p==NULL&&ok==0)
q->next =run;
if(p!=NULL&&ok==0)
p->next =run;
}
void Insertc()
{
p=headc;
while(p->next !=NULL)
{
p=p->next;
}
p->next=run;
}
void Changeother()
{
p=head->next ;
while(p!=NULL)
{
p->a_time ++;
p=p->next ;
}
}
void Runall()
{
while(head->next !=NULL)
{
Sleep(3000);
q=head->next->next;
run=head->next;
run->next =NULL;
head->next =q;
run->m_time =run->m_time - 1;
run->a_time ++;
run->r_time ++;
Changeother();
cout<<"-----------------------------------------------------------------"<<endl;
cout<<"本次调度进程名: "<<run->name;
if(run->m_time >0)
{
run->value =run->value -1;
cout<<" 仍未完成,插入到就绪队列 运行时间减 1 优先数减 1"<<endl;
Insert();
}
if(run->m_time ==0)
{
run->condition ='E';
cout<<" 完成运行,从就绪队列删除."<<endl;
Insertc();
}
Outhead();
Outheadc();
}
cout<<"所有进程都已完成!"<<endl;
Outheadc();
}
void Runall1()
{
while(head->next !=NULL)
{
q=head->next->next;
run=head->next;
run->next =NULL;
head->next =q;
run->m_time =run->m_time - 1;
run->a_time ++;
run->r_time ++;
Changeother();
if(run->m_time >0)
{
run->value =run->value -1;
Insert();
}
if(run->m_time ==0)
{
run->condition ='E';
Insertc();
}
}
Outheadc();
}
int Runone()
{
run=head->next;
if(run!=NULL)
{
q=head->next->next;
run->next =NULL;
head->next =q;
run->m_time =run->m_time - 1;
run->a_time ++;
run->r_time ++;
Changeother();
cout<<"-----------------------------------------------------------------"<<endl;
cout<<"本次调度进程名: "<<run->name;
if(run->m_time >0)
{
run->value =run->value -1;
cout<<" 仍未完成,插入到就绪队列 运行时间减 1 优先数减 1"<<endl;
Insert();
}
if(run->m_time ==0)
{
run->condition ='E';
cout<<" 完成运行,从就绪队列删除."<<endl;
Insertc();
}
Outhead();
Outheadc();
return(0);
}
else
{
cout<<"所有进程调度完毕"<<endl;
Outheadc();
return(1);
}
}
void Begin()
{
int n=0,f;
cout<<"待运行的进程数量:";
cin>>NO_OF_PROGRAM;
Initiate();
Sortness();
while(n!=5)
{
if(n==0)
{
cout<<"-------------------------"<<endl;
cout<<"1---继续下一次调度"<<endl;
cout<<"2---向就绪队列添加进程"<<endl;
cout<<"3---自动执行"<<endl;
cout<<"4---直接显示调度后结果"<<endl;
cout<<"5---返回上一菜单"<<endl;
cout<<"-------------------------"<<endl;
cout<<"请选择 :";
cin>>n;
}
else if(n==1)
{
f=Runone(); n=0;
if(f==1) n=5;
}
else if(n==2)
{
run=(struct PCB*) malloc (sizeof(struct PCB));
run->next =NULL;
cout<<"进程名:";
cin>>iname;
strcpy(run->name,iname);
cout<<"运行时间:";
cin>>run->m_time ;
cout<<"优先数:";
cin>>run->value ;
run->a_time =0;
run->r_time =0;
run->condition ='R';
Insert();
n=0;
}
else if(n==3)
{
Runall();
n=5;
}
else if(n==4)
{
Runall1();
n=5;
}
else if(n!=5)
{
cout<<"选择错误! 重新选择"<<endl;
n=0;
}
}
}
void main()
{
int c=0;
while(c!=2)
{
cout<<"\n\n\t ";
for(int i=0;i<40;i++)
cout<<"*";
cout<<endl<<"\t"<<" * 优 先 级 调 度 算 法 *"<<endl;
cout<<endl<<"\t"<<" * 02计算机 *"<<endl;
printf("\t ");
for( i=0;i<40;i++)
printf("*");
printf("\n\n\n\n\n");
cout<<"-------------------------"<<endl;
cout<<"1---开始运行"<<endl;
cout<<"2---退出系统"<<endl;
cout<<"-------------------------"<<endl;
cout<<"请选择: ";
cin>>c;
if(c==