为什么80%的码农都做不了架构师?>>>
页式存储管理方案
位示图
内存被划分成2048块(页)。用32位字长的字存放位示图,为0的位表示该块尚未分配,为1的位表示该块已分配?
实习检查:
1、运行程序,由检查教师给出文件名,该文件中存有内存目前状况的位示图的数据(0和1的文件)。(程序应做提示,界面友好)。
2、你所编制的程序应读入数据,存放在相应的数据结构中。
3、显示友好的用户界面,由检查教师输入内存申请(总块数)。
4、根据申请和位示图状态,为用户分配内存,并建立页表。
5、输出位示图和页表
本程序包括主要实现位示图算法,比较简单.
页式存储管理方案
#include < stdio.h >
#include < iostream.h >
#include < string .h >
const int PAGES &#61; 256 ; // 定义总块数
const int WORD &#61; 32 ; // 定义字长
const int WORDNUM &#61; PAGES / WORD; // 定义总字数
typedef struct node{
char jobname[ 20 ];
int num;
int nums[PAGES];
struct node * next;
}jobs;
int table[WORDNUM][WORD];
int freenum &#61; 0 ;
jobs * head;
// 初始化函数
void initial(){
int i,j;
jobs * p;
// 初始化位示图
for (i &#61; 0 ;i < WORDNUM;i &#43;&#43; ){
for (j &#61; 0 ;j < WORD;j &#43;&#43; ){
table[i][j] &#61; 0 ;
}
}
// 初始化作业表头
p &#61; new jobs;
strcpy(p -> jobname, " null " );
p -> num &#61; 0 ;
p -> next &#61; NULL;
head &#61; p;
}
// 读入位示图初始数据
void readData()
{
int i,j;
FILE * fp;
char fname[ 20 ];
cout << " 请输入初始位示图数据文件名: " << endl;
cin >> fname;
if ((fp &#61; fopen(fname, " r " )) !&#61; NULL){
for (i &#61; 0 ;i < WORDNUM;i &#43;&#43; ){
for (j &#61; 0 ;j < WORD;j &#43;&#43; ){
fscanf(fp, " %d " , & table[i][j]);
if (table[i][j] &#61;&#61; 1 )
freenum &#43;&#43; ;
}
}
cout << " 初始位示图 " << endl;
for (i &#61; 0 ;i < WORDNUM;i &#43;&#43; ){
for (j &#61; 0 ;j < WORD;j &#43;&#43; ){
cout << table[i][j] << " " ;
}
cout << endl;
}
cout << " 总空闲块数: " << freenum;
}
else {
cout << " 文件不能打开 " << endl;
}
}
// 新加入作业函数
void add()
{
char jobname[ 20 ];
int num;
jobs * p;
int i,j,k &#61; 0 ;
cout << " 请输入新增的作业名: " ;
cin >> jobname;
cout << " 新增作业所需页数: " ;
cin >> num;
if (num <&#61; freenum){
freenum -&#61; num;
p &#61; new jobs;
strcpy(p -> jobname,jobname);
p -> num &#61; num;
for (k &#61; 0 ;k < num;k &#43;&#43; ){
i &#61; 0 ;
j &#61; 0 ;
while (table[i][j] &#61;&#61; 1 ){
j &#61; 0 ;
while (table[i][j] &#61;&#61; 1 )j &#43;&#43; ;
if (table[i][j] &#61;&#61; 1 )
i &#43;&#43; ;
}
p -> nums[k] &#61; i * WORD &#43; j;
table[i][j] &#61; 1 ;
}
p -> next &#61; head -> next;
head -> next &#61; p;
}
else {
cout << " 错误,当前剩余页数小于所需页数,请稍候再试:} " << endl;
}
}
// 完成作业函数
void finish()
{
char jobname[ 20 ];
jobs * p, * q;
int n,i,j,num,k;
cout << " 请输入完成的作业名: " ;
cin >> jobname;
p &#61; head -> next;
q &#61; head;
while (p !&#61; NULL){
if (strcmp(p -> jobname,jobname)){
q &#61; q -> next;
}
p &#61; p -> next;
}
p &#61; q -> next;
num &#61; p -> num;
for (k &#61; 0 ;k < num;k &#43;&#43; ){
n &#61; p -> nums[k];
i &#61; n / WORD;
j &#61; n % WORD;
table[i][j] &#61; 0 ;
}
freenum &#43;&#61; num;
q -> next &#61; p -> next;
delete p;
}
// 显示当前位示图函数
void view_table()
{
int i,j;
cout << " 当前位示图 " << endl;
for (i &#61; 0 ;i < WORDNUM;i &#43;&#43; ){
for (j &#61; 0 ;j < WORD;j &#43;&#43; ){
cout << table[i][j] << " " ;
}
cout << endl;
}
cout << " 总空闲块数: " << freenum << endl;
}
// 显示所有页表函数
void view_pages()
{
jobs * p;
int i;
p &#61; head -> next;
if (p &#61;&#61; NULL)cout << " 当前没有用户作业 " << endl;
else
cout << " 当前所有的用户作业页表情况 " << endl;
while (p !&#61; NULL){
cout << " 作业名: " << p -> jobname << " 所用块数: " << p -> num << endl;
cout << " 本作业所点块的序列是: " << endl;
for (i &#61; 0 ;i < p -> num;i &#43;&#43; ){
cout << p -> nums[i] << " " ;
}
cout << endl;
p &#61; p -> next;
}
}
// 显示版权信息函数
void version()
{
cout << endl << endl;
cout << " ┏━━━━━━━━━━━━━━━━━━━━━━━┓ " << endl;
cout << " ┃ 页式内存管理系统模拟程序 ┃ " << endl;
cout << " ┠───────────────────────┨ " << endl;
cout << " ┃ (c)All Right Reserved Neo ┃ " << endl;
cout << " ┃ sony006&#64;163.com ┃ " << endl;
cout << " ┃ version 2004 build 1122 ┃ " << endl;
cout << " ┗━━━━━━━━━━━━━━━━━━━━━━━┛ " << endl;
cout << endl << endl;
}
void main()
{
int t &#61; 1 ,chioce;
version();
initial();
readData();
while (t &#61;&#61; 1 ){
cout << endl << " &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; " << endl;
cout << " 页式内存管理系统模拟程序 " << endl;
cout << " &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; " << 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 &#61; 0 ;
break ;
default :
cout << " 选择错误 " << endl;
}
}
}