?? computerosexp_03_02.cpp
字號:
#include "stdafx.h"
#include <iostream.h>
#include <time.h>
#include <afx.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "Sys.h"
#include "PCB.h"
int GetRandom(int min, int max)//獲取從min到max的隨機數
{
return rand() % (max + 1 -min) + min;
}
void main()
{
int pcbNum = 0, resourceNum = 0;
Sys *sys = new Sys();
sys->InitSys();
const int resLen = sys->resLen;
const int pcbLen = sys->pcbLen;
int *req = new int[resLen];
char **name = new char*[pcbLen];
int ccc = 0;
while(true)
{
PCB *tempPCB = sys->headPCB;
srand((unsigned)time(NULL));//保證是獲取到的隨機數
while(tempPCB != 0)
{
sys->PrintRemainRes();
sys->PrintSys();
cout << "進程" << tempPCB->name << "開始請求資源... ..." << endl;
Res *tempRes = sys->headRes;
int i = 0;
cout << "進程" << tempPCB->name << "欲請求的資源如下:" << endl;
while(tempRes !=0 && i < resLen)
{
req[i] = GetRandom(0, tempPCB->Need[i]);
cout << tempRes->name << " " << req[i] << " ";
tempRes = tempRes->nextRes; i++;
}
cout << endl;
tempPCB->Run(sys->Remain, req);
if(sys->IsSecurity(name) == true)
{
cout<< "進程" << tempPCB->name << "請求成功!" << "存在安全序列如下:" << endl;
for(int j = 0; j < pcbLen; j++)
cout << name[j] << " ";
cout << endl;
}
else
{
cout << "不存在安全序列!" << "進程" << tempPCB->name << "請求失敗!" << endl;
tempPCB->Resever(sys->Remain, req);
}
tempPCB = tempPCB->nextPCB;
cout << endl << endl << endl;
}
ccc++;
if(ccc > 0)
break;
}
sys->PrintRemainRes();
delete sys, req, name;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -