什么是最大文件大小映射在64位机 [英] What's the max file mapping size in 64bits machine

查看:210
本文介绍了什么是最大文件大小映射在64位机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的64位架构。你能告诉我什么是按文件映射在64位Linux机器支持的最大文件大小。我想通过文件映射打开超过20GB的文件,是不是可以?

我写了一个样本code。但它会导致总线错误当我在GBSIZE指针的偏移值:

 无符号字符* pCur = pBegin + GBSIZE;
// pBegin是MMAP返回的指针
的printf(%C,* pCur);
 

顺便说一句,的printf(%C,* pBegin); 工作正常。和我的地址尺寸:38位物理,48位虚拟

下面是完整的code:

 的#include< stdio.h中>
#包括< stdlib.h中>
#包括< SYS / types.h中>
#包括< SYS / stat.h>
#包括< unistd.h中>
#包括< fcntl.h>
#包括< SYS / mman.h>

//#定义FILEPATH小文件
#定义FILEPATH大文件
#定义GBSIZE(1024L * 1024L * 1024L)
#定义TBSIZE(1024L * GBSIZE)
#定义NUMSIZE(20L * GBSIZE)
//#定义NUMSIZE(10)
#定义FILESIZE(NUMINTS *的sizeof(INT))

INT主(INT ARGC,字符* argv的[])
{
    INT I;
    INT的fd;
    无符号字符* pBegin;

    FD =打开(FILEPATH,O_RDONLY);
        如果(FD == -1){
        PERROR(错误打开文件进行读取);
        出口(EXIT_FAILURE);
    }

    pBegin = MMAP(0,NUMSIZE,PROT_READ,MAP_SHARED,FD,0);
    如果(pBegin == MAP_FAILED){
        关闭(FD);
        PERROR(错误mmapping文件);
        出口(EXIT_FAILURE);
    }

    / **错误发生在这里! ** /
    无符号字符* pCur = pBegin + GBSIZE;
    的printf(%C,* pCur);

    如果(在munmap(pBegin,NUMSIZE)== -1){
        PERROR(错误未mmapping文件);
    }
    关闭(FD);
    返回0;
}
 

解决方案

虽然指针是64位宽,大多数处理器实际上并不支持使用完整的64位虚拟地址。要查看的/ proc / cpuinfo的什么尺寸的虚拟地址,你的处理器支持,看看(48位是典型的)。

  grep的地址尺寸的/ proc / cpuinfo的
 

此外,一半的虚拟地址空间中所使用的内核,而不是提供给用户空间 - 留在目前的Linux实现47位

不过,即使考虑到这一点,你将仍然有大量的空间为20GB的文件。 47位在理论上意味着128TB的一个虚拟地址空间。

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?

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); 

BTW, printf("%c",*pBegin ); works fine. and my address sizes : 38 bits physical, 48 bits virtual

Here is the full code:

#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;
}

解决方案

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

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆