主题:进程调度优先级调度算法程序运行时出现的问题!
谁能帮把我这个程序改改,改到能运行为止,小弟感激不尽~~~!!!
头文件不知道怎么设置!!去掉的话错误会有非常多!!
#include "stdafx.h"
#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()
{
cout< cout<<"您现在查看的是就绪队列的信息:"< pcb1 p;
cout<<"进程号 "<<"进程名 "<<"优先级 "<<"状态"<<"已运行时间 "<<"需运行时间"< for(p=s;p!=NULL;p=p->next)
{
cout<id<<" "<name<<" "<super<<" "<state<<" "<rtime<<" "<ntime< }
}
//print the information of the blocked queue打印等待队列信息
void print1()
{
cout< cout<<"您现在查看的是等待队列的信息"< pcb1 p;
cout<<"进程号 "<<"进程名 "<<"优先级 "<<"状态 "<<"已运行时间 "<<"需运行时间"< for(p=w;p!=NULL;p=p->next)
{
cout<id<<" "<name<<" "<super<<" "<state<<" "<rtime<<" "<ntime< }
}
//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< cout<<"进程"<id<<" 已经结束"< 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< cout<<"现在就绪队列已经为空,再没有进程需要阻塞"< }
}
//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< cout<<"等待队列已经为空,没有进程再需要唤醒"< }
}
//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< cout<<"就绪队列已经为空"< }
}
//creat process新进程进入
void input()
{
pcb1 p2;
p2=new pcb;
cout<<"请输入 进程号、进程名、进程优先级、需要运行时间";
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<<"*****************************进程调度模拟程序开始*******************************"< cout<<"----w/唤醒进程-----r/运行进程-----z/等待进程----q/退出程序--"< cout<<"--------c/创建进程---------s /查看就绪进程---------l/查看等待队列----"< while(ch!='q')
{
cout<<"请输入一个字符"< 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;
}
}
}
头文件不知道怎么设置!!去掉的话错误会有非常多!!
#include "stdafx.h"
#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()
{
cout< cout<<"您现在查看的是就绪队列的信息:"< pcb1 p;
cout<<"进程号 "<<"进程名 "<<"优先级 "<<"状态"<<"已运行时间 "<<"需运行时间"< for(p=s;p!=NULL;p=p->next)
{
cout<id<<" "<name<<" "<super<<" "<state<<" "<rtime<<" "<ntime< }
}
//print the information of the blocked queue打印等待队列信息
void print1()
{
cout< cout<<"您现在查看的是等待队列的信息"< pcb1 p;
cout<<"进程号 "<<"进程名 "<<"优先级 "<<"状态 "<<"已运行时间 "<<"需运行时间"< for(p=w;p!=NULL;p=p->next)
{
cout<id<<" "<name<<" "<super<<" "<state<<" "<rtime<<" "<ntime< }
}
//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< cout<<"进程"<id<<" 已经结束"< 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< cout<<"现在就绪队列已经为空,再没有进程需要阻塞"< }
}
//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< cout<<"等待队列已经为空,没有进程再需要唤醒"< }
}
//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< cout<<"就绪队列已经为空"< }
}
//creat process新进程进入
void input()
{
pcb1 p2;
p2=new pcb;
cout<<"请输入 进程号、进程名、进程优先级、需要运行时间";
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<<"*****************************进程调度模拟程序开始*******************************"< cout<<"----w/唤醒进程-----r/运行进程-----z/等待进程----q/退出程序--"< cout<<"--------c/创建进程---------s /查看就绪进程---------l/查看等待队列----"< while(ch!='q')
{
cout<<"请输入一个字符"< 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;
}
}
}