⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yeshi.c

📁 用C写的页式存储管理程序
💻 C
字号:
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#include <process.h>
#define  TRUE     1
#define  FALSE    0
#define  INTRNUM  12
#define  BLOCKLENGTH  1024
#define  PAGENUM   7 
#define  LOADBLOCK  4
typedef  enum
{
null = 0,
add = 1,
sub = 2,
mul = 3,
save = 4,
load = 5,
rep  = 6
}operator;  /*操作码,  存指令, 非存指令, 空指令三种*/

typedef struct
{
operator  optr;   /*操作码*/
unsigned  page;   /*访问的页号*/
unsigned  pageaddr;  /*访问的页内地址*/
}instruction;    /*指令格式*/

typedef struct
{
unsigned  page;  /*页号*/
short     flag;  /*标志位*/
unsigned  block;  /*主存块号*/
short     modify;  /*修改标志*/
long      diskaddr; /*磁盘位置*/
}pagerecord;  /*页表中每项记录的结构*/

instruction  instrset[INTRNUM] =
{
{add,   0,   70},
{add,   1,   50},
{mul,   2,   15},
{save,  3,   21},
{load,  0,   56},
{sub,   6,   40},
{rep,   4,   53},
{add,   5,   23},
{save,  1,   37},
{load,  2,   78},
{add,   4,   01},
{save,  6,   84}
};

pagerecord   pagetable[PAGENUM] =
{
{0,        1,         5,           0,           011},
{1,        1,         8,           0,           012},
{2,        1,         9,           0,           013},
{3,        1,         1,           0,           021},
{4,        0,         0,           0,           022},
{5,        0,         0,           0,           023},
{6,        0,         0,           0,           121}
};

int  pagein = LOADBLOCK-1,pageout = 0;

void run();
int  searchpagetable(unsigned int );

void main()
{
system("cls");

printf("操作系统中绝对地址计算和缺页中断模拟.\n");

printf("环境参数:内存块大小:%d,分页数:%d,指令数目:%d\n",
  BLOCKLENGTH,
  PAGENUM,
  INTRNUM
  );
printf("按任意键开始运行指令...\n\n");
getch();
run();
}

void run()
{
int instrcnt = 0;
int RecordId = 0;

unsigned int  absoluteaddr = 0; /*绝对地址*/

while ( instrset[instrcnt].optr != null )
{
  printf("执行第%d条指令: [操作码: %s, 访问页: %d 页内地址: %-3d]:\n",
   instrcnt,
   ( (instrset[instrcnt].optr==save) ? "Save" : "Not save"),
   instrset[instrcnt].page,
   instrset[instrcnt].pageaddr
   );

  RecordId = searchpagetable( instrset[instrcnt].page );/*搜索访问页面的记录,返回记录索引*/

  if ( pagetable[RecordId].flag == TRUE )/*页面标志位为 1, 计算 绝对地址*/
  {
   absoluteaddr = pagetable[RecordId].block * BLOCKLENGTH + instrset[instrcnt].pageaddr;
   if ( instrset[instrcnt].optr == save )
   {
    pagetable[RecordId].modify = 1;
    printf("由于该指令为存指令, 故页面改动位置 1.\n");
   }
   printf("指令执行正常,所访问内存绝对地址:%-4d.\n",absoluteaddr);/*打印十六进制绝对地址*/
  }
  else
  {
   printf("执行该指令时发生缺页中断.缺页号: %-3d.\n",instrset[instrcnt].page);
   if (pagetable[pageout].modify == TRUE)
   {
    printf("调出页面:%d",pagetable[pageout].page );
   }
   pagein++;
   pagetable[pagein%LOADBLOCK].page = instrset[instrcnt].page;
   printf("调入页面:%d\n",instrset[instrcnt].page);
   pageout = (++pageout)%LOADBLOCK;
  }

  printf("\n");
  instrcnt++;
}
printf("\n所有指令执行完毕.\n");
}

int  searchpagetable(unsigned int page)
{
int count = 0;

for ( count = 0; count < PAGENUM; count++ )
{
  if ( page == pagetable[count].page )
  {
   return count;
  }
}
return -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -