【问题标题】:C++ mincore return vector every byte is 1C ++ mincore返回向量每个字节为1
【发布时间】:2019-05-15 11:18:41
【问题描述】:

我使用mincore通过在内存或磁盘中打开mmap来判断内存。但返回一个集合向量。为什么?事实上,结果必须是一个完全清晰的向量,但我已经准备好了。

这是我的代码。为什么总是跳过第 28 行 (cout << "find" << endl;)?

/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;
}

我想通过mmap打开内存来获取地址,老手有办法吗?/ 我需要帮助。

【问题讨论】:

  • 代码必须作为文本包含在问题中。
  • 您将复制粘贴代码,然后选择它,然后按 ctrl+k 或工具栏中的 { } 按钮
  • 为什么mincore返回所有1位向量?
  • 请也添加必要的包含
  • 哦,谢谢!第一次使用。

标签: c++ mmap


【解决方案1】:

我得到一个旧文件(不要长时间打开),mincore 返回一个法线向量(有 0 和 1),但是一个新文件(刚刚打开或读取...),mincore 返回一个全部设置位向量。 这种现象是由于页面缓存造成的,它会将最近的页面保存到缓存中,如果程序重复打开一个文件,文件的页面就会进入内存。

【讨论】:

    猜你喜欢
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2013-12-24
    • 2012-10-08
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多