百科狗-知识改变命运!
--

shmop_open() - shmop函数(共享内存)

百变鹏仔12个月前 (11-21)阅读数 20#技术干货
文章标签范例

shmop_open()

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

Create or open shared memory block

说明

shmop_open(int $key,string $flags,int $mode,int $size): resource

shmop_open() can create or open a shared memory block.

参数

$key

System's id for the shared memory block. Can be passed as a decimal or hex.

$flags

The flags that you can use:

  • "a" for access(sets SHM_RDONLY for shmat)use this flag when you need to open an existing shared memory segment for read only
  • "c" for create(sets IPC_CREATE)use this flag when you need to create a new shared memory segment or if a segment with the same key exists, try to open it for read and write
  • "w" for read & write access use this flag when you need to read and write to a shared memory segment, use this flag in most cases.
  • "n" create a new memory segment(sets IPC_CREATE|IPC_EXCL)use this flag when you want to create a new shared memory segment but if one already exists with the same flag, fail. This is useful for security purposes, using this you can prevent race condition exploits.
$mode

The permissions that you wish to assign to your memory segment, those are the same as permission for a file. Permissions need to be passed in octal form, like for example0644

$size

The size of the shared memory block you wish to create in bytes

Note:

shmop_open() - shmop函数(共享内存)

Note: the 3rd and 4th should be entered as 0 if you are opening an existing memory segment.

返回值

On success shmop_open() will return an resource that you can use to access the shared memory segment you've created.FALSE is returned on failure.

更新日志

版本说明
7.0.0 The return type of shmop_open() has been changed from int to resource.

范例

Create a new shared memory block

This example opened a shared memory block with a system id returned by ftok().

参见

  • shmop_close()Close shared memory block
  • shmop_delete()Delete shared memory block
On *nix systems shmop_open is able to create an "infinite" amount of segments when setting $key = 0.
After executing the following command twice in an interactive shell
php > $res = shmop_open(0,"n",0600,1024);
list the memory segments currently present 
$ ipcs -m
------ Shared Memory Segments --------
key    shmid   owner   perms   bytes   nattch   status
0x00000000 2293762  user    600    1024    0
0x00000000 2326531  user    600    1024    0
For any integer  0 in conjunction with the flag "n" shmop_open works like documented. It fails.
There is a little ftok function. This function isn't included into php for windows so i've grabbed it directly from linux glibc 2.3.2 source code. I hope that this can be useful.
There is the code:

sorry for my english :)
I'm having the same issue affecting XP and described below, on Mac OS X Lion.
To solve it, use before 'a' flag, then 'n'. Avoid 'c' flag.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)