#include
#include
#include
#include
using namespace std;
# define the random (x) (rand () % x)
//page table entries structure
Typedef struct _pagetableitem
{
Int pagenum;//page number
Int blocknum; No.//block
} pgtabitem;//page table
Typedef pgtabitem * pagetable;
//request table structure
Typedef struct _reqtable
{
Unsigned pid; No.//process
Unsigned reqpagenum;//request page number
Pagetable pgtabadr;//page table starting address
Bool state;//state
} reqtabitem;
List
Unsigned pagenum=random (80) + 21;//memory page number 21-100
Unsigned pagesize=random (16) + 5;//page size 5-20
Unsigned pnum=random (4) + 5;//process the number of 5-8
Int * mempage=new int (pagenum);//integer array was used to simulate the memory page number
/* * initializes the memory page/
Void Init_Mempage (void)
{
int i=0;
for(i=0; i
}
/get the memory usage */*
Int Get_Mempagenum (void)
{
int sum=0;
for(int i=0; i
Sum++;
Return the sum.//determine how much memory page is already in use
}
/* initialize the default request table */
Void Init_Reqtable (void)
{
int i;
for(i=1; i<=int (pnum); I++)
{
Reqtabitem preq;
Preq. Pid=I;
Preq. Reqpagenum=random (4) + 2;//process request page size to 5
Preq. State=false;
Preq. Pgtabadr=NULL;
Reqtable. Push_back (preq);//in turn pressure into the container
}
}
/* allocates memory for the default process */
Void Init_DistMem (void)
{
Int reqpnum;//process request page number
int i;
List
For (; Pos!=reqtable. End (); Pos++)
{
Pos reqpnum=(*). Reqpagenum;
If (reqpnum> Int (Get_Mempagenum ()))//whether the number of memory page request is greater than the rest of the
{
Cout<& lt;" Not enough memory!"
The else
{
Pos (*). The state=true;
Pagetable temp=new pgtabitem [reqpnum];//the new temporary page table entries array
If (temp==NULL)
{
Cout<& lt;" Memory allocation failure!"
}
Pos (*). Pgtabadr=temp;
for(i=0; i
\ [I] pagenum=I;//page table page number
Int randnum=random (pagenum) + 1;//randomly generated a block number
While (mempage [randnum]==1)//looking for unused physical block
Randnum=random (pagenum) + 1;
\ [I] blocknum=randnum;//page table block number
Mempage [randnum]=1;
}
}
}
}
/* manually create process and allocate memory */
Void Dist_Mem (void)
{
int i;
Reqtabitem preq;//the newly created process record
Int pid; No.//process
Int reqpnum;//request page number
Bool flag=false;
Do {
Cout<& lt;" Please enter the process number: ";
Flag=false;
Cin> Pid;
For (list
{
If (pos (*). Pid==pid)
{
Flag=true;
Cout<& lt;" The process number already exists, please input again "& lt;
}
}
} while (flag==true);//loop until input Pid met condition
Preq. Pid=pid;
Cout<& lt;" Please input to the number of pages: ";
Cin> Reqpnum;
Preq. Reqpagenum=reqpnum;
Preq. State=false;
Preq. Pgtabadr=NULL;
Reqpnum=preq. Reqpagenum;
If (reqpnum> Get_Mempagenum ())
{
Cout<& lt;" Not enough memory, process creation failed!"
The else
{
Preq. State=true;
Pagetable temp=new pgtabitem [reqpnum];
If (temp==NULL)
{
Cout<& lt;" Memory allocation failure!"
}
Preq. Pgtabadr=temp;
for(i=0; i
\ [I] pagenum=I;//page table page number
Int randnum=random (pagenum) + 1;//randomly generated a block number
While (mempage [randnum]==1)
Randnum=random (pagenum) + 1;
\ [I] blocknum=randnum;//page table block number
Mempage [randnum]=1;
}
}
Reqtable. Push_back (preq);//to add the record of the process to request table
}
/* program at the end of the release of application of dynamic memory */
Void Destroy (void)
{
List
For (pos=reqtable. The begin (); Pos!=reqtable. End (); Pos++)
{
If (pos (*). State==true)
Delete [] (* (pos). Pgtabadr;
}
Reqtable. The clear ();
}
/* print process request table */
Void PrintReqtable (void)
{
Cout
For (pos=reqtable. The begin (); Pos!=reqtable. End (); Pos++)
{
Cout<& lt;" | "& lt;