CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
【经验总结】不能实施并行处理的情况 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  PHP

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

楼主tjsparkle(不混csdn)2002-07-22 10:53:31 在 Web 开发 / PHP 提问

我现在一个页面中用如下代码共享一段内存,并在第一个字节存入字符“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  
   
  请问这是怎么回事?求教各位大虾了。 问题点数:20、回复次数:4Top

1 楼_Shakespeare(网络骑士)回复于 2002-07-22 11:26:14 得分 18

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  
  浅妄薄见,望与斟酌  
     
  Top

2 楼tjsparkle(不混csdn)回复于 2002-07-22 11:45:35 得分 0

thanks.  
  我现在的问题的不能够访问已经建立好的共享内存,用  
  shmop_open("login","a",0,0)出错,是在这一句上就出错了,不是因为后面的read.  
  不知道有没有人用成功过的?  
  对了,楼上的我对权限的设置还不是很清楚,0644是怎么确定下来的,能说说吗?  
  谢谢了。  
  Top

3 楼jyc_nj(老蔣)回复于 2002-07-22 12:05:16 得分 2

我没用过,不过看手册上的例子,第一个参数是int   key,   例子里用了0xfffTop

相关问题

  • 能在win2k下通过shmop_*()函数操作共享内存嘛?
  • 内存函数大比拼
  • 内存拷贝函数?
  • PHP的copy函数
  • 如何在子函数的子函数中分配内存?
  • 重载函数内存怎样分配?
  • 如何看内存中的函数:
  • 讨论内存分配函数malloc,内存释放函数free,如何实现?
  • PHP 函数 转成 ASP函数
  • PHP函数fopen的问题

关键词

  • 内存
  • 页面
  • 执行
  • access
  • shmop
  • shm
  • segment
  • shared memory
  • 共享内存
  • ipc

得分解答快速导航

  • 帖主:tjsparkle
  • _Shakespeare
  • jyc_nj

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
惹火投票。。火热进行中...
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
CSDN网站24小时值班电话:13552009689
Copyright © 2000-2009, CSDN.NET, All Rights Reserved
GongshangLogo