📄 main.cpp
字号:
// Main.cpp: implementation of the Main class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Process.h"
#include "Main.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
Engine::Engine()
{
timeSlice=10;
}
void Engine::CreateProcesses(int num,int maxPriority,int maxStartTime,int maxExeTime)
{
memset(a,0,sizeof(PCB)*num);
numOfProcess=num;
srand(GetTickCount());
CString s;
for (int i=0;i<num;i++)
{
a[i].ID=i+1;
s.Format("进程 %d",i+1);
char * c=s.GetBuffer(10);
int l=s.GetLength();
memcpy(a[i].name,c,l);
a[i].next=NULL;
a[i].priority=rand()%(maxPriority+1);
a[i].round=timeSlice;
a[i].state=NEW;
a[i].timeLeft=rand()%(maxExeTime)+1;
if (a[i].timeLeft<timeSlice) a[i].round=a[i].timeLeft;
a[i].timeStart=rand()%(maxStartTime+1);
a[i].timeUsed=0;
}
}
void Engine::CopyProcesses()
{
this->runningCount=0;
ended=false;
memcpy(b,a,sizeof(PCB)*numOfProcess);
}
void Engine::InitQueue()
{
readyHead=NULL;
running=NULL;
endedHead=NULL;
newHead=NULL;
BYTE mark[MAXSIZE];
memset(mark,0,MAXSIZE);
PPCB p=NULL;
for (int i=0;i<numOfProcess;i++)
{
int min=100000;
int k=0;
for (int j=0;j<numOfProcess;j++)
if (!mark[j])
{
if (b[j].timeStart<min)
{
min=b[j].timeStart;
k=j;
}
}
mark[k]=1;
if (p==NULL)
{
newHead=&b[k];
p=newHead;
p->next=NULL;
}
else
{
p->next=&b[k];
p=p->next;
p->next=NULL;
}
}
}
PPCB Engine::getNewHead()
{
return newHead;
}
PPCB Engine::getReadyHead()
{
return readyHead;
}
PPCB Engine::getRunning()
{
return running;
}
PPCB Engine::getEndedHead()
{
return endedHead;
}
void Engine::setTimeSlice(int i)
{
timeSlice=i;
}
PriorityEngine::PriorityEngine()
{
}
void Engine::MoveNewToReady(int time)
{
if (newHead==NULL) return;
while(newHead->timeStart==time)
{
PPCB p=newHead;
newHead=newHead->next;
hasMessage=true;
message[numMessage++].Format("时刻:%4d 进程%2d 从外界调入就绪队列",time,p->ID);
addToReady(p);
p->state=READY;
if (newHead==NULL) return;
}
}
void Engine::MoveRunning(int time)
{
if (running==NULL) return;
running->timeLeft--;
running->timeUsed++;
runningCount++;
if (running->timeLeft<timeSlice) running->round=running->timeLeft;
if (runningCount>=timeSlice&&running->timeLeft>0)
{
hasMessage=true;
message[numMessage++].Format("时刻:%4d 进程%2d 被中断,进入就绪队列",time,running->ID);
running->priority-=2;
addToReady(running);
running->state=READY;
running=NULL;
return;
}
if (running->timeLeft<=0)
{
hasMessage=true;
message[numMessage++].Format("时刻:%4d 进程%2d 运行完毕",time,running->ID);
running->next=endedHead;
endedHead=running;
running->state=ENDED;
running->timeEnded=time;
running=NULL;
}
}
void Engine::MoveReadyToRunning(int time)
{
if (running!=NULL) return;
runningCount=0;
running=readyHead;
if (running==NULL)
{
// ::MessageBox(NULL,"ENDED","MESSAGE",MB_OK);
return;
}
running->state=RUNNING;
hasMessage=true;
message[numMessage++].Format("时刻:%4d 进程%2d 从就绪队列调入运行",time,running->ID);
readyHead=readyHead->next;
}
void Engine::StepTo(int time)
{
hasMessage=false;
numMessage=0;
// ASSERT(time!=5);
MoveNewToReady(time);
MoveRunning(time);
MoveReadyToRunning(time);
if (newHead==NULL&&readyHead==NULL&&running==NULL) ended=true;
}
void PriorityEngine::addToReady(PPCB p)
{
if (readyHead==NULL)
{
readyHead=p;
p->next=NULL;
}
else if (p->priority>readyHead->priority)
{
p->next=readyHead;
readyHead=p;
}
else
{
PPCB pp=readyHead;
while (pp->next!=NULL&&pp->next->priority>=p->priority) pp=pp->next;
p->next=pp->next;
pp->next=p;
}
}
void RoundEngine::addToReady(PPCB p)
{
if (readyHead==NULL)
{
readyHead=p;
p->next=NULL;
}
else
{
PPCB pp=readyHead;
while (pp->next!=NULL) pp=pp->next;
pp->next=p;
p->next=NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -