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

📄 clock.cpp

📁 改进型CLOCK 页面置换算法实现在进程运行过程中
💻 CPP
字号:
#include"Clock.h"

void MemPageIinit(int number) ;
void ReplacePage( page * &preptr, page * &ptr ) ;
void ClockReplace() ;
void ProcessPageInit() ;
void ShowPage() ;
void RunMempage() ;
void CmdParse() ;
void SetPage(int pageid , int a ,int m ) ;

/***************************************************************
name :
function :
parameter:
****************************************************************/
int main()
{

	ProcessPageInit() ;
	MemPageIinit(4) ;
	RunMempage() ;

	CmdParse() ;

//	ShowPage() ;
//	ClockReplace() ;
//	ShowPage() ;

	return 0 ;
}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void SetPage(int pageid , int a ,int m ) 
{
	page * p ;
	p = headptr ;

    while (1)
    {
		if ( p->pageID == pageid )
		{
			p->A = a ;
			p->M = m ;
			cout<<"页面设置成功!"<<endl;
			break ;
		}
		
		p = p->next ;
		if ( p == NULL )
		{
			cout<<"内存中无此页面,设置失败!"<<endl ;
			break ;
		}
    }
}

/***************************************************************
name :RunMempage
function : 将进程页面载入内存页面
parameter: 无
****************************************************************/
void RunMempage()
{
	page * p ;
	p = headptr ; 

	for( int i= 0 ; 1 ; i ++ )
	{
		p->A = processpage[i].A ;
		p->M = processpage[i].M ;
		p->pageID = processpage[i].pageID ;
		p = p->next ;
		if( p == NULL)
		{
			break ;
		}
	}
}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void CmdParse()
{
	string strcmd ;
	int pageid ,a ,m ;

	cout<<"请输入命令:"<<endl ;
	cout<<"user\\> ";
	cin>>strcmd ;

	while (strcmd != "exit")
	{
		if ( strcmd == "help" ) 
		{
			cout<<"  命令        功能"<<endl ;
			cout<<"setpage   : 设置页面信息"<<endl ;
			cout<<"showpage  : 状态显示"<<endl ;
			cout<<"replace   : Clock页面置换"<<endl ;
			cout<<"exit      : 退出程序"<<endl ;
		}
		else if ( strcmd == "showpage" )
		{
			ShowPage() ;
		}
		else if ( strcmd == "replace" )
		{
			ClockReplace() ;
		}
		else if (strcmd == "setpage")
		{
			cout<<"please input PageID,flag A,falag M:"<<endl;
			cin>>pageid>>a>>m ;

			SetPage( pageid , a , m ) ;
		}
		else
		{
			cout<<"invalidation command,please input :help"<<endl ;
		}
		
		cout<<"user\\> ";
		cin>>strcmd ;
	}

}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void ShowPage() 
{
	page * ptr ;
	ptr = headptr ;

	cout<<"*********************************************************************"<<endl ;
	cout<<"PageID: ";
	for(int i=0 ;i<PPAGENUMBER;i++)
	{
		cout<<i<<" ";
	}
	cout<<endl ;

	cout<<"A :     ";
	for(int it=0 ;it<PPAGENUMBER;it++)
	{
		cout<<processpage[it].A<<" ";
	}
	cout<<endl ;

	cout<<"M :     ";
	for(int ic=0 ;ic<PPAGENUMBER;ic++)
	{
		cout<<processpage[ic].M<<" ";
	}
	cout<<endl ;

	cout<<"MemeryPage"<<endl;
	cout<<"MemPgeID :";
	while(ptr != NULL)
	{
		cout<<ptr->pageID<<" " ;
		ptr = ptr->next ;
	}
	ptr = headptr ;
	cout<<endl ;

	cout<<"A :       ";
	while(ptr != NULL)
	{
		cout<<ptr->A<<" " ;
		ptr = ptr->next ;
	}
	ptr = headptr ;
	cout<<endl ;

	cout<<"M :       ";
	while(ptr != NULL)
	{
		cout<<ptr->M<<" " ;
		ptr = ptr->next ;
	}
	ptr = headptr ;
	cout<<endl ;
	cout<<"*********************************************************************"<<endl ;
}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void ProcessPageInit()
{
	for( int i=0 ; i < PPAGENUMBER ; i ++ )
	{
		processpage[i].pageID = i ;
		processpage[i].A = 0 ;
		processpage[i].M = 0 ;
	}

}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void MemPageIinit(int number)
{
	headptr = new page ;
	tailptr = headptr ;

	for(int i = 1 ; i < number ; i ++ ) 
	{
		tailptr->next = new page ;
		tailptr = tailptr->next ;
		if( i == (number-1) )
		{
			tailptr->next = NULL ;
		}
	}
}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void ReplacePage( page * &preptr, page * &ptr )
{
	page * tempptr ;

	tempptr = ptr ;

	if( ptr == headptr )
	{
		headptr = ptr->next ;
		ptr = ptr->next ;
		preptr = ptr->next ;
	}
	else
	{
		preptr->next = ptr->next ;
		ptr = ptr->next ;
	}
	
	processpage[tempptr->pageID].A = tempptr->A ;
	processpage[tempptr->pageID].M = tempptr->M ;

	free(tempptr) ;
}

/***************************************************************
name :
function :
parameter:
****************************************************************/
void ClockReplace()
{
	page * preptr ;
	int falg_A = 0 , falg_M = 0 ;

	curptr = headptr ;
	preptr = headptr ;

	while(1)
	{
		if( (curptr->A==falg_A)&&(curptr->M==falg_M) )
		{
			ReplacePage(preptr,curptr) ;
			break ;
		}
		if( falg_M == 1 ) //(2)
		{
			curptr->A = 0 ;
		}

		preptr = curptr ;
		curptr = curptr->next ;

		if(curptr == NULL)
		{
			if( falg_M == 0 )
			{
				falg_M = 1 ; //(2)
			}
			else
			{
				falg_M = 0 ;
			}

			preptr = headptr ;
			curptr = headptr ;		
		}
	}

}

⌨️ 快捷键说明

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