php实现读取内存挨次号_.docx

上传人:啊飒飒 文档编号:11646284 上传时间:2021-08-27 格式:DOCX 页数:6 大小:12.75KB
返回 下载 相关 举报
php实现读取内存挨次号_.docx_第1页
第1页 / 共6页
php实现读取内存挨次号_.docx_第2页
第2页 / 共6页
php实现读取内存挨次号_.docx_第3页
第3页 / 共6页
php实现读取内存挨次号_.docx_第4页
第4页 / 共6页
php实现读取内存挨次号_.docx_第5页
第5页 / 共6页
亲,该文档总共6页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《php实现读取内存挨次号_.docx》由会员分享,可在线阅读,更多相关《php实现读取内存挨次号_.docx(6页珍藏版)》请在三一文库上搜索。

1、php实现读取内存挨次号_ 这篇文章主要介绍了php实现读取内存挨次号,非常的简洁有用,需要的伴侣可以参考下 只是做记录,osc本站应当有重复的 semWrapper.class.php /* * 信号量(Semaphore)。 * 这是一个包装类,用于解决不同平台下对“信号量”的不同实现方式。 * 目前这个类只是象征性的,在 Windows 平台下实际是空跑(并没有真的实现互斥)。 */ class SemWrapper private $hasSemSupport; private $sem; const SEM_KEY = 1; public function _construct()

2、$this-hasSemSupport = function_exists( sem_get ); if ( $this-hasSemSupport ) $this-sem = sem_get( self:SEM_KEY ); public function acquire() if ( $this-hasSemSupport ) return sem_acquire( $this-sem ); return true; public function release() if ( $this-hasSemSupport ) return sem_release( $this-sem ); r

3、eturn true; SeqGenerator.class.php /* * 挨次号发生器。 */ class SeqGenerator const SHM_KEY = 1; /* * 对挨次号发生器进行初始化。 * 仅在服务器启动后的第一次调用有效,此后再调用此方法没有实际作用。 * param int $start 产生挨次号的起始值。 * return boolean 返回 true 表示胜利。 */ static public function init( $start = 1 ) / 通过信号量实现互斥,避开对共享内存的访问冲突 $sw = new SemWrapper; if (

4、 ! $sw-acquire() ) return false; / 打开共享内存 $shm_id = shmop_open( self:SHM_KEY, n, 0644, 4 ); if ( empty($shm_id) ) / 因用法了 n 模式,假如无法打开共享内存,可以认为该共享内存已经创建,无需再次初始化 $sw-release(); return true; / 在共享内存中写入初始值 $size = shmop_write( $shm_id, pack( L, $start ), 0 ); if ( $size != 4 ) shmop_close( $shm_id ); $sw

5、-release(); return false; / 关闭共享内存,释放信号量 shmop_close( $shm_id ); $sw-release(); return true; /* * 产生下一个挨次号。 * return int 产生的挨次号 */ static public function next() / 通过信号量实现互斥,避开对共享内存的访问冲突 $sw = new SemWrapper; if ( ! $sw-acquire() ) return 0; / 打开共享内存 $shm_id = shmop_open( self:SHM_KEY, w, 0, 0 ); if

6、( empty($shm_id) ) $sw-release(); return 0; / 从共享内存中读出挨次号 $data = shmop_read( $shm_id, 0, 4 ); if ( empty($data) ) $sw-release(); return 0; $arr = unpack( L, $data ); $seq = $arr1; / 把下一个挨次号写入共享内存 $size = shmop_write( $shm_id, pack( L, $seq + 1 ), 0 ); if ( $size != 4 ) $sw-release(); return 0; / 关闭共享内存,释放信号量 shmop_close( $shm_id ); $sw-release(); return $seq; page.php /用法方法 $seq = SeqGenerator:next(); var_dump($seq); 以上所述就是本文的全部内容了,盼望大家能够喜爱。 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1