Best elimination Algorithm (OPT) FIFO algorithm (FIFO) The most recent unused algorithm (LRU)

Source: Internet
Author: User
Tags rand
 
#include <bits/stdc++.h> #define Del (b) memset (A,b,sizeof (a)) using namespace Std;  int a[4000];
	Randomly generated page int c;//user page capacity void init ()//Generate page {Srand (time (0));
		for (int i=1;i<=320;i++) {int q=rand ()%317;
		A[I++]=Q/10;
		a[i++]= (q+1)/10;
		int Head=rand ()% (q+2);
		A[I++]=HEAD/10;
		a[i++]= (head+1)/10;
		int tail= (rand ()% (318-q)) +q+3;
		A[I]=TAIL/10;
	a[i]= (tail+1)/10;
} c= (rand ()%29) +4;  } struct Node {int dis,val,pos;
	Distance from next identical element, element, position node () {} node (int dd,int vv,int pp) {dis=dd;val=vv;pos=pp;
		} BOOL operator < (const node & a) const//Priority Queue {if (A.dis!=dis) return a.dis>dis;
	else return a.val>val;
}
};
	void OPT ()//Best elimination method (priority queue implementation) {int poss[400];
	int lack=0;  int head[400],net[400];
	The receiving table stores the closest distance Del (head,-1);
	
	Del (poss,0);
	Priority_queue<node> Q;
		for (int i=1;i<=320;i++) {net[i]=head[a[i]];
	Head[a[i]]=i;
	} int t=1,tt=1;
for (int i=1;i<=320;i++)//First fill with C page {if (!poss[a[i]]) {poss[a[i]]=t;			t++;
		} Q.push (Node (net[i],a[i],poss[a[i]));
		tt=i;
	if (t>c) break;
		} if (t<=c) {printf ("100% percent hit \ n"); 
	Return
			} for (int i=tt+1;i<=320;i++) {if (!poss[a[i]) {lack++;
			Node P=q.top ();
			Q.pop ();
			Poss[a[i]]=p.pos;
		poss[p.val]=0;
	} Q.push (Node (net[i],a[i],poss[a[i]));
	} printf ("Number of pages%d\n", lack); 
printf ("%lf%% hit rate \ n", (1.0-((1.0*lack)/(1.0*320)) *100.0);
	} void FIFO () {int aim[400];
	int vis[400];
	Del (vis,0);
	int lack=0;
	int t=0,tt=0;
	queue<int>q;
			for (int i=1;i<=320;i++) {if (!vis[a[i]]) {vis[a[i]]=1;
			t++;
		Q.push (A[i]);
		} tt=i;
	if (t>c) break;
		} if (t<=c) {printf ("100% percent hit \ n");
	Return
			} for (int i=tt+1;i<320;i++) {if (!vis[a[i]) {vis[a[i]]=1;
			Vis[q.front ()]=0;
			Q.pop ();
			lack++;
		Q.push (A[i]);
	}} printf ("Number of pages%d\n", lack);
printf ("%lf%% hit rate \ n", (1.0-((1.0*lack)/(1.0*320)) *100.0);
	} void LRU () {int time[400]={0};
	int vis[400]={0};
	int lack=0; int t=0,tt=0;
	queue<int>q;
			for (int i=1;i<=320;i++) {if (!vis[a[i]]) {vis[a[i]]=1;	
		t++;
		} q.push (A[i]);
		time[a[i]]++;
		tt=i;
	if (t>c) break;
		} if (t<=c) {printf ("100% percent hit \ n");
	Return
				} for (int i=tt+1;i<320;i++) {if (!vis[a[i]) {while (Time[q.front ()]) {Time[q.front ()]--;
				if (Time[q.front ()]==0) break;
			Q.pop ();
			} vis[q.front ()]=0;
			Q.pop ();
			Vis[a[i]]=1;
			time[a[i]]++;
			Q.push (A[i]);
		lack++;
		} q.push (A[i]);
	time[a[i]]++; 
	} printf ("Number of pages%d\n", lack);
printf ("%lf%% hit rate \ n", (1.0-((1.0*lack)/(1.0*320)) *100.0); } int main () {//printf ("!!
	\ n ");
	Init ();
	printf ("opt:\n");  OPT ();
	Priority Queue printf ("fifo:\n"); FIFO ();
	Queue printf ("lru:\n");  LRU (); 
 Queue


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.