加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

shm_open()和ftruncate()的目的是什么?

发布时间:2020-12-16 04:56:11 所属栏目:百科 来源:网络整理
导读:当我们创建一个共享内存时,我们使用shm_open()和ftruncate()函数.根据我的信息,shm_open()创建一个共享内存区域.然后我们使用ftruncate()函数来配置共享内存区域的大小. 那么当shm_open()还不知道大小时,它是如何首先创建内存区域的?如果不是这样,我完全错
当我们创建一个共享内存时,我们使用shm_open()和ftruncate()函数.根据我的信息,shm_open()创建一个共享内存区域.然后我们使用ftruncate()函数来配置共享内存区域的大小.

那么当shm_open()还不知道大小时,它是如何首先创建内存区域的?如果不是这样,我完全错了,那么请告诉我shm_open()和ftruncate()的目的.提前致谢!!!

解决方法

shm_open的要点是你可以打开一个现有的内存区域.但是,如果它不存在并且你创建它,shm_open在创建文件时表现得像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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读