23,125
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <memory.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>
#define PAGE_SIZE 4096
#define PAGE_COUNT 1024*1024
int main()
{
std::string str;
void** table = new void*[PAGE_COUNT];
std::cout << "begin mmap..." << std::endl;
for (int i=0; i<PAGE_COUNT; i++) {
table[i] = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1 ,0);
assert(table[i] != MAP_FAILED);
memset(table[i], 1, PAGE_SIZE);
}
std::cout << "mmap finish. input any str and enter to begin munmap..." << std::endl;
std::cin >> str;
for(int i=0; i<PAGE_COUNT; i++) {
if (i%11==0 || i%197==0 || i%393==0)
continue;
if (munmap(table[i], PAGE_SIZE) != 0)
std::cout << errno << std::endl;
}
delete[] table;
std::cin >> str;
return 0;
}