64位机器中的最大文件映射大小是多少 [英] What's the max file mapping size in 64bits machine
问题描述
我是 64 位架构的新手.你能告诉我 64 位 linux 机器中文件映射支持的最大文件大小是多少.我想通过文件映射打开20GB以上的文件,可以吗?
I'm new to 64-bits architecture. Could you tell me what's MAX file size supported by file mapping in 64 bits linux machine. I want to open more than 20GB files by file mapping, is it available?
我写了一个示例代码.但是当我在 GBSIZE 偏移量中获取指针的值时,它会导致 Bus Error:
I write a sample code. But it causes Bus Error when I get the value of the pointer in GBSIZE offset:
unsigned char* pCur = pBegin + GBSIZE;
//pBegin is the pointer returned by mmap
printf("%c",*pCur);
顺便说一句,printf("%c",*pBegin);
工作正常.和我的地址大小:38 位物理,48 位虚拟
BTW, printf("%c",*pBegin );
works fine. and my address sizes : 38 bits physical, 48 bits virtual
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
//#define FILEPATH "smallfile"
#define FILEPATH "bigfile"
#define GBSIZE (1024L*1024L*1024L)
#define TBSIZE (1024L*GBSIZE)
#define NUMSIZE (20L * GBSIZE)
//#define NUMSIZE (10)
#define FILESIZE (NUMINTS * sizeof(int))
int main(int argc, char *argv[])
{
int i;
int fd;
unsigned char *pBegin;
fd = open(FILEPATH, O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
exit(EXIT_FAILURE);
}
pBegin = mmap(0, NUMSIZE, PROT_READ, MAP_SHARED, fd, 0);
if (pBegin == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
/** ERROR happens here!!! **/
unsigned char* pCur = pBegin + GBSIZE;
printf("%c",*pCur);
if (munmap(pBegin, NUMSIZE) == -1) {
perror("Error un-mmapping the file");
}
close(fd);
return 0;
}
推荐答案
尽管指针是 64 位宽,但大多数处理器实际上并不支持使用完整 64 位的虚拟地址.要查看您的处理器支持的虚拟地址大小,请查看 /proc/cpuinfo
(通常为 48 位).
Although pointers are 64-bit wide, most processors do not actually support virtual addresses using the full 64 bits. To see what size virtual addresses your processor supports, look in /proc/cpuinfo
(48 bits is typical).
grep "address sizes" /proc/cpuinfo
此外,一半的虚拟地址空间由内核使用,用户空间不可用 - 在当前的 Linux 实现中留下 47 位.
Additionally, half of the virtual address space is used by the kernel and not available to userspace - leaving 47 bits in the current Linux implementation.
然而,即使考虑到这一点,您仍然有足够空间来存放 20GB 的文件.理论上47位意味着128TB的虚拟地址空间.
However, even taking this into account, you will still have plenty of room for a 20GB file. 47 bits in theory means a virtual address space of 128TB.
这篇关于64位机器中的最大文件映射大小是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!