📄 复件 (2) main.cpp
字号:
#include "head.h"
//////////////////////////////////////////////////////////////Common
int getNowTime(){//取得现在时间
return time(NULL);
}
void wait(int n){//等待n秒钟
int now=getNowTime();
while(getNowTime()<=(now+n));
}
int getNextID(){//取得下一个进程的ID
return ++ID;
}
int getRandResourceClass(){//取得随机的资源种类
return rand()%M+1; ; // 改为一个随机生成的数字(0~M)
}
int getRandResourceNum(){//取得随机的资源个数
return rand()%K+1;// 改为一个随机生成的数字(0~M)
}
int getRandRunTime(){//取得随机的执行时间表
return rand()%T+1;
}
int getRandProcNum(){//随机取得进程数
return rand()%RandProcNum+1;
}
int getRandNum(){//取一个随机数
return rand()%(3*TimePiece)+1;//至多在3个时间片内产生一个随机进程
}
bool isCreateNewProc(){
int temp=rand()%100;
if(temp<=RATE) return true;
else return false;
}
/////////////////////////////////////////////////////////////////Device
void InitDevice(){//初始化虚拟设备 共M*N个
int i=0,j=0;
int temp=0;
while(i<M){
while(j<N){
DEVICE[i][j].id=i*N+j;
DEVICE[i][j].status=DReady;
j++;
}
i++;
}
}
DeviceStatus getDeviceStatus(int m,int n){//取得第m类,第n个资源的状态
return DEVICE[m][n].status;
}
int getAvailableDeviceCount(int m){//取得第m类可用资源的个数
int temp=0,i=0;
while(i<N){
if(DEVICE[m][i].status==DReady) temp++;
i++;
}
return temp;
}
void setDeviceBusy(int m,int k){//设置第m类共K个设备为busy状态
int temp=0;
int count=0;
while(temp<N&&count<=k){
DEVICE[m][temp].status=DBusy;
count++;
temp++;
}
}
void setDeviceReady(int m,int k){//设置设备第M类资源的共K个设备为Ready状态
int temp=0;
int count=0;
while(temp<N&&count<=k){
if(DEVICE[m][temp].status==DBusy) {
DEVICE[m][temp].status=DReady;
count++;
}
else temp++;
}
}
int ckeckDeviceAvailable(int m,int k){//检查设备是否有K个可用的设备
int temp=getAvailableDeviceCount(m);
if(temp>=k) return SUCCESS;
else return FAILURE;
}
void disPlayDeviceStatus(int m,int n){//显示第m类资源第n个设备的状态信息
printf("%s",DeviceStatusOut[DEVICE[m][n].status]);
}
/////////////////////////////////////////////////////////////////////Proc
void createRandProc(Proc &p){//创建随机进程
p.id=getNextID();
printf("当前创建 P%d 进程...\n",p.id);
p.superId=0;
p.initTime=getNowTime()-INITTIME;
p.leftTime=p.runTime=getRandRunTime();
p.m=getRandResourceClass();
p.k=getRandResourceNum();
int temp;
temp=ckeckDeviceAvailable(p.m,p.k);
if(temp==SUCCESS) {
setDeviceBusy(p.m,p.k);
p.status=PReady;
}
else p.status=PBlock;
//printf("%s",ProcStatusOut[p.status]);
}
void running(PCB &P,Proc &p){
changeProcStatus(P,PReady);
if(p.status==PReady){
if(p.leftTime-TimePiece>0){//若剩余执行时间大于时间片则执行
p.status=PRun;
displayPCB(P);
wait(TimePiece);
p.leftTime-=TimePiece;
}
else {//否则
p.status=PRun;
displayPCB(P);
p.leftTime=0;
wait(p.leftTime);
setDeviceReady(p.m,p.k);
}
clearProc(P);
}
else{
int temp;
temp=ckeckDeviceAvailable(p.m,p.k);
if(temp==SUCCESS) {
setDeviceBusy(p.m,p.k);//有一个延时从下一个时间片开始执行
p.status=PReady;
}
displayPCB(P);
}
}
void destory(Proc p){
setDeviceReady(p.m,p.k);
}
void displayProc(Proc p){//显示进程的信息
printf(" |%d\t|%d\t |%s |%d\t | %d\t| %d\t | %d\t| %d\t|\n",p.id,p.superId,ProcStatusOut[p.status],p.initTime,p.runTime,p.leftTime,p.m,p.k);
}
/////////////////////////////////////////////////////////////////////PCB
int initPCB(PCB &P){//初始化一个带表头链表
P = (PCB)malloc(sizeof(PCBNode)); // 生成新结点
P->next=NULL;
return OK;
}
int countProc(PCB &P){//取得PCB表中进程的个数
PCB s=P->next;
int temp=0;
while(s!=NULL){
temp++;
s=s->next;
}
return temp;
}
int insertProc(PCB &P,Proc p){//在PCB表中插入一个进程
PCB s,temp;
s = (PCB)malloc(sizeof(PCBNode)); // 生成新结点
s->proc = p;
s->next =NULL; // 插入L中
temp=P;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=s;
return OK;
}
int clearProc(PCB &P){//检查
PCB s,temp;
temp=P;
while(temp->next!=NULL){
s=temp->next;
if(s->proc.leftTime==0){
printf("进程 P%d 已经完成!\n",s->proc.id);
wait(1);
destory(s->proc);
temp->next=s->next;
free(s);
break;
}
temp=temp->next;
}
return OK;
}
void displayPCB(PCB &P){//显示所有进程
PCB temp;
temp=P;
printf("┏----------------------------------------------------------------------┓\n");
printf(" |ID----|SuperID--|-Status-|InitTime|--RunTime--|LeftTime|---M--|---K---|\n");
while(temp->next!=NULL){
displayProc(temp->next->proc);
temp=temp->next;
}
printf("┗----------------------------------------------------------------------┛\n");
}
void createRandPCB(PCB &P){
initPCB(P);
//srand(time(0)) ;//初始化随机种子。
int temp=getRandProcNum();
while(temp>0){
Proc p;
//srand(time(0)) ;//初始化随机种子。
createRandProc(p);
//system("cls");
displayProc(p );
wait(getRandNum());//getRandNum()
insertProc(P,p);
temp--;
}
}
int changeProcStatus(PCB &P,ProcStatus status){//改变进程状态
PCB temp,s;
temp=P;
while(temp->next!=NULL){
s=temp->next;
if(s->proc.status==PRun){
s->proc.status=status;
break;
}
temp=temp->next;
}
return SUCCESS;
}
int runProc(PCB &P,int id){//执行PCB表中进程ID为id的进程
PCB s,temp=P;
while(temp->next!=NULL){
s=temp->next;
if(s->proc.id==id){
printf("程序运行时间: %d\n",getNowTime()-INITTIME);
printf("当前 P%d 正在运行中...\n",id);
running(P,s->proc);
if(isCreateNewProc()==true){
Proc p;
createRandProc(p);
insertProc(P,p);
}
return SUCCESS;
}
temp=temp->next;
}
return FAILURE;
}
void Procede(){
INITTIME=getNowTime();
srand(time(0));
InitDevice();
PCB pcb;
createRandPCB(pcb);
displayPCB(pcb);
int temp=1;
while(pcb->next!=NULL){
system("cls");
runProc(pcb,temp);
temp=(++temp)%(ID+1);
}
displayPCB(pcb);
}
void main(){
Procede();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -