实验报告3

存储管理程序设计

#include<bits/stdc++.h>

#define page_length 7
#define block_number 4
#define block_length 1024
#define disexist 0 
#define work_number 12
using namespace std;
/*需要增加个模拟块结构 
现在出现缺页问题 
*/
void init_pageT();
void init_workT();
void print();
void print1();
int transform(int a,int b);
void start();

int block[block_number] = {-1,-1,-1,-1};//内存块对应的页号 
int point = 0;// 内存块指针 

struct page_table{
	int page_id;
	int exist_flag;
	int block_id;
	int revise_flag;
	int location;
}process[page_length];

struct pro{
	char op[5];
	int page_id;
	int adress;
}work[work_number];


int main(){
	init_pageT();
	print();
	init_workT();
	print1();
	start();
}
void init_pageT(){
	int a[page_length] = {5,8,9,1,0,0,0};
	// init exist-flag 
	int b[page_length] = {11,12,13,21,22,23,121};
	for(int i = 0;i < page_length;i++){
		process[i].exist_flag = 0;
	} 
	for(int i = 0;i < block_number;i++){
		process[i].exist_flag = 1;
	}
	// init page_id
	for(int i = 0;i < page_length;i++){
		process[i].revise_flag = 0;
		process[i].page_id = i;
		process[i].block_id = a[i];
		process[i].location = b[i];
	}
	
}
void init_workT(){
	cout << "===================================="<<endl;
	string a[work_number] ={"+","+","x","存","取","-","移位","+","存","取","+","存"};
	int b[work_number] = {0,1,2,3,0,6,4,5,1,2,4,6};
	int c[work_number] = {70,50,15,21,56,40,53,23,37,78,1,84};
	for(int i = 0;i < work_number;i++){ 
		strcpy(work[i].op,a[i].c_str());
		work[i].page_id = b[i];
		work[i].adress = c[i];
	}
}
void print1(){
	cout << "***work table***"<<endl;
	cout<<endl; 
	for(int i = 0;i < work_number;i++)
		cout<<setw(5)<<work[i].op<<setw(10)<<work[i].page_id <<setw(10)<<work[i].adress <<endl;
}
void print(){
	cout<<"!!!page table!!!"<<endl<<endl;
	for(int i = 0;i <= 40;i++){
		cout<<"=";
	}cout<<endl;
	for(int i = 0;i < page_length;i++){
		cout<<process[i].page_id <<"    "<<process[i].exist_flag <<"    "<<process[i].block_id <<"    "<< process[i].revise_flag <<"    "<<process[i].location<<endl;
	}
} 
void start(){
	int j = 0;
	point = 0;
	while(j < work_number){
		if(work[j].page_id > page_length-1){
			cout<<"irror" << endl;
			continue;
			//页表长 
		}else{
			for(int i = 0;i < block_number;i++){
				if(block[i] == -1){
					block[i] = work[j].page_id;
					cout << "*"<<work[j].page_id<<endl;
					cout <<"IN"<<work[j].page_id<<endl;
					break;
				}
			}
			if(process[work[j].page_id].exist_flag == 1){
				int ad = transform(work[j].page_id,work[j].adress);
				if(!strcmp(work[j].op,"存") || !strcmp(work[j].op,"写")){
					process[work[j].page_id].revise_flag = 1;
				}
				cout << "real path:" << ad << endl;
			}//在内存内 
			else //中断处理 
			{
				int temp;
				cout <<"*"<<work[j].page_id<<endl;
				int tmp = block[point];
				if(process[tmp].exist_flag == 1){
					cout << "OUT"<<tmp<<endl;
					temp = process[tmp].block_id;
					process[tmp].exist_flag = 0;
					process[tmp].block_id = 0;
				}
				cout <<"IN"<<work[j].page_id<<endl;
				block[point] = work[j].page_id;
				process[work[j].page_id].exist_flag = 1;
				process[work[j].page_id].block_id = temp;
				print();
				point++;
				point = point % 4;
				continue;
			}
		}
		j++;
	} 
}
int transform(int a,int b){
	return process[a].block_id * block_length + b;
}//求绝对地址 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值