- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 MAP_SHARED 创建一个内存映射文件。当文件大小达到 2gb 时,我遇到了问题。下面粘贴的代码是我正在使用的(作为测试)。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#define MY_FILE "/dev/shm/mmap_test"
#define BLOCK_SIZE (1024*1024)
#define NUM_FILES 1
void mk_file(int f_num)
{
uint64_t len = 0;
int fd, j, k;
char tmp_file[1024], *x, *rr_addr;
// Open file in /dev/shm
sprintf(tmp_file, "%s%d", MY_FILE, f_num);
fd = open(tmp_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1)
{
perror("file open");
exit(-1);
}
// 16Gb file
len = 16UL * 1024 * 1024 * 1024;
printf("len: %ld Gb\n", len/(1024*1024*1024));
printf("Mapping %ld blocks\n", len/BLOCK_SIZE);
for (j = 0; j < len/BLOCK_SIZE; j++) {
// Increase the file size
ftruncate(fd, ((j + 1) * BLOCK_SIZE));
// Just mmap memory... don't have file backing
//rr_addr = mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
// MMAP a region to the file at a given offset
rr_addr = mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_NORESERVE, fd, (j * BLOCK_SIZE));
if (rr_addr == MAP_FAILED) {
perror("mmap error");
printf("INDEX: %d\n", j);
exit(-1);
}
// Write to every byte of allocated memory
x = (char *) rr_addr;
for (k = 0; k < BLOCK_SIZE; k++)
{
*x = '1';
x++;
}
}
return;
}
int main(int argc, char **argv)
{
uint64_t i;
for (i = 0; i < NUM_FILES; i++)
mk_file(i);
return 0;
}
在上面的代码中,当文件中的偏移量达到 2gb 时出现总线错误。这些是我尝试过的事情:
我想知道这是否是 linux 内核中的问题?有没有人尝试使用大于 2gb 的 MAP_SHARED 映射单个文件并成功使用(读/写)它?
最佳答案
我认为问题在于 j
是一个 int
。当 j
达到较大的值时,(j + 1) * BLOCK_SIZE
会溢出并且您的 ftruncate
调用不会执行您想要的操作。检查 ftruncate
的返回值应该可以确认这一点。
mmap man page特别调用 SIGBUS
表示尝试的访问不受文件支持。
关于c - mmap 总线错误写入超过 2Gb 的 MAP_SHARED 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22794432/
#include typedef std::vector vc; typedef std::vector vvc; vvc f() { const int N = (1 不把它还给操作系统。 因
我正在尝试训练 Dlib's train_shape_predictor_ex.cpp与 Halen数据集。我在 Release模式 中使用 Visual Studio 将代码编译为 64 位平台 作
当我试图解决this 时想到了这个问题。问题。 我有一个容量为 120 GB 的硬盘,其中 100 GB 被一个巨大的文件占用。所以 20 GB 仍然是免费的。 我的问题是,我们如何将这个巨大的文件拆
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Can you allocate a very large single chunk of memory (
我有一台运行 Linux (3.19.5-200.fc21.x86_64) 的 128 GB RAM 的计算机。但是,我不能在单个进程中分配超过 ~30 GB 的 RAM。除此之外,malloc 失败
我需要为 808704000 个 float 分配内存,大约是 3085 MB。我的电脑有 32 GB 内存,运行 64 位 Linux (CentOS 6.6)。每次我尝试分配内存时,malloc
很抱歉这个愚蠢的问题,但是当reading about 32 bits limitation ,我发现由于 Memory-Mapped 方法,MongoDB 不能存储超过 2 GB,但是: 2^32
假设我们有一个 32 位地址,那么每一位可以是 1 或 0。 所以组合总数等于2^32。 所以我们可以表示 2^32 个地址(没有单位)。 但为什么人们说 32 位地址可以表示 2^32 字节地址(为
好吧,这个问题确实是一个挑战! 背景 我正在从事一个涉及比正常数字更大的基于算术的项目。我是新手,我打算使用 4 GB 文件大小的最坏情况(我什至希望将其扩展到 5GB 上限,因为我之前看到文件大小大
我在文件系统上有大约 12 个大小为 1 GB 到 10 GB 的存储库,我需要为所有这些存储库设置自动备份(我们的旧备份脚本在计算机出现故障时丢失了) XP 64 位机器。 看完this quest
我尝试调整 Linux VM 上的操作系统磁盘大小,该 VM 通常由 azure 自动创建。我无法创建自定义操作系统磁盘 - 请告知我该怎么做? enter image description her
我在 .NET 中遇到了一个问题,我的数组受到我拥有的 RAM 数量的限制,并且我需要可以容纳至少 40 GB 字节的数组。我正在考虑使用硬盘驱动器作为虚拟数组的想法不在乎它是否慢。 我正在研究这个想
我尝试调整 Linux VM 上的操作系统磁盘大小,该 VM 通常由 azure 自动创建。我无法创建自定义操作系统磁盘 - 请告知我该怎么做? enter image description her
我的目标是看看当使用比物理 GPU 内存所能容纳的更多纹理数据时会发生什么。我的第一次尝试是加载多达 40 个 DDS 纹理,导致内存占用比 GPU 内存高得多。但是,我的场景在 9500 GT 上仍
Windows 上的 JDK 最多需要 2 GB 左右的 RAM。即使我们为 JDK 分配更多 RAM;它不接受它。如果我需要在 Windows 上运行需要 8 GB RAM 的进程;我怎样才能实现它
我有一个程序需要分配2个15亿长度的整数数组。这是一个编码挑战( https://projecteuler.net/problem=282 ),并且没有办法使用如此大的数组(如果有,请不要告诉我;我应
假设我有一个 32 位内核。 4 Gb RAM,10 Gb 交换分区。 我有一个在无限循环中有 malloc 的进程。因此,最终系统的 OOM 将终止该进程。这里有两个论点。 参数 1:因为它是 32
我有一个可以大于 4GB 的文件。我正在使用 linux split 命令按行拆分它(这是要求)。但是拆分原始文件后,我希望拆分文件的大小始终小于 2GB。原始文件大小可能在 3-5 GB 之间。我想
我有一台带有 32GB RAM 的 Mac 服务器(雪豹)。当我尝试在 Perl (v 5.10.0) 中分配超过 1.1GB 的 RAM 时,出现内存不足错误。这是我使用的脚本: #!/usr/bi
我们正在尝试运行.NET 4 Web应用程序(在8 GB,8核心Windows 2008 R2 64位Webedition服务器上),该应用程序使用Spire.doc创建Word mailmerge文
我是一名优秀的程序员,十分优秀!