有人用过php里面的关于共享内存函数shmop_open...吗?求教。在线等。

tjsparkle 2002-07-22 10:53:31
我现在一个页面中用如下代码共享一段内存,并在第一个字节存入字符“1”。
$shm_id=shmop_open("login","c",0644,2);
shmop_write($shm_id, "1", 0);
在第二个页面中要读取这段共享内存段里面的这个值,该怎么办?
我用了
$shm_id1=shmop_open("login","a",0,0);
if($login=shmop_read($shm_id1,0,1))
echo "no";
else echo "yes";

可是运第二个页面的结果是
unable to attach or create shm segment on line 2

请问这是怎么回事?求教各位大虾了。
...全文
626 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jyc_nj 2002-07-22
  • 打赏
  • 举报
回复
我没用过,不过看手册上的例子,第一个参数是int key, 例子里用了0xfff
tjsparkle 2002-07-22
  • 打赏
  • 举报
回复
thanks.
我现在的问题的不能够访问已经建立好的共享内存,用
shmop_open("login","a",0,0)出错,是在这一句上就出错了,不是因为后面的read.
不知道有没有人用成功过的?
对了,楼上的我对权限的设置还不是很清楚,0644是怎么确定下来的,能说说吗?
谢谢了。
_Shakespeare 2002-07-22
  • 打赏
  • 举报
回复
shmop_open
(PHP 4 >= 4.0.4)

shmop_open -- Create or open shared memory block
Description
int shmop_open ( int key, string flags, int mode, int size)


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

shmop_open() takes 4 parameters: key, which is the system's id for the shared memory block, this parameter can be passed as a decimal or hex. The second parameter are 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.

The third parameter is the mode, which are 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 ex. 0644. The last parameter is size of the shared memory block you wish to create in bytes.
Note: 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 id that you can use to access the shared memory segment you've created.


Example 1. Create a new shared memory block

<?php
$shm_id = shmop_open(0x0fff, "c", 0644, 100);
?>




This example opened a shared memory block with a system id of 0x0fff.

User Contributed Notes
shmop_open
macmaster@pobox.com
30-Mar-2001 12:15

the key is a LONG variable type, meaning that the key can only be eight (8)
bytes long, which can be too short if you're using any form of automagic
key generation (like a parsed filename)



Mitchell_Shnier@ieee.orgZ
07-Nov-2001 05:51

To check whether a particular shared memory segment is already created, you
need to concatenate the "a" and "c" flags. For example
(where $SystemKey is the Unix key used by the other process(es) with which
you want to share this memory segment)...
$shm_id = shmop_open($SystemKey, "ac", 0, 0);
if ($shm_id) {
#it is already created
} else {
#you need to create it with shmop_open using "c" only
}
Using only "a" does not work (just as using only IPC_EXCL in the
Unix shmget() call is meaningless). Also, use the ipcs shell command to
see your shared memory segments.



hackie@prohost.org
20-Jan-2002 05:04

All of the problems have been addressed in the CVS, in addition the a mode
now indeed DOES attach to the segment in readonly mode (i.e. SHM_RDONLY),
so using shm_write on it would fail with a warning. It has 2 new flags w
(read/write) and n (new segment IPC_CREAT|IPC_EXCL).
And a number of segfaults fixed :)
这是php.net提供的函数和一些网友使用心得

我的看法是
$shm_id=shmop_open("login","c",0644,2);----注意权限是0644也就是
6 4 4
写,执行 执行 执行
而你在第二个页面用$login=shmop_read($shm_id1,0,1)来”读“所以提示说
unable to attach or create shm segment on line 2
浅妄薄见,望与斟酌

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧