#include "iostream.h"
#include "string.h"
const int prnum=5;
const int ROUND=3;
typedef enum flag{Ready,Run,Finish};
struct pcb
{
public:
char name[10]; //进程名
int pri; //进程优先数
int round; //进程轮转时间片
int cputime; //进程占用的CPU时间
int needtime; //进程到完成还要的CPU时间
flag state; //进程状态
struct pcb *next;//链指针
};
typedef struct pcb plist;
typedef plist *link;
void main()
{
int i;
int chose;
cout<<"该程序实现的是5个进程的两种调度算法"<<endl;
while ((chose!=1)&&(chose!=2))
{
cout<<"=================================="<<endl;
cout<<" 请选择一种进程调度算法: "<<endl;
cout<<" 1.优先数调度算法 "<<endl;
cout<<" 2.循环时间片轮转调度算法 "<<endl;
cout<<"=================================="<<endl;
cout<<"请选择(1或者2):";
loop:cin>>chose;
cout<<endl;
if ((chose!=1)&&(chose!=2))
{
cout<<"输入错误,请重新输入(1或者2):"<<endl;
goto loop;
}
else cout<<"请分别按顺序输入5个进程的各项参数:"<<endl;;
}
void showlist(link,char*,int); //显示进程队列
void instlist(link,link); //按优先数插入进程
void appenlist(link,link); //按就绪先后加入进程
link gethead(link); //取队首进程
link ptr,head1,head2;
head1=new plist;
head1->next=NULL; //就绪队首指针
head2=new plist;
head2->next=NULL; //完成队首指针
//初始化进程队列
cout<<"进程名 "<<"优先数 "<<"占用CPU时间 "<<"还需CPU时间"<<endl;
for(i=0;i<prnum;i++)
{
ptr=new plist;
cin>>ptr->name>>ptr->pri>>ptr->cputime>>ptr->needtime;
ptr->round=ROUND;
ptr->state=Ready;
ptr->next=NULL;
if(chose==1)
instlist(head1,ptr);
else
appenlist(head1,ptr);
}
cout<<"\n"<<endl;
while(head1->next!=NULL)
{
if(head1->next!=NULL)
showlist(head1,"就绪进程队列:",chose);
if(head2->next!=NULL)
showlist(head2,"完成进程队列:",chose);
ptr=gethead(head1);
if(chose==1)
{
ptr->cputime=ptr->cputime+3;
ptr->needtime=ptr->needtime-3;
ptr->state=Ready;
}
else
{
ptr->cputime=ptr->cputime+ptr->round;
ptr->needtime=ptr->needtime-ptr->round;
ptr->state=Ready;
}
if(ptr->needtime<=0)
{
ptr->needtime=0;
ptr->state=Finish;
appenlist(head2,ptr);
}
else
{
if(chose==1)
instlist(head1,ptr);
else
appenlist(head1,ptr);
}
}
showlist(head2,"完成进程队列:",chose);
}
//按就绪先后加入进程
void appenlist(link head,link ptr1)
{
link p;
p=head;
if(head->next==NULL)
head->next=ptr1;
else
{
while(p->next!=NULL)p=p->next;
p->next=ptr1;
}
};
//按优先数插入进程
void instlist(link head,link ptr1)
{
link p1,p2;
p1=head->next;
p2=head;
if(head->next==NULL)
{
head->next=ptr1;
}
else
{
while((p1->next!=NULL)&&((p1->pri)>=(ptr1->pri)))
{
p2=p1;
p1=p1->next;
}
if((p1->pri) < (ptr1->pri))
{
p2->next=ptr1;
ptr1->next=p1;
}
else
{
p1->next=ptr1;
}
}
};
//取队首进程
link gethead(link head)
{
link ptr1;
ptr1=head->next;
head->next=ptr1->next;
ptr1->next=NULL;
ptr1->state=Run;
return ptr1;
};
//显示进程队列
void showlist(link ptr1,char *msg,int k)
{
ptr1=ptr1->next;
cout<<msg<<endl;
if(k==1)
{
cout<<"进程名 "<<"优先数 "<<"占用CPU时间 "<<"还需CPU时间 "<<"进程状态 "<<endl;
while(ptr1!=NULL)
{
cout<<" "<<ptr1->name<<" "<<ptr1->pri;
cout<<" "<<ptr1->cputime<<" "<<ptr1->needtime;
cout<<" "<<ptr1->state<<endl;
ptr1=ptr1->next;
}
}
else
{
cout<<"进程名 "<<"时间片 "<<"占用CPU时间 "<<"还需CPU时间 "<<"进程状态 "<<endl;
while(ptr1!=NULL)
{
cout<<" "<<ptr1->name<<" "<<ptr1->round;
cout<<" "<<ptr1->cputime<<" "<<ptr1->needtime;
cout<<" "<<ptr1->state<<endl;
ptr1=ptr1->next;
}
}
};
优先数,时间片算法
最新推荐文章于 2024-10-29 10:26:27 发布