分页式内存管理的模拟系统

#include<iostream.h>
#include<string.h>
#include<malloc.h>
#include "stdlib.h"
#include "conio.h"
#define NULL 0
#define LEN sizeof(struct pagetable)
#define LENJ sizeof(struct jobtable)   
//作业的页表结构体
struct pagetable{
	int pagenum;//页号
	int blocknum;//块号
	struct pagetable* next;//指向下一项
};
//作业表结构体
struct jobtable{
	char jobname[10];//作业的名字
	int  jobsize;//作业的大小
	struct pagetable* head;//指向作业的页表
	struct jobtable* next;//指向下一个作业
};
int weitu[8][8];//位示图
int weitusize=2;//内存每块为2K
int blank=64;//当前空闲的块号
int sign=0;//内存分配错误标志
int list=0;//作业序列
int n=0;//内存作业队列标志
int n1=0;//等待作业队列标志
struct jobtable *r,*s;
struct jobtable *r1,*s1;

struct jobtable *jobhead;//作业表
struct jobtable *waithead;//等待作业表
//初始化位示图
void initweitu()
{
	for(int i=0;i<8;i++)
		for(int j=0;j<8;j++)
			weitu[i][j]=0;
	/*weitu[0][0]=1;
	weitu[0][1]=1;
	weitu[0][4]=1;
	weitu[0][5]=1;
	weitu[0][6]=1;
	weitu[1][1]=1;
	weitu[1][3]=1;
	weitu[1][5]=1;
	weitu[3][0]=1;
	weitu[3][7]=1;
    blank=54;*///当前空闲的块号
}
//打印位示图子模块
void display()
{
	cout<<"********位示图**********\n";
	for(int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
		   cout<<weitu[i][j]<<"  ";
		}
	    cout<<endl;
	}
	cout<<"************************\n";
	//查询当前空闲块
	cout<<"当前空闲块数"<<blank<<endl;
}
//查询等代队列的作业
void displaywait()
{
	struct jobtable *temp;
	temp=waithead;
	if(waithead==NULL)cout<<"等待队列为空!"<<endl;
	while(temp!=NULL)
	{
		cout<<"作业"<<temp->jobname<<"申请"<<temp->jobsize<<"KB过大,进入等待队列"<<endl;
		temp=temp->next;
	}
	

}
//查询加载到内存的作业队列
void displayjob()
{
	struct jobtable *temp;
	temp=jobhead;
	if(jobhead==NULL)cout<<"内存中暂时还没有作业!"<<endl;
	while(temp!=NULL)
	{
		cout<<"作业"<<temp->jobname<<"申请"<<temp->jobsize<<"KB已进入内存"<<endl;
		temp=temp->next;
	}
}
//查询作业的页表
void findpagetable(struct pagetable *head)
{
	struct pagetable *p;
	p=head;
	cout<<"****作业页表****"<<endl;
	cout<<" 页号  块号"<<endl;
	while(p!=NULL)
	{
		cout<<"  "<<p->pagenum<<"     "<<p->blocknum;
		cout<<endl;
		p=p->next;	
	}
	cout<<"**************"<<endl;
}

void yebiao()
{
	char strname[10];
	struct jobtable *temp;
	cout<<"请输入作业的名字:";
	cin>>strname;
	temp=jobhead;
	while(temp!=NULL)
	{
		if(!strcmp(temp->jobname,strname))
			break;
           temp=temp->next;
	}
	if(temp==NULL)
	{
		cout<<"该作业不在主存,无法查看该作业的页表"<<endl;
	}
	else
	{
	    cout<<"作业"<<temp->jobname<<"的页表"<<endl;
		findpagetable(temp->head);
	}

}
//分配内存
//jobsize:作业的大小
struct pagetable *fenpei(int jobsize)
{
	int num=0;
	int i,j,k;
	int jobblocknum;
	struct pagetable *head,*p1,*p2;
	head=NULL;//初始化指针
    sign=0;//初始化标志
    jobblocknum=(jobsize+1)/weitusize;//作业所占的块数
	if(jobblocknum>blank)
	{
		cout<<"内存分配失败!等待系统释放内存资源..."<<endl;
		sign=1;//内存分配失败
	}
	else
	{
	    	
		p1=p2=(struct pagetable*)malloc(LEN);

		for(k=0;k<jobblocknum;k++)
		{
			for(i=0;i<8;i++)
			{
				for(j=0;j<8;j++)
				{
					if(weitu[i][j]==0)//查找空闲快
					{
						weitu[i][j]=1;//修改为占用标志 1
						blank--;//空闲块数减一
						//登记作业的页表
						p1->pagenum=num;
						p1->blocknum=(i*8+j)+1;
						num=num+1;
						if(num==1) head=p1;
						else p2->next=p1;
						p2=p1;
                        p1=(struct pagetable*)malloc(LEN);
                        goto out;
					}
				}
			}
out:
		p2->next=NULL;
		}


	}
	return(head);
}
void jiazai()
{
	char strname[10];
	int size;
	cout<<"请输入作业的名字:";cin>>strname;
	strcpy(r->jobname,strname);
	cout<<"请输入作业的大小:";cin>>size;
	r->jobsize=size;
	r->head=fenpei(r->jobsize);
	if(sign==0)
	{
	  n++;
	  if(n==1) jobhead=r;
	  else s->next=r;
      s=r;
	  cout<<"输入的作业"<<r->jobname<<"已成功加载到内存中"<<endl;
  	  r=(struct jobtable*)malloc(LENJ);
      s->next=NULL;
	}
	else 
	{
		strcpy(r1->jobname,strname);
		r1->jobsize=size;
		n1++;
		if(n1==1) waithead=r1;
		else s1->next=r1;
        s1=r1;
		r1=(struct jobtable*)malloc(LENJ);
        s1->next=NULL;
	}
}
//回收内存
//传入一个作业的页表头指针
void huishou(struct pagetable *head)
{
	int row,clom;
	struct pagetable *p,*p1;
	struct jobtable *temp,*pretemp;
	int jobblocknum;
	int sign00=0;
	p=head;
	while(p!=NULL)
	{
		row=(p->blocknum-1)/8;
		clom=(p->blocknum-1)%8;
	    weitu[row][clom]=0;
		blank++;
		p1=p;
		p=p->next;
		free(p1);
	}
	//每次回收后去扫描作业的等代队列
	temp=pretemp=waithead;//将等待队列中的作业加载到内存中
	while(temp!=NULL)
	{
	  jobblocknum=(temp->jobsize+1)/weitusize;
	  if(jobblocknum<=blank)
	  {
		   strcpy(r->jobname,temp->jobname);
		   r->jobsize=temp->jobsize;
		   r->head=fenpei(r->jobsize);
		   n++;
		   if(n==1) jobhead=r;
		   else s->next=r;
           s=r;
		   r=(struct jobtable*)malloc(LENJ);
           s->next=NULL;
	  cout<<"等待队列中的作业"<<temp->jobname<<"已成功加载到内存中"<<endl;
	  waithead=temp->next;
	  if(waithead==NULL) n1=0;
	  
	  pretemp=temp;
	  sign00=1;
	  }
	  if(sign00==0) break;
      temp=temp->next;
	  sign00=0;
	  free(pretemp);
	}
	
}
void cexiao()
{
	char strname[10];
	struct jobtable *temp,*pretemp;
	cout<<"请输入要撤消作业的名字:";
	cin>>strname;
	temp=pretemp=jobhead;
	while(temp!=NULL)
	{
		if(!strcmp(temp->jobname,strname))
			 break;
		pretemp=temp;
        temp=temp->next;
	}
	if(temp==NULL)
	{
		cout<<"作业"<<strname<<"不在主存,操作有误"<<endl;
	}
	else
	{
		cout<<"回收作业"<<temp->jobname<<"成功"<<endl;
		huishou(temp->head);
		if(!strcmp(pretemp->jobname,temp->jobname)) 
		{jobhead=temp->next;
		if(s==temp) s=pretemp;
		if(jobhead==NULL) n=0;
		}
		else {pretemp->next=temp->next;if(s==temp) s=pretemp;}
		free(temp);
	}

}
//主程序:
int main()
{
	char ch;
	//初始化工作
	jobhead=NULL;
	waithead=NULL;
	initweitu();
	r=s=(struct jobtable*)malloc(LENJ);
	r1=s1=(struct jobtable*)malloc(LENJ);
	//菜单功能
	cout<<"**********分页式内存管理的模拟系统************"<<endl;
	cout<<"查询内存分配位示图,请按:1\n"
		<<"查询  等待作业队列,请按:2\n"
		<<"查询内存中作业队列,请按:3\n"
		<<"查询  某作业的页表,请按:4\n"
		<<"装载  一  个 作 业,请按:5\n"
		<<"撤消  一  个 作 业,请按:6\n"
		<<"退   出    系   统,请按:0\n";
	cout<<"**********************************************"<<endl;
    cout<<"请选择操作码:"; 
	cin>>ch;
    do{
		if(ch=='0') {cout<<"你真的要退出系统吗?(Y/N):";
			         cin>>ch;
                     if(ch=='Y') break;
					 else{cout<<"请选择操作码:"; 
					 cin>>ch;}
		}
	//用switch语句实现任务选择功能
    switch(ch)
	{

	case '1':display();break;
	case '2':displaywait();break;
	case '3':displayjob();break;
	case '4':yebiao();break;
	case '5':jiazai();break;
	case '6':cexiao();break;
	default:cout<<"您的输入有错误,请重新输入\n";
	}
	cout<<"请选择操作码:"; 
	cin>>ch;
	if(ch=='0') {cout<<"你真的要退出系统吗?(Y/N):";
			         cin>>ch;
                     if(ch=='Y') break;
					 else{cout<<"请选择操作码:"; 
					 cin>>ch;}
		}
	}while(1);
	cout<<"谢谢使用分页式内存管理的模拟系统"<<endl;
	return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值