的shm_open()和ftruncate的目的()? [英] Purpose of shm_open() and ftruncate()?

查看:451
本文介绍了的shm_open()和ftruncate的目的()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们创建一个共享内存,我们使用的shm_open() ftruncate()功能。根据我的资料的shm_open()创建一个共享内存区域。然后我们使用 ftruncate()功能配置共享内存区域的大小。

When we create a shared-Memory we use shm_open() and ftruncate() function. According to my information shm_open() create a shared-memory region. And then we use ftruncate() function to configure the size of shared-memory region.

那么怎么的shm_open()的时候还不知道大小放在首位创建的内存区域?如果不是这种情况,我完全错了,那么请告诉我的shm_open() ftruncate()先谢谢了!

Well how does shm_open() creates the memory region in the first place when it doesn't yet know the size? And if this is not the case and I am totally wrong, then please tell me the purpose of shm_open() and ftruncate(). Thanks in Advance!!!

推荐答案

的shm_open 的主要一点是,你可以打开的现有内存区。然而,在这情况下,它不存在,你会创建的shm_open 行为像打开创建文件时;在新创建的内存区域的具有0尺寸从 Linux指南手册

The main point of shm_open is that you can open an existing memory area. However in the case that it didn't exist and you'd create it, shm_open behaves like open when creating a file; the newly created memory area has size of 0. From Linux manuals:

O_CREAT

创建共享内存对象,如果它不存在。对象的用户和组所有权从调用进程的相应的有效ID的采取,并且该对象的许可位被设置根据低阶的模式9位,不同的是在处理文件模式创建掩码设置这些位(请参阅umask(2))为新对象被清除。它可以用来定义模式的一组宏常数列在开放(2)。 (这些常数的符号定义可以通过包括得到。)

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 .)

一个新的共享内存对象最初长度为零 - 对象的大小可以通过设置 ftruncate(2) 新。共享内存对象的分配字节自动初始化为0。

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()必须用一个开放的共享内存区的大小从最初的大小而改变。

Since the shm_open does not take the size of newly created area as an argument (it'd complicate the system call / library call to add arguments for all sorts of cases), ftruncate() must used to change the size of an opened shared memory area from its initial size.

当然,你不必使用 ftruncate 为已经正确地创建和调整其他地方的共享内存段。如果你想知道它的大小,使用 FSTAT 。另请参见 shm_overview(7)

Of course you do not have to use ftruncate for a shared memory segment that is already properly created and resized elsewhere. Should you want to know its size, use fstat. See also shm_overview(7)

这篇关于的shm_open()和ftruncate的目的()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆