rpm:php Shared memory
Shared memory is primarily used for interprocess communication
Shared Memory in PHP has two sets of extensions that can be implemented
1,Shmop need to open --enable-shmop parameters when compiling
Instance:
$shm _key = Ftok (__file__, ' t ');/** open a shared memory int $key, string $flags, int $mode, int $size $flags: A: Access read-only memory segment C: Create a new Memory segment, or if the memory segment already exists, try to open it for reading and writing W: Read-write memory segment N: Creates a new memory segment that fails if the memory segment already exists $mode: octal format 0655$size: Data Size bytes opened */$SHM _id = Shmop_open ($shm _key, "C", 0644, 1024);/** * Write data data must be in string format, last point offset * Note: The offset must be within the specified range, otherwise not write * */ $size = SHM Op_write ($shm _id, ' Songjiankang ', 0); echo "write into {$size}"; #读取的范围也必须在申请的内存范围之内, otherwise fail $data = Shmop_read ($shm _id, 0, Var_dump ($data); #删除 just make a delete flag bit, while not allowing the new process process to read, the system will automatically delete the Shmop_delete ($shm _id) when there is no process read; #关闭该内存段shmop_close ($ SHM_ID);
2, using Semaphore expansion of the SEM class function (more convenient to use, similar to the key-value format)
Get the file token Key$key = Ftok (__dir__, ' a ');//Create a shared memory $shm_id = Shm_attach ($key, 1024, 777); Resource Typeif ($shm _id = = = False) {die (' Unable to create the shared memory segment ');} #设置一个值shm_put_var ($shm _id, 111, ' value '), #删除一个key//shm_remove_var ($shm _id, 111), #获取一个值 $value = Shm_get_var ($shm _id, 111); Var_dump ($value); #检测一个key是否存在//Var_dump (Shm_has_var ($shm _id, 111)); #从系统中移除shm_remove ($shm _id); Close and shared Memory connection Shm_detach ($shm _id);
Note: Both of these methods are not common
A message queue implemented with shared memory and semaphores
/*** uses shared memory and Semaphore implementations * * supports multiple processes and supports storage of various data types * Note: Complete the queue or queue operation and use Unset () as soon as possible to release the critical section **/class shmqueue{private $maxQSize = 0;//Team Column Maximum length private $front = 0; Team Head pointer Private $rear = 0; The tail pointer is private $blockSize = 256; The size of the block (byte) private $memSize = 25600; Maximum shared memory (byte) private $shmId = 0; Private $filePtr = './shmq.ptr '; Private $semId = 0; Public Function __construct () {$shmkey = Ftok (__file__, ' t '); $this->shmid = Shmop_open ($shmkey, "C", 0644, $this->memsize); $this->maxqsize = $this->memsize/$this->blocksize; Please apply a semaphore $this->semid = Sem_get ($shmkey, 1); Sem_acquire ($this->semid); Application for entry into the critical area $this->init (); } Private Function init () {if (file_exists ($this->fileptr)) {$contents = file_get_contents ($ THIS->FILEPTR); $data = Explode (' | ', $contents); if (isset ($data [0]) && isset ($data [1])) { $this->front = (int) $data [0]; $this->rear = (int) $data [1]; }}} Public function GetLength () {return ($this->rear-$this->front + $this->memsize) % ($this->memsize))/$this->blocksize; Public Function EnQueue ($value) {if ($this->ptrinc ($this->rear) = = $this->front) {//Team full return false; } $data = $this->encode ($value); Shmop_write ($this->shmid, $data, $this->rear); $this->rear = $this->ptrinc ($this->rear); return true; Public Function DeQueue () {if ($this->front = = $this->rear) {//team NULL return FALSE; } $value = Shmop_read ($this->shmid, $this->front, $this->blocksize-1); $this->front = $this->ptrinc ($this->front); return $this->decode ($value); } Private Function Ptrinc ($ptr) {return ($ptR + $this->blocksize)% ($this->memsize); The private function encode ($value) {$data = serialize ($value). "__eof"; Echo '; echo strlen ($data); Echo '; Echo $this->blocksize-1; Echo '; if (strlen ($data) > $this->blocksize-1) {throw new Exception (strlen ($data). "is overload block size!"); } return $data; } Private Function Decode ($value) {$data = explode ("__eof", $value); return unserialize ($data [0]); Public Function __destruct () {$data = $this->front. ' | '. $this->rear; File_put_contents ($this->fileptr, $data); Sem_release ($this->semid); Out of the critical section, releasing the Semaphore}}/* *//Incoming team operation $SHMQ = new Shmqueue (); $data = ' test data '; $SHMQ->enqueue ($data); * Unset ($SHMQ); Out of line operation $SHMQ = new Shmqueue (); $data = $shmq->dequeue (); * Unset ($SHMQ); */
Linux under the IPC command to view, with the IPCRM command can be deleted
Reference: http://www.laruence.com/2008/04/21/101.html
http://www.yuansir-web.com/2012/09/22/php
PHP Shared Memory