PHP Shared Memory Usage example analysis, PHP shared instance analysis
This example describes the PHP shared memory usage. Share to everyone for your reference, as follows:
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 inner Save segment, or if the memory segment already exists, try to open it to read and write W: writable memory Segment N: Creates a new memory segment that fails if the memory segment already exists $mode: Octal format 0655$size: Open Data size byte */$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 = Shmop_wri Te ($shm _id, ' Songjiankang ', 0); echo "write into {$size}"; #读取的范围也必须在申请的内存范围之内, otherwise fail $data = Shmop_read ($shm _id, 0, 100); 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 team operation and use Unset () as soon as possible to release the critical section **/class shmqueue{private $maxQSize = 0; Large 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->filep TR); $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)/$th is->blocksize; Public Function EnQueue ($value) {if ($this->ptrinc ($this->rear) = = $this->front) {//Team full return FAL Se } $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
More about PHP related content readers can view the topic: "PHP Basic Grammar Introductory Tutorial", "PHP error and Exception handling method summary", "PHP Programming algorithm Summary" and "PHP object-oriented Programming introduction Tutorial"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- A method for sharing memory between PHP processes of a single server
- PHP Shared memory Segment sample sharing
- PHP cross-domain cookie sharing usage method
- PHP uses session ID to implement session sharing and login validation code
- PHP enables multi-server sharing SESSION data
- How to implement NFS sharing for multi-server session sharing in PHP
- A method of implementing multi-server sharing session data with PHP
- How Linux Message Queuing completes inter-process communication under PHP
http://www.bkjia.com/PHPjc/1099077.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099077.html techarticle PHP shared Memory Usage example analysis, PHP shared instance Analysis This article describes the PHP shared memory usage. Share to everyone for reference, as follows: Shared memory is mainly used for process ...