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