#include <windows.h> |
#include <stdio.h> |
#include <iostream> |
#include <stdlib.h> |
using namespace std; |
DWORD WINAPI Thread1( LPVOID lpPara); //线程1 |
DWORD WINAPI Thread2( LPVOID lpPara); //线程2 |
HANDLE hmove=CreateSemaphore(NULL,1,1, "yw_zh" ); //同步信号量 |
int xl[20]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1}; |
int i=0,d=32768; |
int zy=0; //记录寄存器需要首位置1的物理块号 |
struct phyblk |
{ |
unsigned int regit; //存放16位寄存器数值 |
int psave; //记录存放的页面 |
bool state; |
phyblk(){regit=0;psave=0;state=0;} |
}; |
phyblk pb[3]; //3个物理块 |
int main() |
{ |
DWORD ThreadId1, ThreadId2; |
cout<< "请输入页面访问序列的文件名:3.txt" <<endl |
<< "最近最久未使用页面置换算法(LRU)页面置换序列:" ; |
HANDLE mthread = CreateThread(NULL,0,Thread1,NULL,0,&ThreadId1); |
if (mthread == NULL) |
return -1; |
HANDLE vthread = CreateThread(NULL,0,Thread2,NULL,0,&ThreadId2); |
if (vthread == NULL) |
return -1; |
system ( "pause" ); |
} |
DWORD WINAPI Thread1( LPVOID lpPara) |
{ |
int a=0,b=0,c=0,qy=0; |
while (i<20) |
{ |
Sleep(80); |
WaitForSingleObject(hmove,INFINITE); |
for (a=0;a<3;a++) |
if (pb[a].psave==xl[i]&&pb[a].state==1) //判断页面是否已经在内存中 |
{ |
zy=a; |
goto outcyc; //跳出for循环 |
} |
for (a=0;a<3;a++) |
{ |
if (pb[a].state==0) //寻找空物理块 |
{ |
pb[a].psave=xl[i]; |
zy=a; |
pb[a].state=1; |
goto outcyc; |
} |
if (pb[a].regit<d) //找出寄存器数值最小的物理块的块号 |
{ |
d=pb[a].regit; |
b=a; |
} |
} |
zy=b; |
cout<<pb[b].psave<< " " ; |
pb[b].state=1; |
pb[b].psave=xl[i]; |
qy++; //记录缺页数 |
d=32768; |
|
outcyc: |
i++; |
|
} |
cout<<endl<< "总缺页数:" <<qy<<endl; |
return 0; |
} |
DWORD WINAPI Thread2( LPVOID lpPara) |
{ |
int a=0,b=0,c=0; |
while (i<20) |
{ |
Sleep(100); |
for (a=0;a<3;a++) |
pb[a].regit>>=1; //向右移1位 |
pb[zy].regit+=32768; //首位置1 |
ReleaseSemaphore(hmove,1,NULL); |
} |
} |