我正在运行以下(最小复现)代码:
它的输出结果(使用
其他配置(debian stretch,Linux 5.2.21):
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
void main() {
int fd = open("file.data", O_RDONLY);
void* ptr = mmap(0, (size_t)240 * 1024 * 1024 * 1024, PROT_READ, MAP_SHARED, fd, 0);
printf("Result = %p\n", ptr);
printf("Errno = %d\n", errno);
}
它的输出结果(使用
gcc test.c && ./a.out
进行编译和运行)如下:Result = 0xffffffffffffffff
Errno = 9
file.data
是一个 243 GiB 的文件:
$ stat file.data
File: file.data
Size: 260165023654 Blocks: 508135088 IO Block: 4096 regular file
Device: 801h/2049d Inode: 6815790 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ user) Gid: ( 1001/ user)
Access: 2023-05-08 09:22:07.314477587 -0400
Modify: 2023-06-16 07:53:12.275187040 -0400
Change: 2023-06-16 07:53:12.275187040 -0400
Birth: -
其他配置(debian stretch,Linux 5.2.21):
$ sysctl vm.overcommit_memory
vm.overcommit_memory = 1
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 768178
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 768178
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
$ free -m
total used free shared buff/cache available
Mem: 192105 671 189213 9 2220 190314
Swap: 0 0 0
我已经遵循的建议:
根据我的理解,我应该能够使用mmap
来映射这个文件。我将其映射为只读,这样内核就可以在需要时自由地将其全部交换回磁盘。应该有足够的连续内存,因为这是一个64位系统。
如何使mmap
调用正常工作?
程序的/proc/*/maps
输出:
2aaaaaa000-2aaaaab000 r-xp 00000000 08:01 6815754 /home/<username>/a.out
2aaacaa000-2aaacab000 r--p 00000000 08:01 6815754 /home/<username>/a.out
2aaacab000-2aaacac000 rw-p 00001000 08:01 6815754 /home/<username>/a.out
3ff7a3a000-3ff7bcf000 r-xp 00000000 08:01 5767499 /lib/x86_64-linux-gnu/libc-2.24.so
3ff7bcf000-3ff7dcf000 ---p 00195000 08:01 5767499 /lib/x86_64-linux-gnu/libc-2.24.so
3ff7dcf000-3ff7dd3000 r--p 00195000 08:01 5767499 /lib/x86_64-linux-gnu/libc-2.24.so
3ff7dd3000-3ff7dd5000 rw-p 00199000 08:01 5767499 /lib/x86_64-linux-gnu/libc-2.24.so
3ff7dd5000-3ff7dd9000 rw-p 00000000 00:00 0
3ff7dd9000-3ff7dfc000 r-xp 00000000 08:01 5767249 /lib/x86_64-linux-gnu/ld-2.24.so
3ff7fe8000-3ff7fea000 rw-p 00000000 00:00 0
3ff7ff8000-3ff7ffb000 r--p 00000000 00:00 0 [vvar]
3ff7ffb000-3ff7ffc000 r-xp 00000000 00:00 0 [vdso]
3ff7ffc000-3ff7ffd000 r--p 00023000 08:01 5767249 /lib/x86_64-linux-gnu/ld-2.24.so
3ff7ffd000-3ff7ffe000 rw-p 00024000 08:01 5767249 /lib/x86_64-linux-gnu/ld-2.24.so
3ff7ffe000-3ff7fff000 rw-p 00000000 00:00 0
3ffffde000-3ffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
MAP_NORESERVE
也可能使映射正常工作。 - Andrew HenleMAP_NORESERVE
可能还可以使映射正常工作。 - Andrew HenleMAP_NORESERVE
可能还能使映射正常工作。 - undefined