假设:
- 页面和物理块大小都为1KB;
- 假设系统中只有一个进程P1,其页表如下
表1 进程P1的页表
| 页号 | 物理块号 | 存在位P |
| 0 | 3 | 1 |
| 1 | 0 | |
| 2 | 1 | 1 |
| 3 | 0 | |
| 4 | 0 | 1 |
| 5 | 0 | |
| 6 | 0 |
P=1表示在内存;P=0表示不在内存;
A=1表示最近被访问;A=0表示最近未被访问;
M=1表示被修改;M=0表示未被修改。
- 简单起见,假设内存为4KB,即4个物理块(物理块号:0~3)
表2 内存情况
| 物理块号 | 页号 | 访问位A | 修改位M | 指针(指向下一物理块号) |
| 0 | 4 | 1 | 1 | 1 |
| 1 | 2 | 0 | 1 | 3 |
| 2 | 2 | |||
| 3 | 0 | 1 | 0 | 0 |
假设初始情况下,替换指针指向0号物理块,空闲块指针指向2号物理块,两个指针分别作为头指针构成“已用块循环队列”和“空闲块循环队列”。
进程P1在某次执行过程中依次访问如下逻辑地址(其中R表示读取,W表示修改):
455(R)
1029(R)
2093(W)
2612(W)
3106(R)
4149(W)
5122(R)
6211(R)
6897(R)
875(W)
- 假设系统采用简单Clock置换算法,则依次给出这些逻辑地址对应的物理地址,并且在此过程中输出请求调入和置换页面的情况。
- 假设系统采用改进型Clock置换算法,则依次给出这些逻辑地址对应的物理地址,并且在此过程中输出请求调入和置换页面的情况
程序主要构成部分及其算法说明
主函数里调用了Init函数,并根据需要调用了Clock1()函数与Clock2()函数,分别为简单型clock与改进型clock;在两个函数中,Init():初始化页表,并引导用户页面号引用串。两个Clock函数中,分别有In()函数、Check函数,print函数,change函数,其中,in函数用于判断页面是否存在内存中,check函数根据时钟置换算法的不同分别检查访问位、访问位与修改位,change函数用于将页面与块中页面进行交换,print函数用于打印输出。
调试过程和运行结果
简单clock
改进型
附:源码
#includeusing namespace std; //#define R 0 //#define W 1 const int MaxNumber = 100; const int BlockNum = 4;//物理块数 const int PageNum = 7;//页面数 const int N=10;//进程执行访问逻辑地址次数 int Address[MaxNumber];//储存访问的逻辑地址 char ClockType[MaxNumber];//储存操作类型 queue Free;//空闲块 queue Busy;//占用块 int Replaceptr;//替换指针 int Freeptr;//空闲指针 struct Page{ int BlockNum; bool Exist;//存在位 }Page[PageNum]; struct Block{ int PageID;//页号 int Access;//访问位 int Modify;//修改位 int next;//指针 }Block[BlockNum]; void Init() {//初始化页表 for (int i = 0, j = 0; i < PageNum, j < BlockNum; i++, j++) { Page[i].Exist = 0;//存在位为0 Page[i].BlockNum = -1;//初始值置为-1代表当前物理块为空 Block[j].PageID = -1; Block[j].Access = 0; Block[j].Modify = 0; Block[j].next = -1; } Page[0].BlockNum = 3; Page[0].Exist = 1; Page[2].BlockNum = 1; Page[2].Exist = 1; Page[4].BlockNum = 0; Page[4].Exist = 1; Busy.push(0); Block[0].PageID = 4; Block[0].Access = 1; Block[0].Modify = 1; Block[0].next = 1; Busy.push(1); Block[1].PageID = 2; Block[1].Access = 0; Block[1].Modify = 1; Block[1].next = 3; Busy.push(3); Block[3].PageID = 0; Block[3].Access = 1; Block[3].Modify = 0; Block[3].next = 0; Free.push(2); Replaceptr = 0; Freeptr = 2; cout << "输入要访问的逻辑地址与访问类型(R为读取,W为修改):"; for (int j = 0; j < N; j++) { cin>>Address[j]>>ClockType[j]; } } void print(int address,int type){//输出函数 int pagenumber = address/1024;//页号 int pageaddress = address%1024;//页内地址 cout<<"逻辑地址 "<>a; if(a==1){ Clock1(); }else if(a==2){ Clock2(); }else{ cout<<"输入错误!"<



