我程序编译时能够通过,单步调试时却出现"Acess Violation"的错误提示,请问哪位大虾知道这是什么类型的错误呀,是由什么引起的呀?
好的,我把程序传上来!
#include<malloc.h>
#include<stdio.h>
#include<iostream.h>
enum STATE {RUN,READY,SUSPEND};//进程状态
int Readylength;
int Suspendlength;
typedef struct Node{
struct Node *pnext;
int ProcessID;
enum STATE State;
}PCB;//进程控制快
PCB *pReadyFront,*pReadyTail;//就绪队列的队头和队尾指针
PCB *pSuspendFront,*pSuspendTail;//等待队列的队头和队尾
PCB *pRunTask;
void CreatReadyQueue(int length)
{
int i,j=0;
PCB *ptr;
for(i=0;i<length;i++)
{
ptr=(PCB *)malloc(sizeof(PCB));
cout<<"请输入第"<<i+1<<"个处于就绪状态的进程标号:";
cin>>j;
ptr->ProcessID=j;
ptr->State=READY;
ptr->pnext=NULL;
pReadyTail->pnext=ptr;//插到就绪队列末尾。。。。。程序运行到这里就运行不下去了,出现了我所说的错误
pReadyTail=ptr;
Readylength++;
}
}
void CreatSuspendQueue(int length)
{
int i,j=0;
PCB *ptr;
for(i=0;i<length;i++)
{
ptr=(PCB *)malloc(sizeof(PCB));
cout<<"请输入第"<<i+1<<"个处于等待状态的进程标号:";
cin>>j;
ptr->ProcessID=j;
ptr->State=SUSPEND;
ptr->pnext=NULL;
pSuspendTail->pnext=ptr;//插到等待队列末尾
pReadyTail=ptr;
Suspendlength++;
}
}
void GreatRunProcess(void)
{
PCB *ptr;
int i;
ptr=(PCB *)malloc(sizeof(PCB));
cout<<"请输入处于运行状态的进程号:";
cin>>i;
ptr->pnext=NULL;
ptr->ProcessID=i;
ptr->State=RUN;
pRunTask=ptr;
}
PCB *GetReadyProcess(void)
{
PCB *ptr;
if(pReadyFront==pReadyTail)
return NULL;//就绪队列为空
else
{
ptr=pReadyFront->pnext;
Readylength--;
return ptr;
}
}
PCB *GetSuspendProcess(void)
{
PCB *ptr;
if(pSuspendFront==pSuspendTail)
return NULL;//等待队列为空
else
{
ptr=pSuspendFront->pnext;
Suspendlength--;
return ptr;
}
}
PCB *GetRunProcess(void)
{
PCB *ptr;
ptr=pRunTask;
if(!ptr)
return NULL;
else
{
pRunTask=NULL;
return ptr;
}
}
void putReadyProcess(PCB *pTask)
{
pReadyTail->pnext=pTask;
pReadyTail=pTask;
Readylength++;
}
void putSuspendProcess(PCB *pTask)
{
pSuspendTail->pnext=pTask;
pSuspendTail=pTask;
Suspendlength++;
}
void putRunProcess(PCB *ptr)
{
pRunTask=ptr;
}
void SetProcessState(PCB *pTask,enum STATE status)
{
pTask->State=status;
}
void Scheduler(void)
{
PCB *p;
p=GetReadyProcess();
if(!p)
cout<<"就绪队列为空!"<<endl;
else
{
SetProcessState(p,RUN);
putRunProcess(p);
}
}
void Suspend(void)
{
PCB *p;
p=GetRunProcess();
if(!p)
cout<<"无进程处于运行状态!"<<endl;
else
{
SetProcessState(p,SUSPEND);
putSuspendProcess(p);
}
}
void Active(void)
{
PCB *p;
p=GetSuspendProcess();
if(!p)
cout<<"无进程处于等待状态!"<<endl;
else
{
SetProcessState(p,READY);
putReadyProcess(p);
}
}
void TimeOut(void)
{
PCB *p;
p=GetRunProcess();
if(!p)
cout<<"无进程处于运行状态!"<<endl;
else
{
SetProcessState(p,READY);
putReadyProcess(p);
}
}
void KillProcess(void)
{
free(pRunTask);
pRunTask=NULL;
}
void DisplayMenu(void)
{
cout<<"------MENU-------"<<endl;
cout<<"0: 退出! "<<endl;
cout<<"1: 唤醒进程! "<<endl;
cout<<"2: 调度! "<<endl;
cout<<"3: 时间片超时! "<<endl;
cout<<"4: 阻塞! "<<endl;
cout<<"5: 杀死进程! "<<endl;
}
void DisplayProcess(void)
{
PCB *ptr;
cout<<"一共有"<<Readylength<<"个进程处于就绪状态!"<<endl;
ptr=pReadyFront->pnext;
while(ptr)
{
cout<<ptr->ProcessID<<' ';
ptr=ptr->pnext;
}
cout<<endl;
cout<<"一共有"<<Suspendlength<<"个进程处于等待状态!"<<endl;
ptr=pSuspendFront->pnext;
while(ptr)
{
cout<<ptr->ProcessID<<' ';
ptr=ptr->pnext;
}
cout<<endl;
if(!pRunTask)
cout<<"无进程处于运行状态!"<<endl;
cout<<pRunTask->ProcessID<<"号进程处于运行状态!"<<endl;
}
void main()
{
int choice;
CreatReadyQueue(5);
CreatSuspendQueue(5);
GreatRunProcess();
while(1)
{
DisplayMenu();
cout<<"请选择你的操作!`!"<<endl;
cin>>choice;
switch(choice)
{
case 0:cout<<"欢迎再来!你将退出系统"<<endl;
return;
break;
case 1:Active();
DisplayProcess();
break;
case 2:Scheduler();
DisplayProcess();
break;
case 3:TimeOut();
DisplayProcess();
break;
case 4:Suspend();
DisplayProcess();
break;
case 5:KillProcess();
DisplayProcess();
break;
default:cout<<"你输入有误,请重新输入!"<<endl;
}//switch
}//while
}