?? neicun.cpp
字號:
// neicun.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#define M 3
#define Myprintf printf("|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|\n") /*表格控制*/
typedef struct page
{ int num; /*記錄頁面號*/
int time; /*記錄調入內存時間*/
}Page; /* 頁面邏輯結構,結構為方便算法實現設計*/
Page b[M]; /*內存單元數*/
int zhiling[100]; //指令流
int N; //所調度的頁面總數
int c[M][100]; /*暫保存內存當前的狀態:緩沖區*/
int I; //一個進程的最大頁面數
int queue[100]; /*記錄調入隊列*/
int K; /*調入隊列計數變量*/
void Yebiao()//輸出頁表結構
{
float byte,byte1;
int i;
printf("請輸入進程大小:");
scanf("%f",&byte);
getchar();
I=int(byte/1000);
byte1=float(I*1000);
if(byte>byte1)
I++;
printf("此進程共%d個頁面,初始狀態為:\n",I);
for(i=0;i<I;i++)
printf("%d\tFalse\n",i);
}
bool zhilingliu() //確定頁表調用流
{
float dizhi;
char c;
printf("請輸入指令地址流:(以回車分開,以#號結束)\n");
for(N=0;N<100;N++)
{ scanf("%f",&dizhi);
//printf("%f\n",dizhi);
// printf("%c",c);
c=getchar();
//printf("%c",c);
//printf("%f\n",dizhi);
zhiling[N]=int(dizhi/1000);
if(zhiling[N]>=I)
{
printf("頁失效!產生中斷!\n");
return false;
}
if(c=='#')
return true;
}
}
void Init(Page *b,int c[][100]) /*初始化內存單元、緩沖區*/
{
int i,j;
for(i=0;i<M;i++)
{
b[i].num=-1;
b[i].time=N-i-1;
}
for(i=0;i<M;i++)
for(j=0;j<100;j++)
c[i][j]=-1;
}
/*取得在內存中停留最久的頁面,默認狀態下為最早調入的頁面*/
int GetMax(Page *b)
{ int i;
int max=-1;
int tag=0;
for(i=0;i<M;i++)
{ if(b[i].time>max)
{ max=b[i].time;
tag=i;
}
}
return tag;
}
/*判斷頁面是否已在內存中*/
int Equation(int fold,Page *b)
{ int i;
for(i=0;i<M;i++)
{ if (fold==b[i].num)
return i; }
return -1; }
/*LRU核心部分*/
void Lru(int fold,Page *b)
{ int i;
int val;
val=Equation(fold,b);
if (val>=0)
{
b[val].time=0;
for(i=0;i<M;i++)
if (i!=val)
b[i].time++; }
else
{ queue[++K]=fold;/*記錄調入頁面*/
val=GetMax(b);
b[val].num=fold;
b[val].time=0;
for(i=0;i<M;i++)
if (i!=val)
b[i].time++;
}
}
/*主程序*/
int main(int argc, char* argv[])
{
int i,j;
start:
K=-1;
Yebiao();
if(zhilingliu()==false)
return 0;
Init(b, c);
for(i=0;i<N;i++)
{
Lru(zhiling[i],b);
// c[0][i]=zhiling[i];
/*記錄當前的內存單元中的頁面*/
for(j=0;j<M;j++)
c[j][i]=b[j].num;
}
/*結果輸出*/
printf("運行過程中內存狀態為:\n");
Myprintf;
for(j=0;j<N;j++)
printf("|%2d ",zhiling[j]);
printf("|\n");
Myprintf;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(c[i][j]==-1)
printf("|%2c ",32);
else
printf("|%2d ",c[i][j]);
}
printf("|\n");
}
Myprintf;
printf("\n調入隊列為:");
for(i=0;i<K+1;i++)
printf("%3d",queue[i]);
printf("\n缺頁次數為:%6d\n缺頁率:%16.6f",K+1,(float)(K+1)/N);
printf("\nAre you continuing!\ty?");
if(getche()=='y')
{
printf("\n");
goto start; }
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -