PHP Process Communication Foundation--SHMOP, SEM series function use

Source: Internet
Author: User

PHP Process Communication Foundation--SHMOP, SEM series function use

The principle of process communication is to open up a shared area in the system, whether it is a pipeline or shared memory, this is the principle. If you have this concept in mind, it will be convenient to understand the code. As the official online shmop function of the English interpretation of a little more complex, so while practicing, while the simple translation of the next. The memory expansion function of the semaphore is relatively simple and very well understood. So there is no translation. This blog is the basis of communication, so just put the example code, according to the code to knock over, you can understand a 7788. If you really do not understand, you can copy it, the direct interruption of debugging, but also a good learning mentality.
This article references:
http://php.net/manual/en/ref.sem.php
http://php.net/manual/en/ref.shmop.php
Http://www.cnblogs.com/nickbai/articles/6133354.html

1, SHMOP series function use

<?php
//Using SHMOP series functions
Set_time_limit (0);
$shm _key= Ftok (__file__,' t ');
$shm _id= Shmop_open ($shm _key,"C",0655,1024x768);
$size= Shmop_write ($shm _id,"Hello World". Php_eol,0);
Shmop_write ($shm _id,' I Love You '. Php_eol,$size+1);
Echo $size. Php_eol;
$data= Shmop_read ($shm _id,0, -);
Var_dump ($data);
Shmop_delete ($shm _id);
Shmop_close ($shm _id);

//shared Memory Functions

/**
* Shmop_open (int $key, string $flags, int $mode, int $size)
* $key Shared Memory key
* There are several values for $flags:
* A: Create a read-only shared memory area.
* C: If the shared memory area already exists, open the shared memory area and try to read and write. Otherwise, create a new shared memory area
* W: Create a read/write shared memory area
* N: Create a shared memory area and return failure if it already exists
*
* $mode Read and Write permissions. such as 0755 0644 etc.
* $size request the size of the shared memory area
*/

/**
* Shmop_read (Resource $shmid, int $start, int $count)
* The data will be read from the shared memory block
* $shmid shared Memory ID, resource type
* $start start reading from that byte of shared memory
* How many bytes $count read at a time.
* If the count value is less than the length of information sent, the information is truncated.
*/

/**
* Shmop_write (Resource $shmid, string $data, int $offset)
* Write data to a shared memory block
* $data the data that will be written
* $offset start writing from that location in the shared memory block.
* The return value of this function is the length of the write data.
*/

/**
* Shmop_size (Resource $shmid);
* Returns the size of the current shared memory block, already used
*/


/**
* Shmop_delete (Resource $shmid)
* Delete a shared memory block, delete the reference relationship
*/

/**
* Shmop_close (Resource $shmid)
* Turn off shared memory blocks
* You must use Shmop_delete before you can continue using Shmop_close
*/

2, SHM series function use

<?php
//using semaphores to expand shared memory
$key= Ftok (__file__,' A ');
$share _key=1;

//Create a shared memory with a size of 1024 bytes and a permission of 755
$shm _id= Shm_attach ($key,1024x768,0755);

//Put a key=>value in the shared memory
$message 1="I Love Roverliang";
Shm_put_var ($shm _id,$share _key,$message 1);

///Reuse key, the value of the previous setting will be overwritten by the value of the latter setting.
$message 2="I Love Rover";
Shm_put_var ($shm _id,$share _key,$message 2);

//Read a shared memory value
$read _message= Shm_get_var ($shm _id,$share _key);
Echo $read _message. Php_eol;

//Not taken away, but read
$read _message2= Shm_get_var ($shm _id,$share _key);
Echo $read _message2. Php_eol;

//Determine if a value exists in shared memory
$isexists= Shm_has_var ($shm _id,$share _key);
Var_dump ($isexists);

//Delete a value
Shm_remove_var ($shm _id,$share _key);

//Determine if a value exists in shared memory
$isexists= Shm_has_var ($shm _id,$share _key);
Var_dump ($isexists);

//delete a shared memory
Shm_remove ($shm _id);

//Close shared memory connections
Shm_detach ($shm _id);

PHP Process Communication Foundation--SHMOP, SEM series function use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.