?? shijianpian.cpp
字號:
#include "stdafx.h"
#include "shijianpian.h"
/*初始化隊列*/
LinkQueue::LinkQueue()
{
this->front=(PCBQueue)malloc(sizeof(PCB));
this->rear=this->front;
if (!this->front) exit (QOVERFLOW);/*存儲分配失敗*/
this->front->next = NULL;
totalTime = 0;
curTime = 0;
bLastOver = false;
nZhouzhuanTime = 0;
}
/*銷毀隊列*/
LinkQueue::~LinkQueue()
{
while(this->front)
{
this->rear= this->front->next;
free(this->front);
this->front=this->rear;
}
}
/*隊列長度*/
int LinkQueue::GetLength() const
{
int i;
i=this->rear-this->front ;
return i ;
}
/* 隊列運行總時間 */
int LinkQueue::GetTotalTime() const
{
return totalTime;
}
/*叛空*/
bool LinkQueue::IsEmpty() const
{
if(this->front-this->rear==0)
return true;
else
return false;
}
/* 加入預備隊列 */
void LinkQueue::SetElemVec( const std::vector<ElemType>& vec )
{
this->vecElem = vec;
totalTime = 0;
for ( QCIT it = this->vecElem.begin(); it != this->vecElem.end(); it++ )
{
totalTime += it->cpuTime;
}
}
/*入隊列;插入元素e為新的隊尾元素*/
Status LinkQueue::EnQueue( ElemType e)
{
PCBQueue p;
p=(PCBQueue)malloc(sizeof(PCB));
if(!p) exit (QOVERFLOW); /*存儲分配失敗*/
p->data=e;
p->next=NULL;
this->rear->next=p;
this->rear=p;
return QOK;
}
/*若隊列不空,則刪除Q的隊頭元素,用e返回其值,并返回OK;否則返回ERROR*/
Status LinkQueue::DeQueue( ElemType &e)
{
PCBQueue p;
if (this->front==this->rear) return QERROR;
p=this->front->next;
e=p->data;
this->front->next=p->next;
if (this->rear==p)
this->rear = this->front;
free (p);
return QOK;
}
/*判斷是否有新進程到達,若有新進程到達,則將其插入隊尾*/
std::vector<ElemType>::const_iterator LinkQueue::NewArrive( int iTime , QCIT it_begin)
{
QCIT it;
for ( it = it_begin; it != this->vecElem.end(); it++ )
{
if ( it->arriveTime == iTime )
return it;
}
return it;
}
/*運行進程*/
Status LinkQueue::RunOnce()
{
ElemType eTmp1;
this->bLastOver = false;
/*判斷是否有些進程到達到達插入隊尾*/
QCIT it = NewArrive( this->curTime , vecElem.begin() );
while ( it != this->vecElem.end() )
{
this->EnQueue( *it );
it++;
it = NewArrive( this->curTime, it );
}
/*對隊首進程進行運行*/
(this->front->next->data.alreadyTime)++;
(this->front->next->data.needTime)--;
lastElem = this->front->next->data;
/*判斷仍需時間為0結束進程*/
if((this->front->next->data.needTime)==0)
{
this->bLastOver = true;
this->nZhouzhuanTime = this->curTime - this->front->next->data.arriveTime;
this->DeQueue( eTmp1 );
}
/*否則插入隊列尾*/
else
{
this->DeQueue( eTmp1 );
this->EnQueue ( eTmp1 );
}
this->curTime++;
if ( this->GetTotalTime() == this->curTime ) return QOK;
return QERROR;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -