进程用链表表示,资源也是用链表表示。类似一个十字链表。能在进程里面去查看资源。
#include<iostream>
#include<time.h>
using namespace std;
#define Sort 5
#define Number 20
struct Resource{
int r_No;
int max;
int allocation;
int need;
Resource *next;
};
struct Process{
int p_No;
Process * next;
Resource * Rhead;
};
Process *Creat_Process(int n){
Process *pP;
Process *pEnd;
Process *pHead;
pHead = NULL;
pP = new Process;
pEnd = pP;
for(int i = 0 ; i < n ; i++){
Resource *rR;
Resource *rEnd;
Resource *rHead;
if(pHead == NULL){
pHead = pP;
pP->p_No =i+1;
rHead = NULL;
rR = new Resource;
rEnd =rR;
for(int j = 0; j < Sort; j++)
{
if(rHead == NULL){
pP->Rhead=rHead = rR;
rR->r_No = j+1;
rR->max = rand()%Number + 1;
rR->allocation = rand()%rR->max;
rR->need = rR->max - rR->allocation;
}//if
else {
rEnd->next = rR;
rR->r_No = j+1;
rR->max = rand()%Number + 1;
rR->allocation = rand()%rR->max;
rR->need = rR->max - rR->allocation;
rEnd = rR;
}
rR = new Resource;
}//for
rEnd->next =NULL;
delete rR;
}//if
else{
pEnd->next=pP;
pEnd = pP;
pP->p_No = i+1;
rHead = NULL;
rR = new Resource;
rEnd =rR;
for(int f=0;f<Sort;f++){
if(rHead ==NULL){
pP->Rhead=rHead = rR;
rR->r_No = f+1;
rR->max = rand()%Number + 1;
rR->allocation = rand()%rR->max;
rR->need = rR->max - rR->allocation;
}//if
else{
rEnd->next=rR;
rEnd=rR;
rR->r_No =f+1;
rR->max = rand()%Number + 1 ;
rR->allocation = rand()%rR ->max;
rR->need = rR ->max - rR->allocation;
}//else
rR = new Resource;
}//for
rEnd->next =NULL;
delete rR;
}//else
pP = new Process;
}//for
pEnd->next=NULL;
delete pP;
cout<<"1"<<endl;
return (pHead);
}
int main(void)
{
srand(time(NULL));
Process *show;
show = Creat_Process(5);
cout<<"2"<<endl;
while(show)
{
cout<<"P_NO: "<<show->p_No<<"Re: "<<show->Rhead->r_No<<endl;
for(int i=0;i<5;i++){
cout<<"R_NO:"<<show->Rhead->r_No<<" alloc: "<<show->Rhead->allocation<<endl;
show->Rhead=show->Rhead->next;
}
show=show->next;
}
return 0;
}