nefu 625 Page Replacement

8 篇文章 0 订阅


Page Replacement

Time Limit 1000ms

Memory Limit 65536K

description

Page replacement algorithms were a hot topic of research and debate in the 1960s and 1970s. That mostly ended with the development of sophisticated LRU approximations and working set algorithms. Since then, some basic assumptions made by the traditional page replacement algorithms were invalidated, resulting in a revival of research. In particular, the following trends in the behavior of underlying hardware and user-level software has affected the performance of page replacement algorithms: 
Size of primary storage has increased by multiple orders of magnitude. With several gigabytes of primary memory, algorithms that require a periodic check of each and every memory frame are becoming less and less practical. Memory hierarchies have grown taller. The cost of a CPU cache miss is far more expensive. This exacerbates the previous problem. 

Locality of reference of user software has weakened. This is mostly attributed to the spread of object-oriented programming techniques that favor large numbers of small functions, use of sophisticated data structures like trees and hash tables that tend to result in chaotic memory reference patterns, and the advent of garbage collection that drastically changed memory access behavior of applications. 

Requirements for page replacement algorithms have changed due to differences in operating system kernel architectures. In particular, most modern OS kernels have unified virtual memory and file system caches, requiring the page replacement algorithm to select a page from among the pages of both user program virtual address spaces and cached files. The latter pages have specific properties. For example, they can be locked, or can have write ordering requirements imposed by journaling. Moreover, as the goal of page replacement is to minimize total time waiting for memory, it has to take into account memory requirements imposed by other kernel sub-systems that allocate memory. As a result, page replacement in modern kernels (Linux, FreeBSD, and Solaris) tends to work at the level of a general purpose kernel memory allocator, rather than at the higher level of a virtual memory subsystem. 

There are many page replacement algorithms, one of them is LRU: 

The least recently used page (LRU) replacement algorithm, though similar in name to NRU(Not recently used), differs in the fact that LRU keeps track of page usage over a short period of time, while NRU just looks at the usage in the last clock interval. LRU works on the idea that pages that have been most heavily used in the past few instructions are most likely to be used heavily in the next few instructions too. While LRU can provide near-optimal performance in theory (almost as good as Adaptive Replacement Cache), it is rather expensive to implement in practice. There are a few implementation methods for this algorithm that try to reduce the cost yet keep as much of the performance as possible. 

One important advantage of LRU algorithm is that it is amenable to full statistical analysis. It has been proved, for example, that LRU can never result in more than N-times more page faults than OPT algorithm, where N is proportional to the number of pages in the managed pool. 

The most expensive method is the linked list method, which uses a linked list containing all the pages in memory. At the back of this list is the least recently used page, and at the front is the most recently used page. The cost of this implementation lies in the fact that items in the list will have to be moved about every memory reference, which is a very time-consuming process. 

Another method that requires hardware support is as follows: suppose the hardware has a 64-bit counter that is incremented at every instruction. Whenever a page is accessed, it gains a value equal to the counter at the time of page access. Whenever a page needs to be replaced, the operating system selects the page with the lowest counter and swaps it out. With present hardware, this is not feasible because the required hardware counters do not exist. 

The LRU algorithm with 3 pages at most in the management pool runs as follows:


7  0  1  2  0  3  0  4  2  3  0  3  2  1  2  0  1  7  0  1  reference string
----------------------------------------------------------------------------------------
7  7  7  2     2     4  4  4  0        1     1     1         
   0  0  0     0     0  0  3  3        3     0     0       page frames in pool
      1  1     3     3  2  2  2        2     2     7       

12 page faults occurred.
For a given reference string, you need to calculate the number of page faults.

							

input

The first line contains an integer, the number of test cases. Each test case contains two lines, the first line is the capacity of the management pool m(0 < m ≤ 10000), and the length of reference string n(0 < n ≤ 100000). The next line contains exactly n integers, which indicate the reference sequence of page frames (page number ranged from 0 to n). 
							

output

For each test case, the output should contains the number of page faults that occurred.

							

sample_input

3
3 5
1 2 3 4 5
3 5
1 2 1 2 3
3 20
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
							

sample_output

5
3
12
							
题意:看着题目就发蒙,其实仔细看前面那一大部分都没有用,这道题讲的是计算机组成原理课上讲的

           LRU替换原则,刚开始内存没满时顺序存储,如果满了看哪个最久每用,就把它替换掉,

           如果里面本来就有那就不算为冲突,并且把这个相同的看为是最近刚用的,

            求一共出现多少次冲突。                                                         

           理解之后,我的第一反应就是队列,然后再加一点点模拟。                                                                                                                                                                                                                                                                                                                                                                                        

代码:

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

int T;
int n,m;

queue<int>que;

int v[111111];

int idx;
int ans;
int get;

int main()
{
    cin>>T;
    while (T--)
    {
        memset(v,0,sizeof(v));
        while (!que.empty()) que.pop();
        ans=0;
        cin>>m>>n;
        idx=0;
        for (int lp=0; lp<n; lp++)
        {
            int x;
            cin>>x;
            if (v[x]>0) m++;
            else ans++;
            que.push(x);
            v[x]++;
            while (que.size()>m)
            {
                get=que.front();
                que.pop();
                v[get]--;
                if (v[get]>0)
                {
                    m--;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值