PHP进程间通讯-shmop

1、新建操作类ShmopBlock

class ShmopBlock {
	private static $singleinstance;
	public static function getIntance() {
		if (!(self::$singleinstance instanceof self)) {
			self::$singleinstance = new self();
		}
		return self::$singleinstance;
	}
	private function __construct() {}
    private function __clone() {}    
    
    public function clear($key, $size = 256, $perms = 0644) {
		try {
            $_shmid = @shmop_open($key, 'c', $perms, $size);
            if ($_shmid === false) {
                exit('shmop_open error!');  
            }       
          
            shmop_delete($_shmid);
            @shmop_close($_shmid);
            return true;
		} catch (Exception $e) {
			Log::info("clear Exception: " . $e->getMessage(), Log::EXCEPT);
			return false;
		} 
    }
    
    public function writeString($key, $data, $size = 256, $perms = 0644) {
		try {    
            $_shmid = @shmop_open($key, 'c', $perms, $size);
            if ($_shmid === false) {
                exit('shmop_open error!');  
            }   +
            
            
            var_dump($_shmid);    
          
            $_length = shmop_write($_shmid, pack('a*', $data), 0);
            if ($_length === false) {
                exit('shmop_write error!');  
            }         
            
            @shmop_close($_shmid);
            
            return true;
		} catch (Exception $e) {
			Log::info("writeString Exception: " . $e->getMessage(), Log::EXCEPT);
			return false;
		}
    }
    

    public function readString($key, $size = 256, $perms = 0644) { 
		try {
            $_shmid = @shmop_open($key, 'c', $perms, $size);
            if ($_shmid === false) {
                exit('shmop_open error!');  
            }   
            
            $_data = unpack('a*', shmop_read($_shmid, 0, $size));
            if ($_data === false) {
                exit('shmop_read error!');  
            }                    
            @shmop_close($_shmid);
            
            return $_data;
		} catch (Exception $e) {
			Log::info("readString Exception: " . $e->getMessage(), Log::EXCEPT);
			return false;
		}
    }
    
    public function writeArray($key, $data, $size = 256, $perms = 0644) {
		try {    
            $_shmid = @shmop_open($key, 'c', $perms, $size);
            if ($_shmid === false) {
                exit('shmop_open error!');  
            }       
          
            $_length = shmop_write($_shmid, json_encode($data, JSON_UNESCAPED_UNICODE), 0);
            if ($_length === false) {
                exit('shmop_write error!');  
            }         
            
            @shmop_close($_shmid);
            
            return true;
		} catch (Exception $e) {
			Log::info("writeArray Exception: " . $e->getMessage(), Log::EXCEPT);
			return false;
		}
    }
    
    public function readArray($key, $size = 256, $perms = 0644) {
		try {
            $_shmid = @shmop_open($key, 'c', $perms, $size);
            if ($_shmid === false) {
                exit('shmop_open error!');  
            }   
            
            $_data = shmop_read($_shmid, 0, $size);
            if ($_data === false) {
                exit('shmop_read error!');  
            }                    
            @shmop_close($_shmid);
            
            return json_decode(trim($_data), true);
		} catch (Exception $e) {
			Log::info("readArray Exception: " . $e->getMessage(), Log::EXCEPT);
			return false;
		}
    }
}

2、一个进程写

查看进程共享块:

[apache@ai_bk test]$ ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x91135ad2 98304      root       600        1000       11                      
0xffffffff 196610     apache     644        256        0 
ShmopBlock::getIntance()->writeString(ftok(FILE, 't'), "hello world chp");

执行结果如下:

[apache@ai_bk test]$ php test.php
resource(116) of type (shmop)
[apache@ai_bk test]$ ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x91135ad2 98304      root       600        1000       11                      
0xffffffff 196610     apache     644        256        0    

3、一个进程读

$ret = ShmopBlock::getIntance()->clear(ftok(__FILE__, 't'));    
var_dump($ret);

执行结果如下:

[apache@ai_bk test]$ php test.php
array(1) {
  [1]=>
  string(256) "hello world chp"
}

4、清除

$ret = ShmopBlock::getIntance()->clear(ftok(__FILE__, 't'));    
$ret = ShmopBlock::getIntance()->readString(ftok(__FILE__, 't'));
var_dump($ret);

执行结果如下:

[apache@ai_bk test]$ php test.php
array(1) {
  [1]=>
  string(256) ""
}
[apache@ai_bk test]$ ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x91135ad2 98304      root       600        1000       11 
分类:

发表回复