// ManegmentOfMemory.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
#include "time.h"
#include "ios.h"
#include "iomanip.h"
#define FINISH 0
#define RUN 1
#define WAITE 2
//#define NULL 0
typedef struct memory//内存块
{
int m_begin;
int m_size;
memory *next;
}memory;
typedef struct mLNode
{
//int m_num;
int m_total;
memory *next;
}mLNode;
typedef struct pcb
{
int p_size;
int status;
int pbegin;
//pcb *next;
}pcb;
/*typedef struct pLNode{
int p_num;
pcb *next;
}pLNode;*/
void DisplaymLNode();
void Displayg_pcb();
bool m_FirstFit();//用最先适用算法来进行内存的分配
bool InitialMemory();
bool InitialProcessing();
bool Getg_pro();//随机得到进程的数目
bool m_Recycle(pcb *p);//用于对释放内存的回收
int g_pro;//总的进程数
int g_mem;//总的内存数
unsigned long next = 1;//来产生随机数
mLNode g_mLNode;//未使用的内存块链表
pcb *g_pcb;//进程链表
//pLNode g_pLNode;
void srand(unsigned int seed)
{
next = seed;
}
int rand()
{
next = next * 1103515245 + 12345;
return (unsigned int)(next / 65536)%32768;
}
//初始各个进程,是由随机函数来进行实现的;并且每个进程的大小限制在10~400之间
bool InitialProcessing()
{
g_pcb = new pcb[g_pro];
int msize;
for(int i=0;i<g_pro;i++)
{
//随机产生进程所需空间
//srand( g_pcb[i-1].p_size );
while((msize = rand()%700) < 400 || (msize = rand()%700) > 600);
g_pcb[i].p_size = msize;
g_pcb[i].status = WAITE;
}
return true;
}
//初始化未分配出去的内存块的链表
bool InitialMemory()
{
srand( (unsigned)time( NULL ) );
int msize;
while((msize = rand()%2000) < 900 );
g_mem = msize;
g_mLNode.m_total = msize; //开始时内存还没有分配故总的数目为1且空闲为所刚产生的内存的大小
g_mLNode.next = (memory *)malloc(msize*sizeof(memory)+1);
g_mLNode.next->next = NULL; //刚建立时next应该指向为空
g_mLNode.next->m_begin = 1 ;
g_mLNode.next->m_size = msize;
return true;
}
//随机产生进程的个数并且控制进程的数目在5~12之间
bool Getg_pro()
{
srand( (unsigned)time( NULL ) );
while((g_pro = rand() % 100) < 5 || (g_pro > 8));
cout <<"本次程序所模拟的进程的个数: "<< g_pro-1 <<endl;
//cout <<"请输入任意大的一个数来产生随机数:"
cout << "提示:在表示进程状态时 0---->完 成" <<endl
<< " 1---->正在执行" <<endl
<< " 2---->就 绪" <<endl;
return true;
}
//用最先适用算法来实现内存的分配
bool m_FirstFit()
{
memory *m,*prem;//记录当前和前一次走过的空闲块
m = g_mLNode.next ;
prem = m;
for (int i=1 ;i<=g_pro ;i++){
if (g_pcb[i].status == RUN || g_pcb[i].status == FINISH) continue;
while (m!=NULL){
if (m->m_size >= g_pcb[i].p_size ){//满足进程对内存的需求
if (m->m_size == g_pcb[i].p_size){
g_mLNode.m_total -= g_pcb[i].p_size ;
g_pcb[i].pbegin = m->m_begin ;
prem->next = m->next ;
g_pcb[i].status = RUN;
}
else{
g_mLNode.m_total = g_mLNode.m_total - g_pcb[i].p_size ;
g_pcb[i].pbegin = m->m_begin ;
m->m_begin = m->m_begin + g_pcb[i].p_size;
m->m_size -= g_pcb[i].p_size ;
g_pcb[i].status = RUN;
}
break;//分配成功就跳出来
}
else{
prem = m;
m = m->next ;
}
}
}
return true;
}//m_FirstFit
//来管理内存的回收以及对上下空闲块的合并
bool m_Recycle(pcb *p)
{
memory *m,*prem,*mpcb;
mpcb = (memory *)malloc(sizeof(memory));
mpcb->next = NULL;
m = g_mLNode.next ;//指向第一个未分配的内存块
prem = m;
if (p->status == FINISH || p->status == WAITE) return false;
mpcb->m_begin = p->pbegin ;
mpcb->m_size = p->p_size ;
int size = p->p_size ;
int end = mpcb->m_begin + size - 1;//记录该进程块的始未地址和大小
p->p_size = 0;
p->pbegin = 0;
p->status = FINISH;//释放进程所占有的资源
g_mLNode.m_total += size;//更新未分配的内存总数
//回收内存,将释放内存与其相邻的空闲块相合并得到一个新的空闲内存块,准备将其放入到链表中
while ( m!=NULL ){
if (mpcb->m_begin == m->m_begin + m->m_size ){//空闲在左边
m->m_size = m->m_size +mpcb->m_size ;
mpcb = m;
//prem = m;
//m = m->next ;
break;
}
else if (end == m->m_begin - 1){//空闲块在右边
mpcb->m_size += m->m_size ;
//prem = m;
//m = m->next ;
break;
}
else {
prem = m;
m = m->next ;
}
}//while
m = g_mLNode.next ; prem = m;
while ( m!=NULL && (mpcb->m_begin > m->m_begin) ){
prem = m;
m = m->next ;
}
if (prem == m && m == g_mLNode.next )
{
if ( m->m_begin == mpcb->m_begin) g_mLNode.next = mpcb;
else{
g_mLNode.next = mpcb;
mpcb->next = m;
}
}
else {
mpcb->next = m ;
prem->next = mpcb;
}
return true;
}
//显示各个进程的现况
void Displayg_pcb()
{
cout <<"\n\n" <<endl;
cout << "此进各个进程的状态为:" << endl;
cout << "进程号\t所需内存\t进程状态" << endl;
for(int i=1 ;i<g_pro ;i++){
cout << i << "\t" << g_pcb[i].p_size << "\t\t" << g_pcb[i].status << endl;
}
}
void DisplaymLNode()
{
memory *m = g_mLNode.next;
cout <<"内存大小:"<< g_mem << endl;
cout <<"剩余内存:"<< g_mLNode.m_total <<endl;
int i=1;
while(m!=NULL)
{
cout <<"第"<<i<<"块空闲块 ";
cout <<"起始地址:"<< m->m_begin <<" 大小:"<< m->m_size << endl;//" 末尾地址:"<< m->m_begin + m->m_size -1 <<endl;
m = m->next ;
i++;
}
}
void main()
{
Getg_pro();//获取作业数
InitialProcessing();
InitialMemory();
Displayg_pcb();
DisplaymLNode();
m_FirstFit();
Displayg_pcb();
DisplaymLNode();
for (int j=1 ;j<=g_pro ;j++)
{
m_Recycle(&g_pcb[j]);
m_FirstFit();
Displayg_pcb();
DisplaymLNode();
}
}