linux mmap读取新数据,如何在Linux系统上使用mmap()进行读写

我需要在Linux中使用mmap()创建一些流入和派出类.为此,我尝试制作一些测试代码,将一些整数写入文件,保存,再次加载并将文件中的数据写入cout.如果该测试代码有效,那么之后将流输入和输出不会成为问题.

当我刚开始时,我遇到了段错误,如果我没有得到任何结果,那么我用谷歌搜索了一下.我发现这本书http://www.advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf在第107页左右有一些有用的代码.我复制粘贴该代码并做了一些小改动并得到了这段代码:

int fd;

void* file_memory;

/* Prepare a file large enough to hold an unsigned integer. */

fd = open ("mapTester", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);

//Make the file big enough

lseek (fd, 4 * 10 + 1, SEEK_SET);

write (fd, "", 1);

lseek (fd, 0, SEEK_SET);

/* Create the memory mapping. */

file_memory = mmap (0, 4 * 10, PROT_WRITE, MAP_SHARED, fd, 0);

close (fd);

/* Write a random integer to memory-mapped area. */

sprintf((char*) file_memory, "%d\n", 22);

/* Release the memory (unnecessary because the program exits). */

munmap (file_memory, 4 * 10);

cout << "Mark" << endl;

//Start the part where I read from the file

int integer;

/* Open the file. */

fd = open (argv[1], O_RDWR, S_IRUSR | S_IWUSR);

/* Create the memory mapping. */

file_memory = mmap (0, 4 * 10, PROT_READ | PROT_WRITE,

MAP_SHARED, fd, 0);

close (fd);

/* Read the integer, print it out, and double it. */

scanf ((char *) file_memory, "%d", &integer);

printf ("value: %d\n", integer);

sprintf ((char*) file_memory, "%d\n", 2 * integer);

/* Release the memory (unnecessary because the program exits). */

munmap (file_memory, 4 * 10);

但是在“标记”cout之后,我得到了一个细分市场.

然后我用这个替换“读取部分”:

fd = open("mapTester", O_RDONLY);

int* buffer = (int*) malloc (4*10);

read(fd, buffer, 4 * 10);

for(int i = 0; i < 1; i++)

{

cout << buffer[i] << endl;

}

这是一些工作代码,告诉我文件是空的.我尝试了几种方法来写入映射而不会对结果进行任何更改.

那我怎么能让我的代码写出来呢?

我的mmap读代码是否正常(以防你可以看到一些明显的缺陷)?

我找到了一些其他资源但对我没有帮助,但由于我是新用户,我可能只发布最多2个链接.

解决方法:

您应该测试mmap的结果.如果它给出MAP_FAILED,请查看errno以找出原因.

并且你最好mmap多个页面,通常每个4K字节,并由sysconf(_SC_PAGESIZE)给出

您可以使用stat查找某些给定文件的大小(以及许多其他数字).

您可以在现有Linux程序上使用strace来了解他们正在做什么系统调用.

关于/ proc /等,另见this

标签:linux,mmap,segment

来源: https://codeday.me/bug/20190621/1251104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值