//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TmainForm *mainForm;
Tpcb pcbx1;
Tpcb *pcb1=&pcbx1;
Tstatus readyx,blockx;
Tstatus *ready=&readyx,*block=&blockx;
pcbPoint New_PCB=NULL,Curr_PCB=NULL;
int restCpu,restIo,restFile,restMem;
int newCx,finishCx,drawCx,turnCx;
int timeSlice=3;
int maxPcb=5;
int MAX_PCB=5;
bool OverSrc(pcbPoint p);
void ReleaseSrc(pcbPoint p);
bool RequestSrc();
void BlockToReady();
void Disp();
void ExitPcb();
void Setting();
void UnSetting();
void AssignSrc(pcbPoint p);
void TestMine();
void fcfsRun();
AnsiString Curr_Disp(pcbPoint p);
AnsiString Curr_Disp(pcbPoint p)
{
return ("("+IntToStr(p->cpuTime)+","+IntToStr(p->ioSrc )+","
+IntToStr(p->fileSrc)+","+IntToStr(p->memSrc)+")");
}
void fcfsRun()
{
int i=Curr_PCB->cpuTime;
while(i>0)
{
if(RequestSrc())
{
Curr_PCB->cpuTime--;
Disp();
ShowMessage("请按回车");
if(Curr_PCB->cpuTime==0)
{
ReleaseSrc(Curr_PCB);
Disp();
mainForm->PcbHis->Lines->Add("\tThis PCB is Finish");
ShowMessage("ID:"+IntToStr(Curr_PCB->ID)+" Finish!");
pcb1->Dele(Curr_PCB);
finishCx++;
if(newCx==finishCx)
{
mainForm->PcbHis->Lines->Add("进程全部执行完毕!");
ExitPcb();
}
else
{
BlockToReady();
mainForm->Button2->Click();
}
break;
}
}
else
{
if(!OverSrc(Curr_PCB))
{
mainForm->PcbHis->Lines->Add("\t该进程放入阻塞!");
mainForm->PcbHis->Lines->Add("\tREST:"+Curr_Disp(Curr_PCB));
block->Add(Curr_PCB);
}
else
{
mainForm->PcbHis->Lines->Add(IntToStr(Curr_PCB->ID)+"被销毁,超过总资源!");
drawCx++;
pcb1->Dele(Curr_PCB);
BlockToReady();
}
mainForm->Button2->Click();
break;
}
}
}
//---------------------------------------------------------------------------
__fastcall TmainForm::TmainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TmainForm::Button1Click(TObject *Sender)//初始化环境
{
//设置资源数目
restCpu=CPU_TIME;
restIo=IO_SRC;
restFile=FILE_SRC;
restMem=MEM_SRC;
//设置时间片的长度\调度种类\ 进程个数\时间片间隔
maxPcb=StrToInt(mainForm->maxPcbSet->Text);
MAX_PCB=maxPcb;
mainForm->creatPCB->Interval=StrToInt(mainForm->pcbCreatSet->Text);
//变量初始化
newCx=0;
finishCx=0;
drawCx=0;
New_PCB=NULL;
Curr_PCB=NULL;
//初始化队列
pcb1->Init();
ready->Init();
block->Init();
//启动进程创建时钟
Disp();
mainForm->creatPCB->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TmainForm::creatPCBTimer(TObject *Sender)//Form Here
{
pcbIndex* p;
New_PCB=pcb1->Add(); //随机创建一个进程,并使New_PCB指向该进程
newCx++; //记录创建的进程数数量
if( restIo>=New_PCB->ioSrc && //是否有足够的资源满足*New_PCB进程
restFile >= New_PCB->fileSrc && restMem>=New_PCB->memSrc)
{
AssignSrc(New_PCB); //对New_PCB分配资源,
ready->Add(New_PCB); //将New_PCB追加到READY队列中
}
else
{
block->Add(New_PCB); //将New_PCB追加到BLOCK队列中
}
TestMine();
if(newCx==MAX_PCB) //创建的进程数达到系统预设
{
Disp();
mainForm->creatPCB->Enabled=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TmainForm::Button2Click(TObject *Sender)
{
pcbIndexPoint p;
if(!ready->GetFirst(p) ) //取出ready首元素不成功的话
{
mainForm->PcbHis->Lines->Add("READY队列中已经没有元素!");
if(!block->Empty())
{
mainForm->PcbHis->Lines->Add("系统死锁了!");
}
Disp();
ExitPcb();
return;
}
//否则取出首链给Curr_PCB,并删除之
Curr_PCB=p->toPcb;
ready->Dele(p);
//开始
mainForm->PcbHis->Lines->Add("正在运行ID: "+IntToStr(Curr_PCB->ID));
fcfsRun();
}
//---------------------------------------------------------------------------
void __fastcall TmainForm::FormCreate(TObject *Sender)
{
UnSetting();
}
//---------------------------------------------------------------------------
void __fastcall TmainForm::Button3Click(TObject *Sender)
{
Setting();
}
//---------------------------------------------------------------------------
void __fastcall TmainForm::Button4Click(TObject *Sender)
{
UnSetting();
}
//---------------------------------------------------------------------------
void ExitPcb()
{
mainForm->creatPCB->Enabled=false;
Curr_PCB=NULL;
}
void Disp()
{
pcbIndex* p,q;
pcb* r;
mainForm->PcbDisp->Lines->Clear();
mainForm->PcbDisp->Lines->Add("ID\t CPU\t IO\t FILE\t MEM");
mainForm->PcbDisp->Lines->Add("Total\t"+IntToStr(CPU_TIME)+'\t'
+IntToStr(IO_SRC)+'\t'+IntToStr(FILE_SRC)+'\t'
+IntToStr(MEM_SRC));
mainForm->PcbDisp->Lines->Add("Rest\t\t"
+IntToStr(restIo)+"\t"+IntToStr(restFile)
+"\t"+IntToStr(restMem));
mainForm->PcbDisp->Lines->Add("--------------------------------------------------------------------------------");
/*//显示全部
mainForm->PcbDisp->Lines->Add("ALL:");
r=pcb1->getHead();
r=r->next;
while(r!=NULL)
{
mainForm->PcbDisp->Lines->Add(
IntToStr(r->ID)+'\t'+IntToStr(r->cpuTime)+'\t'
+IntToStr(r->ioSrc)+'\t'+IntToStr(r->fileSrc)+'\t'
+IntToStr(r->memSrc));
r=r->next;
}
*/
//就绪队列
mainForm->PcbDisp->Lines->Add("\nready:");
p=ready->GetHead();
p=p->next;
while(p!=NULL)
{
r=p->toPcb;
mainForm->PcbDisp->Lines->Add(
IntToStr(r->ID)+'\t'+IntToStr(r->cpuTime)+'\t'
+IntToStr(r->ioSrc)+'\t'+IntToStr(r->fileSrc)+'\t'
+IntToStr(r->memSrc));
p=p->next;
}
//阻塞队列
mainForm->PcbDisp->Lines->Add("\nBlock:");
p=block->GetHead();
p=p->next;
while(p!=NULL)
{
r=p->toPcb;
mainForm->PcbDisp->Lines->Add(
IntToStr(r->ID)+'\t'+IntToStr(r->cpuTime)+'\t'
+IntToStr(r->ioSrc)+'\t'+IntToStr(r->fileSrc)+'\t'
+IntToStr(r->memSrc));
p=p->next;
}
//现在运行
r=Curr_PCB;
mainForm->PcbDisp->Lines->Add("\nrun:");
if(r!=NULL)
{
mainForm->PcbDisp->Lines->Add(
IntToStr(r->ID)+'\t'+IntToStr(r->cpuTime)+'\t'
+IntToStr(r->ioSrc)+'\t'+IntToStr(r->fileSrc)+'\t'