简体   繁体   English

C ++ Mincore返回向量每个字节为1

[英]C++ mincore return vector every byte is 1

I use mincore to judge memory by mmap open in memory or disk. 我使用mincore通过在内存或磁盘中打开mmap来判断内存。 but return a set vector. 但返回一个设定的向量。 Why? 为什么? In fact the result must be a all clear vector, but I get all set. 实际上,结果必须是一个完全清楚的向量,但是我已经全部设置好了。

This is my code. 这是我的代码。 Why is line 28 ( cout << "find" << endl; ) always skipped? 为什么总是跳过第28行( cout << "find" << endl; )?

/proc/pid/smap can see RSS is 0, but mincore return total file in memory. /proc/pid/smap可以看到RSS为0,但是mincore返回内存中的总文件。

#include <iostream>
#include <unistd.h>
#include <sys/mman.h>
#include <bitset>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

using namespace std;

int main()
{
    char* pData1 = NULL;
    int fd1 = open("test_large_file_1", O_RDWR);
    if (fd1 == -1)
    {
        cout << "file error ..." << endl;
        return -1;
    }
    off_t size1 = lseek(fd1, 0, SEEK_END);
    if (size1 == -1)
    {
        cout << "lseek error ..." << endl;
        return -1;
    }
    pData1 = (char *)mmap(NULL, size1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd1, 0 );
    if (pData1 == MAP_FAILED)
    {
        cout << "mmap error ..." << endl;
        return -1;
    }
    unsigned char *pVec = new unsigned char[size1 / 1024 / 4];
    if (-1 == mincore(pData1, size1, pVec))
    {
        cout << "mincore error ..." << endl;
        return -1;
    }
    for (int i = 0; i < size1 / 1024/ 4; ++i)
    {
        if (i % 1000 == 0)
            cout << (int)pVec[i] << endl;
        if ((pVec[i] & 1) == 0)
        {
            cout << "find" << endl;
            break;
        }
    }
    close(fd1);
    munmap((void *)pData1, size1);
    return 0;
}

I want to get whether an address by mmap opening in memory or not, veteran has some way?/ I need help. 我想通过mmap在内存中打开地址来获取地址,退伍军人有某种方法吗?/我需要帮助。

I get a old file(don't open long time),mincore return a normal vector(has 0 and 1), but a new file(just now open or read ...),mincore return a all set bit vector. 我得到一个旧文件(不会长时间打开),mincore返回一个正常的向量(具有0和1),但是一个新文件(刚刚打开或读取...),mincore返回一个全部设置的位向量。 This phenomenon is due to page cache, that will save recently page to cache, if a program repeatly open a file, the page of file will be get in memory . 这种现象是由于页面缓存所致,它将最近保存的页面保存到缓存中,如果程序重复打开文件,文件的页面将进入内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM