#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定义进程控制块PCB */
char name[10]; //进程名
char state; //进程状态
int super; //进程优先级
int ntime; //进程需要运行时间
int rtime; //进程已经运行的时间
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,插入到当前进程前面*/
{
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0)
first->link=p;
}
}
void input() /* 建立进程控制块函数*/
{
int i;
cout<<"★★★★请输入五个进程信息★★★★"<<endl;
cout<<endl;
for(i=0;i<5;i++)
{
cout<<"进程号No."<<i<<endl;
p=getpch(PCB);
cout<<"输入进程名:";
cin>>p->name;
cout<<"输入进程优先数:";
cin>>p->super;
cout<<"输入进程运行时间:";
cin>>p->ntime;
cout<<endl;
p->rtime=0;p->state='w';
p->link=NULL;
sort(); /* 调用sort函数*/
}
}
int space() //计算进程控制块的个数
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
cout<<endl;
cout<<"进程名 状态 优先级 所需时间 已运行时间"<<endl;
cout<<" "<<pr->name<<" ";
cout<<pr->state<<" ";
cout<<pr->super<<" ";
cout<<pr->ntime<<" ";
cout<<pr->rtime<<" ";
cout<<endl;
}
void disp2(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
cout<<endl;
cout<<"进程名 状态 所需时间 已运行时间"<<endl;
cout<<" "<<pr->name<<" ";
cout<<pr->state<<" ";
cout<<pr->ntime<<" ";
cout<<pr->rtime<<" ";
cout<<endl;
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
cout<<endl<<"**** 当前正在运行的进程是:" <<p->name<<endl; /*显示当前运行进程*/
disp(p);
pr=ready;
cout<<endl<<"****当前就绪队列状态为:"<<endl; /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void check2() /* 建立进程查看函数 */
{
PCB* pr;
cout<<endl<<"**** 当前正在运行的进程是:" <<p->name<<endl; /*显示当前运行进程*/
disp2(p);
pr=ready;
cout<<endl<<"****当前就绪队列状态为:"<<endl; /*显示就绪队列状态*/
while(pr!=NULL)
{
disp2(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
cout<<endl<<"进程"<<p->name<<"已完成"<<endl;
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->super)--;
p->state='w';
sort(); /*调用sort函数*/
}
}
void youxian() // 高优先级优先算法的程序入口
{
int len,h=0;
char ch;
system("cls");
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
h++;
cout<<endl<<"执行的次数:"<<h;
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
ch=getchar();
cout<<endl<<"按任一键继续......";
}
cout<<endl<<endl<<"进程已经完成."<<endl;
ch=getchar();
}
void sort2()
{
PCB *q;
q=ready;
if(ready==NULL) ready=p;
else
{
while(q->link!=NULL) {q=q->link;}
q->link=p;
}
}
void input2() /* 建立进程控制块函数*/
{
int i;
system("cls"); /*清屏*/
cout<<endl<<"★★★★请输入五个进程信息★★★★"<<endl;
cout<<endl;
for(i=0;i<5;i++)
{
cout<<"进程号No."<<i<<endl;
p=getpch(PCB);
cout<<"输入进程名:";
cin>>p->name;
cout<<"输入进程运行时间:";
cin>>p->ntime;
cout<<endl;
p->rtime=0;p->state='w';
p->link=NULL;
sort2(); /* 调用sort函数*/
}
}
void running2() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
p->state='w';
sort2(); /*调用sort函数*/
}
}
void lunzhuan() //轮转法演示进程的程序入口
{
int len,h=0;
char ch;
system("cls");
input2();
len=space();
while((len!=0)&&(ready!=NULL))
{
h++;
cout<<endl<<"执行的次数:"<<h;
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check2();
running2();
ch=getchar();
cout<<endl<<"按任一键继续......";
}
cout<<endl<<endl<<"进程已经完成."<<endl;
ch=getchar();
}
void main()
{
int m;
system("cls");
cout<<" *************************************************"<<endl;
cout<<" 操作系统实验一"<<endl;
cout<<" 用高级语言程序实现进程调度"<<endl;
cout<<" 指导老师:XXX"<<endl;
cout<<" *************************************************"<<endl;
cout<<endl<<endl<<endl;
cout<<" 1.演示最高优先数优先算法."<<endl<<endl;
cout<<" 2.演示轮转法算法."<<endl<<endl;
cout<<" 0.退出程序."<<endl<<endl;
cout<<" 选择进程调度方法:";
cin>>m;
switch(m)
{
case 1:
youxian(); //高优先级优先算法的程序入口
system("cls");
break;
case 2:
lunzhuan(); //轮转法演示进程的程序入口
system("cls");
break;
case 0:
system("cls");
break;
}
}