精华内容
下载资源
问答
  • 页面替换算法

    2017-12-10 20:19:54
    理解页面替换算法的作用和不同的页面替换算法确定被替换页面的原则
  • 闪存感知页面替换算法
  • Here you will get program for lru page ... 在这里,您将获得C语言中的lru页面替换算法的程序。 Least Recently Used (LRU) page replacement algorithm works on the concept that the pages that are heavil...

    lru页面替换算法

    Here you will get program for lru page replacement algorithm in C.

    在这里,您将获得C语言中的lru页面替换算法的程序。

    Least Recently Used (LRU) page replacement algorithm works on the concept that the pages that are heavily used in previous instructions are likely to be used heavily in next instructions. And the page that are used very less are likely to be used less in future. Whenever a page fault occurs, the page that is least recently used is removed from the memory frames. Page fault occurs when a referenced page in not found in the memory frames.

    最近最少使用(LRU)页面替换算法的工作原理是,先前指令中大量使用的页面很可能在下一指令中大量使用。 而且,很少使用的页面将来可能会更少使用。 每当发生页面错误时,都会从内存帧中删除最近使用最少的页面。 当在存储框架中找不到引用的页面时,将发生页面错误。

    Also Read: LRU Page Replacement Algorithm in C

    另请参见:C语言中的LRU页面替换算法

    Below program shows how to implement this algorithm in C.

    下面的程序显示了如何在C中实现此算法。

    C语言中的LRU页面替换算法程序 (Program for LRU Page Replacement Algorithm in C)

    #include<stdio.h>
     
    int findLRU(int time[], int n){
    	int i, minimum = time[0], pos = 0;
     
    	for(i = 1; i < n; ++i){
    		if(time[i] < minimum){
    			minimum = time[i];
    			pos = i;
    		}
    	}
    	
    	return pos;
    }
     
    int main()
    {
        int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0;
    	printf("Enter number of frames: ");
    	scanf("%d", &no_of_frames);
    	
    	printf("Enter number of pages: ");
    	scanf("%d", &no_of_pages);
    	
    	printf("Enter reference string: ");
    	
        for(i = 0; i < no_of_pages; ++i){
        	scanf("%d", &pages[i]);
        }
        
    	for(i = 0; i < no_of_frames; ++i){
        	frames[i] = -1;
        }
        
        for(i = 0; i < no_of_pages; ++i){
        	flag1 = flag2 = 0;
        	
        	for(j = 0; j < no_of_frames; ++j){
        		if(frames[j] == pages[i]){
    	    		counter++;
    	    		time[j] = counter;
    	   			flag1 = flag2 = 1;
    	   			break;
       			}
        	}
        	
        	if(flag1 == 0){
    			for(j = 0; j < no_of_frames; ++j){
    	    		if(frames[j] == -1){
    	    			counter++;
    	    			faults++;
    	    			frames[j] = pages[i];
    	    			time[j] = counter;
    	    			flag2 = 1;
    	    			break;
    	    		}
        		}	
        	}
        	
        	if(flag2 == 0){
        		pos = findLRU(time, no_of_frames);
        		counter++;
        		faults++;
        		frames[pos] = pages[i];
        		time[pos] = counter;
        	}
        	
        	printf("\n");
        	
        	for(j = 0; j < no_of_frames; ++j){
        		printf("%d\t", frames[j]);
        	}
    	}
    	
    	printf("\n\nTotal Page Faults = %d", faults);
        
        return 0;
    }

    Output

    输出量

    Enter number of frames: 3 Enter number of pages: 6 Enter reference string: 5 7 5 6 7 3

    输入帧数:3 输入页数:6 输入参考字符串:5 7 5 6 7 3

    5 -1 -1 5 7 -1 5 7 -1 5 7 6 5 7 6 3 7 6

    5 -1 -1 5 7 -1 5 7 -1 5 7 6 5 7 6 3 7 6

    Total Page Faults = 4

    总页面错误数= 4

    Video Tutorial

    影片教学

    Comment below if you have doubts or found anything incorrect in above lru page replacement algorithm in C.

    如果您对以上C语言中的lru页面替换算法有疑问或发现任何错误,请在下面评论。

    翻译自: https://www.thecrazyprogrammer.com/2016/11/lru-page-replacement-algorithm-c.html

    lru页面替换算法

    展开全文
  • lru页面替换算法Problem statement: 问题陈述: Write a program to simulate Least Recently Used (LRU) page replacement algorithm for the following page reference string: 9, 10, 11, 7, 12, 8, 7, 6, 12, ...

    lru页面替换算法

    Problem statement:

    问题陈述:

    Write a program to simulate Least Recently Used (LRU) page replacement algorithm for the following page reference string: 9, 10, 11, 7, 12, 8, 7, 6, 12, 5, 4, 3, 10, 11, 12, 4, 5, 6, 9, 4, 5.

    编写程序以模拟以下页面引用字符串的最近最少使用(LRU)页面替换算法 :9,10,11,7,12,12,8,7,6,12,5,5,4,3,10,11,12 ,4、5、6、9、4、5。

    Consider (i) 4 frames and (ii) 5 frames. Compare the results...

    考虑(i)4帧和(ii)5帧。 比较结果...

    Solution

    LRU页面替换算法 (LRU Page replacement algorithm)

    1. Set page_fault=0 & frame_size to the value given as input.

      将page_fault = 0和frame_size设置为输入值。

    2. Start traversing the pages.

      开始遍历页面。

      If

      如果

      1. Page_set holds less pages than frame_size. Insert pages one by one until the size of page_set reaches frame_size or all page references are processed. Simultaneously maintain the recent occurred counter (which has come most recently or least recently) of each page in a map called page_counter.
      2. Page_set容纳的页面少于frame_size 。 一页一页地插入页面,直到page_set的大小达到frame_size或处理所有页面引用为止。 同时在称为page_counter的映射中维护每个页面的最近发生的计数器 (最近或最近出现的计数器 )。
      3. Increment page_fault.
      4. 增加page_fault 。

      Else

      其他

      If current page is present in page_set
          Continue;
      Else
      
      
      1. Find the page in the set that was least recently used. We find it using the map we created. We basically need to replace the page with minimum counter.
      2. 在集合中找到使用最少的页面。 我们使用创建的地图找到它。 我们基本上需要用最小计数器替换页面。
      3. Replace the page having the least counter value with current page.
      4. 用当前页面替换计数器值最小的页面。
      5. Increment page_fault.
      6. 增加page_fault 。
      7. Update counter of current page.
      8. 更新当前页面的计数器 。
    3. Return page_fault.

      返回page_fault 。

    LRU页面替换算法和Belady异常的C ++实现 (C++ implementation of LRU page replacement algorithm and Belady's anomaly)

    #include <bits/stdc++.h>
    using namespace std;
    
    int page_faults(int page[],int n, int frame_size){
    	//STL set to hold current pages,max size=frame_size
    	unordered_set<int> page_set ; 
    	//STL map to store counter of each referenced pages 
    	unordered_map<int,int> page_counter;
    	int page_fault=0;
    	//for each memory reference
    	for(int i=0;i<n;i++){ 
    		//frame_size =max set size 
    		cout<<"processing request page no "<<page[i]<<endl;
    		//when all frames are not allocated
    		if(page_set.size()<frame_size){ 
    			//if page is not present in memory
    			if(page_set.find(page[i])==page_set.end()){ 
    				page_set.insert(page[i]); //insert pages
    				page_fault++; //increase page-fault
    			}
    			//update counter value of the referenced page
    			page_counter[page[i]]=i; 
    		}
    		//when all frames are allocated
    		else{ 
    			//if referenced page is not present
    			if(page_set.find(page[i])==page_set.end()){ 
    				int lru=INT_MAX,val;
    				for(auto it=page_set.begin();it!=page_set.end();it++){
    					//find the least recently used page
    					if(page_counter[*it]<lru){  
    						lru=page_counter[*it];
    						val=*it;
    					}
    				}
    				//erase the least recently used page
    				page_set.erase(val); 
    				//insert the current page
    				page_set.insert(page[i]); 
    				//increase page fault
    				page_fault++; 
    			}
    			//update counter value of current page
    			page_counter[page[i]]=i; 
    		}
    		//display existing pages in the frames 
    		//after each page request processed
    		cout<<"pages in the frames are: ";    
    		for(auto it=page_set.begin();it!=page_set.end();it++)
    			cout<<*it<<" ";
    		cout<<endl;
    	}
    }
    
    int main(){
    	//page reference requests
    	int page[]={9, 10, 11, 7, 12, 8, 7, 6, 12, 5, 4, 3, 10, 11, 12, 4, 5, 6, 9, 4, 5}; 
    	int n=sizeof(page)/sizeof(int);
    	//total no of frames in memory
    	int frame_size;   
    	//input frame_size
    	cout<<"enter no of frames"<<endl; 
    	cin>>frame_size;
    	//compute memory value
    	cout<<"no of page faults for "<<frame_size<<" frames are: "<<page_faults(page,n,frame_size)<<endl; 
    
    	return 0;
    }
    
    
    

    Output (first run)

    输出(首次运行)

    enter no of frames 
    4
    processing page no 9 
    pages in the frames are: 9 
    processing page no 10
    pages in the frames are: 10 9
    processing page no 11
    pages in the frames are: 11 9 10 
    processing page no 7 
    pages in the frames are: 7 11 9 10 
    processing page no 12
    pages in the frames are: 12 7 11 10
    processing page no 8 
    pages in the frames are: 8 12 7 11 
    processing page no 7 
    pages in the frames are: 8 12 7 11 
    processing page no 6 
    pages in the frames are: 6 8 12 7
    processing page no 12
    pages in the frames are: 6 8 12 7
    processing page no 5 
    pages in the frames are: 6 5 12 7
    processing page no 4 
    pages in the frames are: 4 6 5 12
    processing page no 3 
    pages in the frames are: 3 4 5 12
    processing page no 10
    pages in the frames are: 10 3 4 5
    processing page no 11
    pages in the frames are: 10 3 11 4 
    processing page no 12
    pages in the frames are: 12 10 3 11
    processing page no 4 
    pages in the frames are: 12 10 4 11
    processing page no 5 
    pages in the frames are: 5 12 4 11 
    processing page no 6 
    pages in the frames are: 6 5 12 4
    processing page no 9 
    pages in the frames are: 9 6 5 4 
    processing page no 4 
    pages in the frames are: 9 6 5 4 
    processing page no 5 
    pages in the frames are: 9 6 5 4 
    ...........no of page faults for 4 frames are: 17........ 
    
    
    

    Output (second run)

    输出(第二次运行)

    enter no of frames 
    5
    processing page no 9 
    pages in the frames are: 9 
    processing page no 10
    pages in the frames are: 10 9
    processing page no 11
    pages in the frames are: 11 9 10 
    processing page no 7 
    pages in the frames are: 7 11 9 10 
    processing page no 12
    pages in the frames are: 12 7 11 9 10
    processing page no 8 
    pages in the frames are: 8 12 7 11 10
    processing page no 7 
    pages in the frames are: 8 12 7 11 10
    processing page no 6 
    pages in the frames are: 6 8 12 7 11 
    processing page no 12
    pages in the frames are: 6 8 12 7 11 
    processing page no 5 
    pages in the frames are: 6 8 5 12 7
    processing page no 4 
    pages in the frames are: 4 6 5 12 7
    processing page no 3 
    pages in the frames are: 3 4 6 5 12
    processing page no 10
    pages in the frames are: 10 3 4 5 12 
    processing page no 11
    pages in the frames are: 10 3 11 4 5 
    processing page no 12
    pages in the frames are: 12 10 3 11 4
    processing page no 4 
    pages in the frames are: 12 10 3 11 4
    processing page no 5 
    pages in the frames are: 5 12 10 11 4
    processing page no 6 
    pages in the frames are: 6 5 12 11 4 
    processing page no 9 
    pages in the frames are: 9 6 5 12 4
    processing page no 4 
    pages in the frames are: 9 6 5 12 4
    processing page no 5 
    pages in the frames are: 9 6 5 12 4
    ...........no of page faults for 5 frames are: 16........
    
    

    Comparison of result

    结果比较

    The outputs show that the no of page fault for 4 frames are 17 while the no of page fault for 5 frames are 16. That means no of page faults has decreased with the increase in frame number.

    输出显示4帧的页面错误数为17,而5帧的页面错误数为16。这意味着,随着帧数的增加,页面错误数也没有减少。

    Comment

    评论

    This phenomenon is an example of Belady's anomaly. Belady's anomaly states that increasing number of frames will never increase number of page faults if LRU page replacement algorithm is used. Using LRU page replacement algo, no. of page fault may decrease or remain same if no of frame is increased. This is so because LRU is a stack algorithm.

    这种现象是贝拉迪异常现象的一个例子贝拉迪的异常状态表明,如果使用LRU页面替换算法,则增加帧数将永远不会增加页面错误数。 使用LRU页面替换算法 ,否。 如果不增加帧数,页面错误的数量可能会减少或保持不变。 之所以如此,是因为LRU是一个堆栈算法

    翻译自: https://www.includehelp.com/algorithms/analysis-of-lru-page-replacement-algorithm-and-beladys-anomaly.aspx

    lru页面替换算法

    展开全文
  • NAND闪存的高效页面替换算法
  • 页面替换算法opt+fifo+lru+clock页面替换算法opt+fifo+lru+clock
  • 南邮页面替换算法

    2019-12-08 21:50:03
    一个页面存储系统使用``FIFO,OPT和LRU页面替换算法,如果一个页面的走向为:2,3,2,1,5,2,4,5,3,2,5,2。当分配给该作业的物理页框块数为3时,试计算访问过程中发生的缺页中断次数和终端率。 代码 #...

    题目;
    一个页面存储系统使用``FIFO,OPT和LRU页面替换算法,如果一个页面的走向为:2,3,2,1,5,2,4,5,3,2,5,2。当分配给该作业的物理页框块数为3时,试计算访问过程中发生的缺页中断次数和终端率。
    代码

    #include<stdio.h>
    int ncc[3]={0,0,0}; 
    int ncc0[3]={0,0,0};                     //内存空间
    int ncc1[3]={0,0,0}; 					//opt算法的内存页面在将来使用的次数
    int ncc2[3]={0,0,0}; 
    
    int nczx0[12]={2,3,2,1,5,2,4,5,3,2,5,2};  //页面走向 
    int nczx1[12]={2,3,2,1,5,2,4,5,3,2,5,2};   
    int nczx2[12]={2,3,2,1,5,2,4,5,3,2,5,2};   
                        
    int bs0[3][12];
    int bs1[3][12];
    int bs2[3][12];  //表示出计算过程的数组
    int zdcs0=0;
    int zdcs1=0;
    int zdcs2=0; //每个算法的中断次数
    int zdl0=0,zdl1=0,zdl2=0;             //中断率 
    void optsf(int x)    //最佳置换方法算法,计算ncc数组中谁该被替换出来
    {
    	int i,k; 
    	int c=0;
    	int ncc1[3]={0,0,0};                   //opt算法的内存页面在将来使用的次数
    	for( i=x+1; i<12 ; i++)    //将当前内存中的页面与nczx中的i页面以后的页面作比较,计算第i次ncc中页面在未来出现的次数
    	{
    		if(nczx0[i]==ncc[0] && ncc1[0]<2) 
    		{
    			ncc1[0]++;
    			c++;	
    		}
    		if(nczx0[i]==ncc[1] && ncc1[1]<2) 
    		{	
    			ncc1[1]++;
    			c++;
    		}
    		if(nczx0[i]==ncc[2] && ncc1[2]<2) 
    		{
    			ncc1[2]++;
    			c++;	
    		}
    		if(c==2) break;
    	}
    	for(k=0;k<3;k++)    //将未来出现最少次数的元素置零
    		if(ncc1[k]==0) 
    		{
    			ncc[k]=0;
    			break;
    		}
    }
    void opt()   //最佳置换
    {
    	int i;
    	for(i=0; i<12; i++)
    	{
    		if( ncc[0]!=nczx0[i] && ncc[1]!=nczx0[i] && ncc[2]!=nczx0[i] )  //判断该改页面是否在内存中
    		{
    			zdcs0++;      //当前页面不在内存,中断次数加1
    			if( ncc[0]==0 ) ncc[0]=nczx0[i];         //当内存中有空闲区域
    			else if( ncc[1]==0 ) ncc[1]=nczx0[i];
    			else if( ncc[2]==0 )ncc[2]=nczx0[i];
    			else                                          //当内存中没有空闲区域就需要将未来最久不使用页面替换出来
    			{
    				optsf(i);                      //调用optsf函数计算下个未来最久不使用	
    				if( ncc[0]==0 ) 
    					ncc[0]=nczx0[i];         //将未来最久不使用页面从内存中找出后用当前页面替换
    			    if( ncc[1]==0 ) 	
    					ncc[1]=nczx0[i];
    				if( ncc[2]==0 ) 
    					ncc[2]=nczx0[i];
    			}
    		}
    		bs0[0][i]=ncc[0];
    		bs0[1][i]=ncc[1];
    		bs0[2][i]=ncc[2];
    	}
    	zdl0=zdcs0*100/12;     //计算页面中断率
    	int o,u;
    	for(o=0;o<3;o++)
    	{
    		for(u=0;u<12;u++)
    			printf("%d ",bs0[o][u]);        //将页面置换过程表示出来
    		printf("\n");
    	}
    }
    void fifo()    //先进先出置换算法
    {
    	int a=0;   //最早页面指针 
    	int zzjc=nczx1[0];
    	int i,o,u,j;
    	for(i=0;i<12;i++)
    	{
    		if( ncc1[0]!=nczx1[i] && ncc1[1]!=nczx1[i] && ncc1[2]!=nczx1[i] )   //判断将要访问的页面是否内存中
    		{
    			zdcs1++;     //该页面不在内存中发生一次页面中断
    			if(ncc1[0]==0) ncc1[0]=nczx1[i];   //如果内存中存在空内存,则直接插入
    			else if(ncc1[1]==0) ncc1[1]=nczx1[i];
    			else if(ncc1[2]==0) ncc1[2]=nczx1[i];
    			else               //如果内存中无空内存,则用要存入的页面替换最早页面
    			{
    			
    				if( ncc1[0]==zzjc ) ncc1[0]=nczx1[i];
    				if( ncc1[1]==zzjc ) ncc1[1]=nczx1[i];
    				if( ncc1[2]==zzjc ) ncc1[2]=nczx1[i];       //替换完成 
    				a++;
    				if(nczx1[a]==nczx1[i])            //
    			    {
    					for(j=a; j<i; j++)
    					{
    						if(nczx1[j]==nczx1[i])
    							a++;
    					}
    				}
    				if( nczx1[a]==ncc1[0] || nczx1[a]==ncc1[1] || nczx1[a]==ncc1[2]) 
    					zzjc=nczx1[a];
    				else
    				{
    					while(nczx1[a]!=ncc1[0] ||nczx1[a]!=ncc1[1] ||nczx1[a]!=ncc1[2])
    					{
    						zzjc=nczx1[a++];
    					}
    				}		 
    			}
    		}
    		bs1[0][i]=ncc1[0];     //将置换过程记录下来
    		bs1[1][i]=ncc1[1];
    		bs1[2][i]=ncc1[2];
    	}
    	zdl1=zdcs1*100/12;
    	for(o=0;o<3;o++)
    	{
    		for(u=0;u<12;u++)
    		printf("%d ",bs1[o][u]);        //将页面置换过程表示出来
    		printf("\n");
    	}	
    }
    void lru()    //过去最久未使用算法,过去最久未被使用的页面就是距离被此页面调度最远的nczx数组元素
    {
    	int i,o,u;
    	int  a=0;    //最久未使用页面指针
    	for(i=0; i<12; i++)
    	{
    		int zjwsyjc=nczx2[a];     //最久未使用的页面是nczx[a],默认nczx[0]
    		if( ncc2[0]!=nczx2[i] && ncc2[1]!=nczx2[i] && ncc2[2]!=nczx2[i] )  //将下一个元素放在在optsf中被置零的位置
    		{
    
    			zdcs2++;   //该页面不在内存中发生一次页面中断
    			if(ncc2[0]==0) ncc2[0]=nczx2[i];         //内存中有空闲区
    			else if(ncc2[1]==0) ncc2[1]=nczx2[i];
    			else if (ncc2[2]==0) ncc2[2]=nczx2[i];
    			else                   //内存中不存在空闲区,用现在的页面替换过去最久未使用的页面
    			{
    				
    				if(ncc2[0]==zjwsyjc) ncc2[0]=nczx2[i];
    				if(ncc2[1]==zjwsyjc) ncc2[1]=nczx2[i];
    				if(ncc2[2]==zjwsyjc) ncc2[2]=nczx2[i];
    				a++;                //已用nczx[i]页面替换最久未使用页面后不再是最久未使用页面,需计算下个最久未使用页面
    			}
    		}	
    		else    //nczx[i]页面已经在内存中,已经被使用一次,不再是最久未使用的页面,计算新的最久未使用页面
    		{
    			a++;      
    		}
    		bs2[0][i]=ncc2[0];
    		bs2[1][i]=ncc2[1];
    		bs2[2][i]=ncc2[2];
    		zdl2=100*zdcs2/12;
    	}
    	for(o=0;o<3;o++)
    	{
    		for(u=0;u<12;u++)
    		printf("%d ",bs2[o][u]);        //将页面置换过程表示出来
    		printf("\n");
    	}	
    }
    void print()
    {
    	int i;
    	printf("页面走向为:");
    	for(i=0;i<12;i++)
    	{
    		printf("%d ",nczx0[i]);
    	}
    	printf("\n");
    }
    prin()
    {
    	printf("中断次数为 ");	
    } 
    void main()
    {
    	print();
    	opt();
    	prin();
    	printf("%d",zdcs0);
    	printf("次  "); 
    	printf("最佳置换算法的中断率:");
    	printf("%d%c",zdl0,'%');
    	printf("\n");
    
    	print();
    	fifo();
    	prin();
    	printf("%d",zdcs1);
    	printf("次  ");
    	printf("先进先出置换算法的中断率:");
    	printf("%d%c",zdl1,'%');
    	printf("\n");
    
    	print();
    	lru();
    	prin();
    	printf("%d",zdcs2);
    	printf("次  ");
    	printf("最久未使用置换算法的中断率:");
    	printf("%d%c",zdl2,'%');
    }
    

    运行结果如下图:
    在这里插入图片描述

    展开全文
  • 操作系统 页面替换算法(OPT最佳置换算法与LRU最近最久未使用算法)
  • PageReplacementPolicy 在内存管理中实现页面替换算法
  • 页面替换算法cHere you will get program foroptimal page replacement algorithm in C. 在这里,您将获得C中最佳页面替换算法的程序。 Optimal page replacement algorithm says that if page fault occurs then ...

    Here you will get program for optimal page replacement algorithm in C.

    在这里,您将获得C中最佳页面替换算法的程序。

    Optimal page replacement algorithm says that if page fault occurs then that page should be removed that will not be used for maximum time in future.

    最佳页面替换算法表示,如果发生页面错误,则应删除该页面,该页面将不会在未来的最大时间内使用。

    It is also known as clairvoyant replacement algorithm or Bélády’s optimal page replacement policy.

    它也被称为透视替换算法或Bélády的最佳页面替换策略。

    Also Read: LRU Page Replacement Algorithm in C

    另请参见:C语言中的LRU页面替换算法

    Video Tutorial

    影片教学

    Below program shows how to implement this algorithm in C.

    下面的程序显示了如何在C中实现此算法。

    C中最佳页面替换算法的程序 (Program for Optimal Page Replacement Algorithm in C)

    #include<stdio.h>
      
     int main()
     {
         int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
         printf("Enter number of frames: ");
         scanf("%d", &no_of_frames);
         
         printf("Enter number of pages: ");
         scanf("%d", &no_of_pages);
         
         printf("Enter page reference string: ");
         
         for(i = 0; i < no_of_pages; ++i){
             scanf("%d", &pages[i]);
         }
         
         for(i = 0; i < no_of_frames; ++i){
             frames[i] = -1;
         }
         
         for(i = 0; i < no_of_pages; ++i){
             flag1 = flag2 = 0;
             
             for(j = 0; j < no_of_frames; ++j){
                 if(frames[j] == pages[i]){
                        flag1 = flag2 = 1;
                        break;
                    }
             }
             
             if(flag1 == 0){
                 for(j = 0; j < no_of_frames; ++j){
                     if(frames[j] == -1){
                         faults++;
                         frames[j] = pages[i];
                         flag2 = 1;
                         break;
                     }
                 }    
             }
             
             if(flag2 == 0){
             	flag3 =0;
             	
                 for(j = 0; j < no_of_frames; ++j){
                 	temp[j] = -1;
                 	
                 	for(k = i + 1; k < no_of_pages; ++k){
                 		if(frames[j] == pages[k]){
                 			temp[j] = k;
                 			break;
                 		}
                 	}
                 }
                 
                 for(j = 0; j < no_of_frames; ++j){
                 	if(temp[j] == -1){
                 		pos = j;
                 		flag3 = 1;
                 		break;
                 	}
                 }
                 
                 if(flag3 ==0){
                 	max = temp[0];
                 	pos = 0;
                 	
                 	for(j = 1; j < no_of_frames; ++j){
                 		if(temp[j] > max){
                 			max = temp[j];
                 			pos = j;
                 		}
                 	}            	
                 }
     			
     			frames[pos] = pages[i];
     			faults++;
             }
             
             printf("\n");
             
             for(j = 0; j < no_of_frames; ++j){
                 printf("%d\t", frames[j]);
             }
         }
         
         printf("\n\nTotal Page Faults = %d", faults);
         
         return 0;
     }

    Output

    输出量

    Enter number of frames: 3 Enter number of pages: 10 Enter page reference string: 2 3 4 2 1 3 7 5 4 3

    输入帧数:3 输入页数:10 输入页面参考字符串:2 3 4 2 1 3 7 5 4 3

    2 -1 -1 2 3 -1 2 3 4 2 3 4 1 3 4 1 3 4 7 3 4 5 3 4 5 3 4 5 3 4

    2 -1 -1 2 3 -1 2 3 4 2 3 4 1 3 4 1 3 4 7 3 4 5 3 4 5 3 4 5 3 4

    Comment below if you have doubts or found anything incorrect in above optimal page replacement algorithm in C.

    如果您对以上C语言中的最佳页面替换算法有疑问或发现任何错误,请在下面评论。

    翻译自: https://www.thecrazyprogrammer.com/2016/11/optimal-page-replacement-algorithm-c.html

    展开全文
  • CLRU:基于NAND闪存的消费电子产品的新页面替换算法
  • 文章目录模拟实现请求分页虚存页面替换算法1. 需求分析2. 数据结构设计3. 程序设计3.1 页面替换算法基本思路3.2 请求页面队列3.3 主存块队列3.4 进程3.5 主存3.6 主存队列的维护3.7 OPT3.8 FIFO3.9 LRU4. 页面替换...
  • 操作系统中页面替换算法的Java代码 复杂度分析 时间复杂度: 空间复杂度: 什么是分页? 在计算机操作系统中,分页是一种内存管理方案,计算机可以通过它来存储和从辅助存储器中检索数据,以供在主存储器中...
  • 页面替换算法LFU算法是记录页地址流的使用频数还是Cache块中使用频数?
  • 页面替换算法,包含fifo lru clock,采用c++编写,操作系统的同学可以看看
  • 页面替换算法 自定义第三次机会页面替换算法结合第二次机会和FIFO页面替换算法 Output page:(0041b)1051 frame:0 ref:0 mod:0 valid:1 page:(10011)65553 frame:1 ref:1 mod:1 valid:1 page:(00407)1031 frame...
  • 在操作系统课上的一点小感想,基于C++的请求分页虚拟页面替换算法,分享出来和大家一起学习。 实现OPT、FIFO、LRU、Clock等页面替换算法。接收用户输入参数,包括程序长度(页面数)、页框个数及页面大小,输出...
  • 页面替换算法 1.局部页面置换算法 1.1最优算法OPT 1.2先进先出算法FIFO 1.3最近最久未使用算法LRU 1.4时钟置换算法CLOCK 1.5改进时钟算法 1.6最不常用算法LFU 2.Belady现象 3.全局页面置换算法 3.1工作集...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 617
精华内容 246
关键字:

页面替换算法