【发布时间】: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位向量?
-
请也添加必要的包含
-
哦,谢谢!第一次使用。