- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
当我们创建共享内存时,我们使用 shm_open()
和 ftruncate()
函数。根据我的信息,shm_open()
创建一个共享内存区域。然后我们使用 ftruncate()
函数来配置共享内存区域的大小。
shm_open()
在不知道大小的情况下如何首先创建内存区域?如果情况并非如此,我完全错了,那么请告诉我 shm_open()
和 ftruncate() 的目的。
提前致谢!!!
最佳答案
shm_open
的要点是您可以打开一个现有 内存区域。然而,如果它不存在而您要创建它,shm_open
将创建一个新的 0 字节共享内存对象,就像使用 O_CREAT
的 open
会创建一个 0 字节的文件。来自 Linux manuals :
O_CREAT
Create the shared memory object if it does not exist. The user and group ownership of the object are taken from the corresponding effective IDs of the calling process, and the object's permission bits are set according to the low-order 9 bits of mode, except that those bits set in the process file mode creation mask (see umask(2)) are cleared for the new object. A set of macro constants which can be used to define mode is listed in open(2). (Symbolic definitions of these constants can be obtained by including .)
A new shared memory object initially has zero length--the size of the object can be set using
ftruncate(2).
The newly allocated bytes of a shared memory object are automatically initialized to 0.
(强调我的)
由于 shm_open
不将新创建区域的大小作为参数(这会使系统调用/库调用复杂化,以便为各种情况添加参数),ftruncate( )
必须用于更改打开的共享内存区域的初始大小。
当然,对于已经在别处正确创建和调整大小的共享内存段,您不必使用 ftruncate
。如果您想知道它的大小,请使用 fstat
。另见 shm_overview(7)
关于c - shm_open() 和 ftruncate() 的用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36055796/
这段代码: extern crate libc; use libc::{O_CREAT, O_RDWR, shm_open}; use std::ffi::CString; fn shm_create
我正在像这样创建一个新的共享内存对象 int fd = shm_open("somekey", O_CREAT | O_RDWR, S_IRWXU | S_IRWXG); 返回值很好,我希望在/d
我正在尝试使用 shm_open() 创建一个新的共享内存文件,但我得到错误号 2(没有这样的文件或目录)。 shm_open ("/DIAG_public", O_CREAT | O_RDWR, S
我的一个 friend 正在为内存受限的 Linux 系统开发一个库。他提议使用 shm_open 为用户计算机上多个不同程序之间的进程间通信分配多个合理大小 (16MB) 的内存。 出现的问题是,如
我正在尝试打开一个共享内存,它给我没有这样的文件或目录错误。但是我在名称区域中有一个文件和一个目录。 fd_sh = shm_open("/home/angus/c_tutorials/interv
我有两个问题: 在使用 shm_open 时,如何知道我是否打开了一个已经存在的共享内存,我正在使用 O_CREATE | O_RDWR。 我正在使用 shm_open 创建/打开一个具有某个名称的共
通常,共享内存是使用映射到进程地址空间的部分磁盘文件实现的。每当在共享区域上发生内存访问时,文件系统都会参与将更改写入磁盘,这是一个很大的开销。 通常,调用 fopen() 会返回传递给 mmap()
这样做有什么好处:shm_open 跟在 mmap 之后? 为什么不创建一个常规文件,然后将 fd 传递给 mmap? 我看不出 shm_open 的优势 - 这些只是引用,不是吗? 我看过全家的男人
POSIX shm_open() 函数返回可用于访问共享内存的文件描述符。这是非常方便的,因为可以使用所有控制文件描述符的传统机制来控制共享内存。 唯一的缺点是 shm_open() 总是需要一个文件
我正在编写一个使用 POSIX 共享内存的程序,但有一个我不确定如何修复的错误。我寻找了类似的问题,但找不到与此特定问题相关的任何问题。 涉及两个文件 - server.c,其中包含程序运行的代码,以
我得到了以下成功执行的 C 代码: ... fd = shm_open(memory_package_name, O_CREAT | O_RDWR | O_EXCL , S_IRUSR | S_IWU
我有一个公共(public)的共享内存空间,多个进程可以读写它。我在使用 shm_open() 访问共享内存并使用 mmap() 写入内存映射文件时遇到了这个问题。但是,在调用我的包装器方法几次之后,
我有以下结构, typedef struct arrays { int *array; int max; int min; } array; 其中*array指向一个动态分配的
我不想创建一个新的共享内存对象,如果它不退出的话。而是在这种情况下返回错误。如果它已经存在,我只想打开它。 最佳答案 阅读shm_overview(7) & shm_open(3) . 如果 shm_
我目前正在 ubunto 上开发一个应用程序并调用 shm_open,目前默认路径在/var/run/shm 中。但是我需要将其更改为/tmp。简单地尝试以下是行不通的: fd = shm_open(
在 Linux 中,我想与其他进程共享我的进程的一些内存内容。其中一种方法是使用 shm_open 和 mmap。如下所示。 /* Create a new memory object */ fd =
我正在尝试使用我在网上找到的示例和文档来创建共享内存区域。我的目标是 IPC ,所以我可以让不同的进程相互通信。 这是我的 C 文件 #include #include #include #in
我正在使用boost库在C++程序的IPC上工作,并且发现如果我请求的内存大小大于其容量,boost::shared_memory_object不会抛出任何错误。在堆栈溢出中至少已两次询问此问题: W
描述: 我有一个名为“Projet”的项目目录,其中包含两个名为“Serveur”和“Client”的目录。 (1)Serveur包含serveur.c (2)Client包含client.c 引用t
我刚刚阅读了 shm_open 和 shmat 的联机帮助页,并正在尝试以下示例。在文件 test.c 中, int main(int argc, char **argv) { void *ret
我是一名优秀的程序员,十分优秀!