- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有几个问题基于以下来源:
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
int g;
int main(void) {
int fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int)); // set size by sizeof(int)
int *p1 = mmap(NULL, 10*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0); //now map 10*sizeof(int).
if (p1== MAP_FAILED) {
printf("*****************error");
}
*p1 = mmap(NULL, 8*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
if (p1== MAP_FAILED) {
printf("*****************error");
}
*p1=89;
return g;
}
问题 1:为什么我在将大小设置为 size_of(int) 然后映射 10*size_of(int) 时没有看到任何错误
问题二:这里创建了多少个共享内存实例?我的意思是,是否像我创建了两次 mmap 一样只创建了一个或两个共享内存?
谢谢
最佳答案
给定代码
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
int g;
int main(void) {
int fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(fd, sizeof(int)); // set size by sizeof(int)
int *p1 = mmap(NULL, 10*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0); //now map 10*sizeof(int).
*p1 = mmap(NULL, 8*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
if (!p1){
printf("*****************error");
}
*p1 = g;
*p1=89;
return g;
}
Question 1 : why i don't see any error while i set size as size_of(int) and then map 10*size_of(int)
因为您不检查 mmap()
的返回值,所以您不知道是否发生了错误。通过立即再次调用 mmap()
*p1 = mmap(NULL, 8*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
您屏蔽了第一个 mmap()
调用中的任何潜在错误,并且您还泄漏了任何已成功分配的内存。
Question 2: how many instace of shared mem is created here? i mean is there only one shared mem created or two as i did mmap twice?
如果第一次调用成功,则映射了两个内存段。如果失败,如果第二个成功,则您只映射一个。
如果第一次调用确实成功,则说明内存泄漏。
请注意,如果您尝试向 mmap()
段写入超过您使用 ftruncate()
设置的文件大小的末尾,您将不会导致文件增长。每the POSIX mmap()
documentation :
Note that references beyond the end of the object do not extend the object as the new end cannot be determined precisely by most virtual memory hardware. Instead, the size can be directly manipulated by ftruncate().
在 Linux 上,尝试访问映射文件末尾之外的 mmap()
数据可能会导致您的进程 receiving a SIGBUS
signal .
关于c - 使用 mmap 映射共享内存大小超过 ftruncate 完成的设置大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54942011/
我试图在 shm_open 和 ftruncate 之后成功地超出共享内存对象。这是代码, char *uuid = GenerateUUID(); int fd = shm_open(uuid, O
我想在每个循环中截断一个文件并重新写入。所以,我使用下面的代码。 但是,这里发生的是我在每个循环的开始处获得了一些额外的空间。没有空间,因为第一次没有添加空间(我也尝试了修剪功能)。 你能帮帮我吗?
我正在尝试使用函数 ftruncate() 来截断文件。 我从网上查到事情是这样的: int ftruncate(int fildes, off_t length); 但我不知道为参数 int fd
我正在尝试用 C++ 编写一个类,它提供一种原子追加到文件的方法,即使在写入过程中出现电源故障的情况下也是如此。 首先,我将当前文件位置(从文件开头开始的 64 位偏移量,以字节为单位)写入一个单独的
我的应用程序尝试用 ftruncate() 和 mmap() 编辑 32-33KB 和 2MB 大页面,但它退出了。 然后我意识到 ftruncate() 失败了: int fd = open("/m
我正在尝试制作一个 C 程序,通过使用 ftruncate 截断或扩展输入文件来将输入文件的大小更改为所需的大小。它还必须使用命令行参数。 例如,以下是有效输入: ./changefilesize d
我正在尝试打开一个共享内存文件并写入其中。问题是 ftruncate 正在返回 -1。 这是我的代码: #include #include #include #include #include
来自 http://pubs.opengroup.org/onlinepubs/7908799/xsh/ftruncate.html 的描述,不是很清楚 ftruncate 是如何工作的。 如果假设,
我想通过使用此文档来构建android源代码: https://source.android.com/source/building-running.html 最后一步是:'make -j16' 一段
我正在尝试修改文件的大小。我正在使用 msys2。我的代码如下所示: #include #include #include void dummy_file(){ FILE *f = fo
据我所知,当我将文件扩展到 2 GB 长度时,ftruncate(2) 不能是原子的。 但幕后究竟发生了什么?我已经应用它,当多个线程扩展文件时它似乎工作正常,但我不确定它是否不会导致任何数据丢失。
我查看了来自 here 的 ftruncate 文档,以及来自 here .对于 IBM 链接,我相应地实现了 ftruncate,但它给我 Invalid Arguments 错误。这是代码: ch
他们都可以根据我的测试改变文件大小。为什么他们都可以将文件更改为更大或更短?fallocate 和 ftruncate 有什么区别? 最佳答案 ftruncate 是一个简单的单一用途函数。 Per
平台是 ARM 上的 Ubuntu Linux。我想将一个字符串写入文件,但我希望每次都截断文件然后写入字符串,即不追加。 我有这个代码: f=fopen("/home/user1/refresh.t
来自 shm_open 手册页: A new shared memory object initially has zero length. The size of the object can be
我想创建一个共享内存对象并将其截断为特定大小。 SHMSIZE定义为512 MODE 设置为 S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_I
当我们创建共享内存时,我们使用 shm_open() 和 ftruncate() 函数。根据我的信息,shm_open() 创建一个共享内存区域。然后我们使用 ftruncate() 函数来配置共享内
我有几个问题基于以下来源: #include #include #include #include int g; int main(void) { int fd = shm_open(
当我想在 linux 中映射一些共享内存时,我会这样做: hFileMap = open(MapName, O_RDWR | O_CREAT, 438); pData = mmap(NULL, Siz
这里的最终目标是我希望能够扩展共享内存段的大小并通知进程在扩展后重新映射该段。然而,似乎在共享内存 fd 上第二次调用 ftruncate 失败并返回 EINVAL。我能找到的唯一其他问题没有答案:f
我是一名优秀的程序员,十分优秀!