和妳的要求也許不知道壹樣但是妳可以看看啊
模擬進程調度算法:
用C++的代碼描述:
#include "iostream.h"
//define pcb
typedef struct pcb
{
char name[10]; //進程名
char state; //狀態w(就緒)r(運行)f(結束)
int id; //id號
int super; //優先級
int ntime; //需運行的時間
int rtime; //已運行的時間
struct pcb *next;
}*pcb1;
pcb1 s,w;//define two publiced linknode ,one is s(ready queue),one is w(blocked queue)
//initialize two queues
void init(pcb1 &r)
{
r=NULL;//both queues is the kind of head index
}
//print the information of the ready queue
void print()
{pcb1 p;
cout<<"您現在查看的是就緒隊列的信息:" ;
cout<<"進程號 "<<"進程名 "<<"優先級 "<<"狀態"<<"已運行時間 "<<"需運行時間"<<endl;
for(p=s;p!=NULL;p=p->next)
{
cout<<p->id<<" "<<p->name<<" "<<p->super<<" "<<p->state<<" "<<p->rtime<<" "<<p->ntime<<endl; }
}
//print the information of the blocked queue
void print1()
{pcb1 p;
cout<<"您現在查看的是阻塞隊列的信息";
cout<<"進程號 "<<"進程名 "<<"優先級 "<<"狀態 "<<"已運行時間 "<<"需運行時間"<<endl;
for(p=w;p!=NULL;p=p->next)
{
cout<<p->id<<" "<<p->name<<" "<<p->super<<" "<<p->state<<" "<<p->rtime<<" "<<p->ntime<<endl; }
}
//check the queue if empty
int empty(pcb1 &r)
{
if(r==NULL)
return 0;
else
return 1;
}
//check the first process of the ready queue if finshed
int check()
{
pcb1 p;
p=s;
if(p->rtime==p->ntime)
{
p->state='F';//if one process finshed then change ti's state
cout<<"進程"<<p->id<<" 已經結束"<<endl;
return 0;
}
else
return 1;
}
//sort process according to the super of the process and insert to the ready(blocked) queue
void sort(pcb1 &r,pcb1 p)
{
pcb1 p1,p2;
int in=0;
if(r==NULL)//the queue is empty
{
r=p;
}
else
{
if(p->super>=r->super)//the super of the process which wait insert to the queue is highter than the super of the first process of the queue
{
p->next=r;
r=p;
}
else
{
p1=r;
p2=r->next;
if(p2==NULL)//only one process in the queue
{
r->next=p;
}
else
{
while(in==0&&p2!=NULL)//insert to the middle of the queue
{
if(p->super>=p2->super)
{
p->next=p2;
p1->next=p;
in=1;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
}
if(in==0)//link to the last of ready queue
p1->next=p;
}
}
}
//block one process and insert to block queue
void block()
{
if(empty(s))
{
if(s->next==NULL)
{
sort(w,s);
s=s->next;
}
else
{
pcb1 p1;
p1=s;
s=s->next;
p1->next=NULL;
sort(w,p1);
}
}
else
{
cout<<"現在就緒隊列已經為空,再沒有進程需要阻塞"<<endl; }
}
//wake one process of block queue and insert to ready queue
void wake()
{
if(empty(w))
{
pcb1 p1;
p1=w;
w=w->next;
p1->next=NULL;
sort(s,p1);
}
else
{
cout<<"阻塞隊列已經為空,沒有進程再需要喚醒"<<endl; }
}
//runing
void runing()
{
if(empty(s))
{
pcb1 p;
p=s;
if(check())//check the first process of the queue if finished
{//no
s=s->next;
p->rtime++;
p->super--;
p->next=NULL;
sort(s,p);
}
else
{//yes
s=s->next;
}
}
else
{
cout<<"就緒隊列已經為空"<<endl; }
}
//creat process
void input()
{
pcb1 p2;
p2=new pcb;
cout<<"請輸入 進程號、進程名、進程優先級、需要運行時間";
cin>>p2->id>>p2->name>>p2->super>>p2->ntime;
p2->rtime=0;
p2->state='W';
p2->rtime=0;
p2->next=NULL;
sort(s,p2);
}
//main function
void main()
{
char ch;
init(s);
init(w);
cout<<"*****************************進程調度模擬程序開始*******************************"<<endl;
cout<<"----w/喚醒進程-----r/運行進程-----z/阻塞進程----q/退出程序--"<<endl;
cout<<"--------c/創建進程---------s /查看就緒進程---------l/查看阻塞隊列----"<<endl;
while(ch!='q')
{
cout<<"請輸入壹個字符"<<endl;
cin>>ch;
switch(ch)
{
case 'c':input(); break;
case 'r':runing(); break;
case 's':print(); break;
case 'w':wake(); break;
case 'l':print1(); break;
case 'z':block(); break;
}
}
}