Storage management program page
bit map
memory is divided into 2048 blocks (pages). With the word stored bit map of 32-bit word length, the bit 0 indicates that the block has not been assigned, the bit 1 indicates that the block has been allocated?
Practice inspection:
1, to run the program, the file name given by the teacher checks the file bit map data memory there in the current situation (the document 0 and 1). (Procedure should be done prompt, friendly interface).
2, the program should be compiled you read data stored in the corresponding data structure.
3, a user-friendly interface, the application input memory (total number of blocks) by the inspection teacher.
4, according to the application and the state diagram bit, allocate memory for the user, and to establish the page table.
5, the output bit map and page table
This program includes a main algorithm implemented Bitmap, is relatively simple.
![](http://static.oschina.net/uploads/img/201601/15103800_TjWD.gif)
![](http://static.oschina.net/uploads/img/201601/15103800_CXRU.gif)
#include < stdio.h >
#include < iostream.h >
#include < string .h >
const int PAGES = 256 ; // 定义总块数
const int WORD = 32 ; // 定义字长
const int WORDNUM = PAGES / WORD; // 定义总字数
typedef struct node{
char jobname[ 20 ];
int num;
int nums[PAGES];
struct node * next;
}jobs;
int table[WORDNUM][WORD];
int freenum = 0 ;
jobs * head;
// 初始化函数
void initial(){
int i,j;
jobs * p;
// 初始化位示图
for (i = 0 ;i < WORDNUM;i ++ ){
for (j = 0 ;j < WORD;j ++ ){
table[i][j] = 0 ;
}
}
// 初始化作业表头
p = new jobs;
strcpy(p -> jobname, " null " );
p -> num = 0 ;
p -> next = NULL;
head = p;
}
// 读入位示图初始数据
void readData()
{
int i,j;
FILE * fp;
char fname[ 20 ];
cout << " 请输入初始位示图数据文件名: " << endl;
cin >> fname;
if ((fp = fopen(fname, " r " )) != NULL){
for (i = 0 ;i < WORDNUM;i ++ ){
for (j = 0 ;j < WORD;j ++ ){
fscanf(fp, " %d " , & table[i][j]);
if (table[i][j] == 1 )
freenum ++ ;
}
}
cout << " 初始位示图 " << endl;
for (i = 0 ;i < WORDNUM;i ++ ){
for (j = 0 ;j < WORD;j ++ ){
cout << table[i][j] << " " ;
}
cout << endl;
}
cout << " 总空闲块数: " << freenum;
}
else {
cout << " 文件不能打开 " << endl;
}
}
// 新加入作业函数
void add()
{
char jobname[ 20 ];
int num;
jobs * p;
int i,j,k = 0 ;
cout << " 请输入新增的作业名: " ;
cin >> jobname;
cout << " 新增作业所需页数: " ;
cin >> num;
if (num <= freenum){
freenum -= num;
p = new jobs;
strcpy(p -> jobname,jobname);
p -> num = num;
for (k = 0 ;k < num;k ++ ){
i = 0 ;
j = 0 ;
while (table[i][j] == 1 ){
j = 0 ;
while (table[i][j] == 1 )j ++ ;
if (table[i][j] == 1 )
i ++ ;
}
p -> nums[k] = i * WORD + j;
table[i][j] = 1 ;
}
p -> next = head -> next;
head -> next = p;
}
else {
cout << " 错误,当前剩余页数小于所需页数,请稍候再试:} " << endl;
}
}
// 完成作业函数
void finish()
{
char jobname[ 20 ];
jobs * p, * q;
int n,i,j,num,k;
cout << " 请输入完成的作业名: " ;
cin >> jobname;
p = head -> next;
q = head;
while (p != NULL){
if (strcmp(p -> jobname,jobname)){
q = q -> next;
}
p = p -> next;
}
p = q -> next;
num = p -> num;
for (k = 0 ;k < num;k ++ ){
n = p -> nums[k];
i = n / WORD;
j = n % WORD;
table[i][j] = 0 ;
}
freenum += num;
q -> next = p -> next;
delete p;
}
// 显示当前位示图函数
void view_table()
{
int i,j;
cout << " 当前位示图 " << endl;
for (i = 0 ;i < WORDNUM;i ++ ){
for (j = 0 ;j < WORD;j ++ ){
cout << table[i][j] << " " ;
}
cout << endl;
}
cout << " 总空闲块数: " << freenum << endl;
}
// 显示所有页表函数
void view_pages()
{
jobs * p;
int i;
p = head -> next;
if (p == NULL)cout << " 当前没有用户作业 " << endl;
else
cout << " 当前所有的用户作业页表情况 " << endl;
while (p != NULL){
cout << " 作业名: " << p -> jobname << " 所用块数: " << p -> num << endl;
cout << " 本作业所点块的序列是: " << endl;
for (i = 0 ;i < p -> num;i ++ ){
cout << p -> nums[i] << " " ;
}
cout << endl;
p = p -> next;
}
}
// 显示版权信息函数
void version()
{
cout << endl << endl;
cout << " ┏━━━━━━━━━━━━━━━━━━━━━━━┓ " << endl;
cout << " ┃ 页式内存管理系统模拟程序 ┃ " << endl;
cout << " ┠───────────────────────┨ " << endl;
cout << " ┃ (c)All Right Reserved Neo ┃ " << endl;
cout << " ┃ [email protected] ┃ " << endl;
cout << " ┃ version 2004 build 1122 ┃ " << endl;
cout << " ┗━━━━━━━━━━━━━━━━━━━━━━━┛ " << endl;
cout << endl << endl;
}
void main()
{
int t = 1 ,chioce;
version();
initial();
readData();
while (t == 1 ){
cout << endl << " =========================================== " << endl;
cout << " 页式内存管理系统模拟程序 " << endl;
cout << " =========================================== " << endl;
cout << " 1.加入新作业 2.完成作业 3.显示当前内存位示图 4.显示所有作业页表 0.退出 " << endl;
cout << " 请选择: " ;
cin >> chioce;
switch (chioce){
case 1 :
add();
break ;
case 2 :
finish();
break ;
case 3 :
view_table();
break ;
case 4 :
view_pages();
break ;
case 0 :
t = 0 ;
break ;
default :
cout << " 选择错误 " << endl;
}
}
}
Reproduced in: https: //my.oschina.net/garyun/blog/602904