我无法在具有足够内存的64位计算机中使用mmap分配超过2GB的连续内存。 以下代码愉快地运行。但是,当我将机器中连续mmap的页面数量增加到略高于2GB时,mmap会失败。
#include <sys/mman.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#define PAGE_SIZE (4*1024)
#define START_ADDRESS 0x8000000
#define START_ADDRESS2 0x800000000
#define N_ITERATIONS 524288
// one more iteration and mmap fails
//#define N_ITERATIONS 524289
void allocate(void* base_address)
{
int i;
for (i = 0; i < N_ITERATIONS; ++i) {
void* current_addr = base_address + PAGE_SIZE * i;
void* ret = mmap((void*)current_addr, PAGE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
MAP_NORESERVE | MAP_FIXED | MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (ret == MAP_FAILED) {
fprintf(stderr, "Error mmap. errno: %d\n", errno);
exit(-1);
}
printf("%d\n", i);
}
}
int main()
{
allocate((void*)START_ADDRESS);
allocate((void*)START_ADDRESS2);
return 0;
}
ulimit -a
-t: cpu time (seconds) unlimited
-f: file size (blocks) unlimited
-d: data seg size (kbytes) unlimited
-s: stack size (kbytes) 8192
-c: core file size (blocks) unlimited
-m: resident set size (kbytes) unlimited
-u: processes 515550
-n: file descriptors 65535
-l: locked-in-memory size (kbytes) unlimited
-v: address space (kbytes) unlimited
-x: file locks unlimited
-i: pending signals 515550
-q: bytes in POSIX msg queues 819200
-e: max nice 0
-r: max rt priority 0
-N 15: unlimited
什么阻止内核分配超过2GB的连续数据?
答案 0 :(得分:3)
base_address
PAGE_SIZE * i
0xffffffff86000000
。{/ p>
因此,您试图在i
附近的地址上分配。
为fscanf
代替%s
,它会起作用。