How to operate a Linux message queue in PHP to complete 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. Messagequeue is a way of inter-process communication 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:
The code is as follows:
$ 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 to Message Queue
Msg_send ($ message_queue, 1, "Hello, World! ");
$ Message_queue_status = msg_stat_queue ($ message_queue );
Print_r ($ message_queue_status );
// Read from Message Queue
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:
The code is as follows:
Resource (4) of type (sysvmsg queue)
Array
(
[Msg_perm.uid] = & gt; 1000
[Msg_perm.gid] = & gt; 1000
[Msg_perm.mode] = & gt; 438
[Msg_stime] => 0
[Msg_rtime] => 0
[Msg_ctime] = & gt; 1279849495
[Msg_qnum] => 0
[Msg_qbytes] = & gt; 16384
[Msg_lspid] => 0
[Msg_lrpid] => 0
)
Array
(
[Msg_perm.uid] = & gt; 1000
[Msg_perm.gid] = & gt; 1000
[Msg_perm.mode] = & gt; 438
[Msg_stime] = & gt; 1279849495
[Msg_rtime] => 0
[Msg_ctime] = & gt; 1279849495
[Msg_qnum] => 1
[Msg_qbytes] = & gt; 16384
[Msg_lspid] = & gt; 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 code is as follows:
Ftok (string $ pathname, string $ proj)
The 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.
Msg_stat_queue (resource $ queue)
This 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 a 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.
The code is as follows:
$ 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. $ pid's data \ r \ n ");
Posix_kill ($ pid, SIGTERM );
}
}
Do {
Msg_receive ($ message_queue, 0, $ message_type, 1024, $ message, true, MSG_IPC_NOWAIT );
Echo $ message;
// Determine whether the queue is empty. exit if it is empty.
// Break;
} While (true)
?>

The running result is:
The code is as follows:
No.0 child process was created, the pid is 5249
No.1 child process was created, the pid is 5250
No. 2 child process was created, the pid is 5251
No. 3 child process was created, the pid is 5252
No. 4 child process was created, the pid is 5253
Process.5251 is writing now
This is process.5251's data
Process.5253 is writing now
Process.5252 is writing now
Process.5250 is writing now
This is process.5253's data
This is process.5252's data
This is process.5250's data
Process.5249 is writing now
This 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.