📄 main.cpp
字号:
#include "stdio.h"
#define BLOCKLENGTH 128
#define CAPABILITY 4
struct PAGETABLEITEM
{
int pageID;
bool loaded;
int blockID;
int location;
bool modified;
PAGETABLEITEM *next;
};
typedef PAGETABLEITEM *itemPtr;
struct INSTRUCTION
{
char operation;
int pageID;
int unitID;
INSTRUCTION *next;
};
typedef INSTRUCTION *instrPtr;
PAGETABLEITEM defaultItem[7] =
{
0, true, 5, 011, false, &defaultItem[1],
1, true, 8, 012, false, &defaultItem[2],
2, true, 9, 013, false, &defaultItem[3],
3, true, 1, 021, false, &defaultItem[4],
4, false,-1,022, false, &defaultItem[5],
5, false,-1,023, false, &defaultItem[6],
6, false,-1,121, false, NULL
};
INSTRUCTION defaultInstr[12] =
{
'+', 0, 70, &defaultInstr[1],
'+', 1, 50, &defaultInstr[2],
'*', 2, 15, &defaultInstr[3],
'S', 3, 21, &defaultInstr[4],
'L', 0, 56, &defaultInstr[5],
'-', 6, 40, &defaultInstr[6],
'M', 4, 53, &defaultInstr[7],
'+', 5, 23, &defaultInstr[8],
'S', 1, 37, &defaultInstr[9],
'L', 2, 78, &defaultInstr[10],
'+', 4, 1, &defaultInstr[11],
'S', 6, 84, NULL
};
int Mm[CAPABILITY];
int MmPtr = 0;
void getInstr(instrPtr instr);
void getPageID(instrPtr instr);
bool chkPageTbl(int pageID, itemPtr item);
void formAbAddr(int blockID, int unitID);
void interrupt(int pageID, itemPtr item);
void main()
{
int i;
int m;
itemPtr item;
instrPtr instr;
for(i=0; i<CAPABILITY; i++)
Mm[i] = i;
printf("Would You Like to Load Default Data ?\n-----0 to Load Default; Others to Input Your Data\n");
scanf("%d", &i);
if(i == 0)
{
item = defaultItem;
instr = defaultInstr;
}
else
{
PAGETABLEITEM newItem[10];
INSTRUCTION newInstr[20];
for(i=0; i<10;i++)
{
printf("PAGE ID::");
scanf("%d",&newItem[i].pageID);
printf("LOADED (1 for yes)::");
scanf("%d",&newItem[i].loaded);
printf("BLOCK ID(-1 if not loaded)::");
scanf("%d",&newItem[i].blockID);
printf("LOCATION ON DISK::");
scanf("%d",&newItem[i].location);
printf("Any MORE(1 for yes)::");
scanf("%d",&m);
if(m)
newItem[i].next = &newItem[i+1];
else
newItem[i].next = NULL;
}
for(i=0; i<20; i++)
{
printf("OPERATION ::");
scanf("%c",&newInstr[i].operation);
printf("PAGE ID::");
scanf("%d",&newInstr[i].pageID);
printf("BLOCK ID::");
scanf("%d",&newInstr[i].unitID);
printf("Any MORE(1 for yes)::");
scanf("%d",&m);
if(m)
newInstr[i].next = &newInstr[i+1];
else
newInstr[i].next = NULL;
}
item = newItem;
instr = newInstr;
}
while(instr)
{
printf("\n----------------------------\n");
getInstr(instr);
getPageID(instr);
if(chkPageTbl(instr->pageID, item))
{
formAbAddr(item[instr->pageID].blockID, instr->unitID);
if(instr->operation == 'S')
{
item[instr->pageID].modified = true;
printf("Page %d Modified\n", instr->pageID);
}
}
else
{
interrupt(instr->pageID, item);
formAbAddr(item[instr->pageID].blockID, instr->unitID);
}
instr = instr->next;
printf("----------------------------\n");
}
}
void getInstr(instrPtr instr)
{
printf("Operation::%c\n",instr->operation);
}
void getPageID(instrPtr instr)
{
printf("Page ID::%d\n",instr->pageID);
/*printf("Modified ::");
if(item[instr->pageID].modified)
printf("YES\n");
else
printf("NO\n");*/
}
bool chkPageTbl(int pageID, itemPtr item)
{
return item[pageID].loaded;
}
void formAbAddr(int blockID, int unitID)
{
int addr;
addr = blockID*BLOCKLENGTH + unitID;
printf("Abso Addr::%d\n", addr);
}
void interrupt(int pageID, itemPtr item)
{
int temp;
printf("Miss Page::* %d\n",pageID);
item[Mm[MmPtr]].loaded = false;
temp = item[Mm[MmPtr]].blockID;
item[Mm[MmPtr]].blockID = -1;
if(item[Mm[MmPtr]].modified)
printf("Page %d Unloaded\n", Mm[MmPtr]);
else
{
printf("Page %d Overlayed\n", Mm[MmPtr]);
item[Mm[MmPtr]].modified = false;
}
Mm[MmPtr] = pageID;
item[pageID].blockID = temp;
item[pageID].loaded = true;
printf("Page %d Now Loaded\n", pageID);
MmPtr = (MmPtr+1)%CAPABILITY;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -