Use PHP to operate a Linux message queue for inter-process communication

Source: Internet
Author: User

When the system we develop needs to run in multi-process mode, inter-process communication becomes a crucial link. Message Queue is a way of communication between processes in Linux.

The concept and implementation of Process Communication in Linux can be viewed: http://www.ibm.com/developerworks/cn/linux/l-ipc/

The concept and implementation of message queue in Linux system can be viewed: http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
The sysvmsg module of PHP encapsulates the System V Message Queue function family in the system v ipc supported by Linux. We need to use the functions provided by the sysvmsg module for inter-process communication. Let's take a look at the example code _ 1:

<? PHP $ message_queue_key = ftok (_ file __, 'A'); $ message_queue = msg_get_queue ($ message_queue_key, 0666); var_dump ($ message_queue ); $ message_queue_status = msg_stat_queue ($ message_queue); print_r ($ message_queue_status); // write msg_send ($ message_queue, 1, "Hello, world! "); $ Message_queue_status = msg_stat_queue ($ message_queue); print_r ($ Queue); // read msg_receive ($ message_queue, 0, $ message_type, 1024, $ message, true, msg_ipc_nowait); print_r ($ message. "\ r \ n"); msg_remove_queue ($ message_queue);?>

 

The running result of this Code is as follows:

resource(4) of type (sysvmsg queue)Array(    [msg_perm.uid] => 1000    [msg_perm.gid] => 1000    [msg_perm.mode] => 438    [msg_stime] => 0    [msg_rtime] => 0    [msg_ctime] => 1279849495    [msg_qnum] => 0    [msg_qbytes] => 16384    [msg_lspid] => 0    [msg_lrpid] => 0)Array(    [msg_perm.uid] => 1000    [msg_perm.gid] => 1000    [msg_perm.mode] => 438    [msg_stime] => 1279849495    [msg_rtime] => 0    [msg_ctime] => 1279849495    [msg_qnum] => 1    [msg_qbytes] => 16384    [msg_lspid] => 2184    [msg_lrpid] => 0)Hello,World!

 

We can see that "Hello, world!" has been successfully read from the message queue !" String

The following lists the main functions in the sample code:

The ftok (string $ pathname, string $ proj) manual explains convert a pathname and a project identifier to a system v ipc key. The key value returned by this function corresponds to a message queue in Linux. You must call this function before obtaining the reference of a message queue. Msg_get_queue (INT $ key [, int $ perms]) msg_get_queue () returns a reference to a Message Queue Based on the input key value. If no message queue corresponds to a key value in Linux, msg_get_queue () creates a new message queue. The second parameter of the function requires an int value as the permission value for the newly created message queue. The default value is 0666. This permission value is the same as the value used in the Linux Command chmod, because everything in the Linux system is a file. Msg_send (resource $ queue, int $ msgtype, mixed $ message [, bool $ serialize [, bool $ blocking [, Int & $ errorcode]) as the name suggests, this function is used to write data to a message queue. The msg_stat_queue (resource $ Queue) function returns the metadata of the message queue. The information in the message queue metadata is complete, including the number of messages to be read in the message queue and the process ID of the last read/write queue. The sample code returns the number of messages to be read in the queue msg_qnum value in the array returned by calling the function in row 8th. Msg_receive (resource $ queue, int $ desiredmsgtype, Int & $ msgtype, int $ maxsize, mixed & $ message [, bool $ unserialize [, int $ flags [, int & $ errorcode]) msg_receive is used to read data in the message queue. Msg_remove_queue (resource $ Queue) msg_remove_queue is used to destroy a queue.

 

The example code _ 1 only shows the application of PHP to operate Message Queue functions. The following code describes the scenario of inter-process communication.

<? PHP $ message_queue_key = ftok (_ file __, 'A'); $ message_queue = msg_get_queue ($ message_queue_key, 0666); $ PIDs = array (); for ($ I = 0; $ I <5; $ I ++) {// create a sub-process $ PIDs [$ I] = pcntl_fork (); if ($ PIDs [$ I]) {echo "no. $ I child process was created, the PID is $ PIDs [$ I] \ r \ n ";} elseif ($ PIDs [$ I] = 0) {$ pid = posix_getpid (); echo "process. $ PID is writing now \ r \ n "; msg_send ($ message_queue, 1," this is process. $ pi D's data \ r \ n "); posix_kill ($ PID, sigterm);} do {msg_receive ($ message_queue, 0, $ message_type, 1024, $ message, true, msg_ipc_nowait); echo $ message; // you need to judge whether the queue is empty. If it is empty, exit // break;} while (true)?>

 

The running result is:

No.0 child process was created, the pid is 5249No.1 child process was created, the pid is 5250No.2 child process was created, the pid is 5251No.3 child process was created, the pid is 5252No.4 child process was created, the pid is 5253process.5251 is writing nowthis is process.5251's dataprocess.5253 is writing nowprocess.5252 is writing nowprocess.5250 is writing nowthis is process.5253's datathis is process.5252's datathis is process.5250's dataprocess.5249 is writing nowthis is process.5249's data

 

 

The running results of this program are different each time. This shows the Asynchronization of multiple processes. The FIFO feature of Message Queue can also be seen from the results.

The above is my research experience. Next, we will continue to study how PHP uses signals, sockets, and other methods for inter-process communication.

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.