Operating system Experiment page replacement algorithm (OPT, FIFO, LRU) C + + simple implementation __web

Source: Internet
Author: User

The topics are as follows: a page when the management system using FIFO, ORT, LRU page replacement algorithm, if a job page to: 2,3,2,1,5,2,4,5,3,2,5,2,. When the number of physical pages assigned to the job is 3 o'clock, the number of page breaks and the fault rate of the missing pages are calculated when the access process occurs.

Requires programming to achieve the solution. In understanding the principle of the three page replacement algorithm is not difficult to implement, this gives the implementation code, in order to avoid the code too messy, this is divided into three programs:

Fifo:

by Zwy #include <deque> #include <cstdio> #include <algorithm> using namespace std;
	int main () {deque<int> dq;
	Deque<int >::iterator POS;
	int numyk,numqueye=0;
	printf ("Please enter the number of physical page blocks:");
	scanf ("%d", &numyk);
	int n;
	printf ("\ n Please enter the number of page moves:");
	scanf ("%d", &n);
		for (int i=0; i<n; i++) {int in;
		scanf ("%d", &in);
			if (Dq.size () <numyk)//There is a free page box {int flag=0;
					for (pos = Dq.begin (); Pos!= dq.end (); pos++)//Traverse queue if ((*pos) ==in) {flag=1;
				Break
				} if (!flag)//does not exist this element {numqueye++;
			Dq.push_back (in);//There is no extra page box {int flag=0;
					for (pos = Dq.begin (); Pos!= dq.end (); pos++) if ((*pos) ==in) {flag=1;
				Break }//exists the element if (!flag)//does not exist this element replaces the first entry {numqueye++;//page number +1 dq.pop_front ();//The first incoming queue dq.push_back (in
	//Into Queue}} printf ("FIFO page number is:%d\n", Numqueye);
printf ("FIFO page fault interrupt rate:%lf\n", (double) numqueye*1.0/n); }
Opt:

by Zhongweiyu//Select a page replacement that is never used or is not used for the longest time////Reset queue after each operation//2 3 2 1 5 2 4 5 3 2 5 2 #include <deque> #include <cstdi
O> #include <algorithm> using namespace std;
	struct opt {int value;
int time;
};
const int maxn=105;
int A[MAXN];
	int main () {deque<opt> dq;
	Deque<opt >::iterator POS;
	int numyk,numqueye=0;
	printf ("Please enter the number of physical page blocks:");
	scanf ("%d", &numyk);
	int n;
	printf ("\ n Please enter the number of page moves:");
	scanf ("%d", &n);
	for (int i=0; i<n; i++) scanf ("%d", &a[i]);
		for (int i=0; i<n; i++) {printf ("%d \ n", i);
		int in;
		In=a[i];
			if (Dq.size () <numyk)//There is an extra page box {int flag=0;
					for (pos = Dq.begin (); Pos!= dq.end (); pos++) if ((*pos). Value==in)//exists element and it is the same {flag=1;
				Break
				}//exists the element if (!flag)//does not exist this element {numqueye++;
				Opt temp;
				Temp.value=in;
				int f=0;
						for (int j=i+1; j<n; J + +) if (a[j]==in) {f=1;
						Temp.time=j-i;
					Break
				} if (!f) temp.time=n; Dq.push_back (temp);
			The extra page box {int flag=0;
					for (pos = Dq.begin (); Pos!= dq.end (); pos++) if ((*pos). value==in) {flag=1;
				Break
				}//There is an item {numqueye++;//page number +1 int m=dq.front () for which the element if (!flag)//does not exist this element replaces the time maximum. Time;
				printf ("m initial value is%d\n", m);  Deque<opt >::iterator Mp=dq.begin (); Note here to be sure to initialize otherwise it is possible that erase cannot find the object crash for (pos = Dq.begin (); Pos!= dq.end (); pos++) {printf ("%d%d\n", (*pos). val
					UE, (*pos). time);
						if ((*pos). Time>m) {printf ("iteration");

					mp=pos;//the position of the largest element m= (*pos). Time;
				} opt temp;
				Temp.value=in;
				int f=0; 

				Dq.erase (MP);
						for (int j=i+1; j<n; J + +) if (a[j]==in) {f=1;
						Temp.time=j-i;
					Break
				} if (!f) temp.time=n;
			Dq.push_back (temp);
			Reset for (pos = Dq.begin (), POS!= dq.end (), pos++) {printf ("the element in the queue is%d\n" (*pos). Value) after each time
			int f=0;
				for (int j=i+1; j<n; J + +) if (a[j]== (*pos). Value){f=1;
					(*pos). Time=j-i;
				Break
		} if (!f) (*pos). Time=n;
	printf ("Opt page number of pages:%d\n", Numqueye);
printf ("Opt page break rate is:%lf\n", (double) numqueye*1.0/n); }
Lru:

by Zwy//selection of the most recent items to replace the most recent period of time has not been visited page replacement and opt similar//2 3 2 1 5 2 4 5 3 2 5 2 test data #include <deque> #include
<cstdio> #include <algorithm> using namespace std;  struct opt {int value;//value int time;
TIME};
const int maxn=105;
int A[MAXN];
	int main () {deque<opt> dq;
	Deque<opt >::iterator POS;
	int numyk,numqueye=0;
	printf ("Please enter the number of physical page blocks:");
	scanf ("%d", &numyk);
	int n;
	printf ("Please enter the number of page moves:");
	scanf ("%d", &n);
	for (int i=0; i<n; i++) scanf ("%d", &a[i]);
		for (int i=0; i<n; i++) {int in;
		In=a[i];
			if (Dq.size () <numyk)//There is an extra page box {int flag=0;
					for (pos = Dq.begin (); Pos!= dq.end (); pos++) if ((*pos). Value==in)//exists element and it is the same {flag=1;
				Break
				}//exists the element if (!flag)//does not exist this element {numqueye++;
				Opt temp;
				Temp.value=in;
				temp.time=0;
			Dq.push_back (temp);
			The extra page box {int flag=0; for (pos = Dq.begin (); Pos!= dq.end (); pos++) if ((*pos). value==in) {flag=1;
				Break
				}//exists the element if (!flag)//does not exist this element {numqueye++;//page number +1 int M=dq.front (). Time;  Deque<opt >::iterator Mp=dq.begin (); Note here to be sure to initialize otherwise it is possible that erase cannot find the object crash for (pos = Dq.begin (), POS!= dq.end (), pos++) {printf ("%d%d\n", (*pos). Valu
					E, (*pos). time);
						if ((*pos). Time>m) {printf ("iteration");

					mp=pos;//the position of the largest element m= (*pos). Time;
				The longest element left in the queue at this time is%d\n, (*MP). value);
				Opt temp;
				Temp.value=in;
				int f=0;
				Dq.erase (MP); temp.time=0;
			Add in after Dq.push_back (temp);
			(pos = Dq.begin (), POS!= dq.end (), pos++) {printf (the element in the queue is%d\n, (*pos). Value)
			int f=0;
					for (int j=i; j>=0; j--) if (a[j]== (*pos). Value) {(*pos). Time=i-j;
				Break
		printf ("Distance from last time is%d\n", (*pos).)
	printf ("LRU Number of pages:%d\n", Numqueye);
printf ("LRU page break rate is:%lf\n", (double) numqueye*1.0/n); }
Make a note of it for the future review.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.