How to operate a Linux message queue to accomplish interprocess communication under PHP _php tips

Source: Internet
Author: User
Tags message queue mixed
About Linux system process communication concepts and implementation can be viewed: http://www.ibm.com/developerworks/cn/linux/l-ipc/
About Linux system Message Queuing concept and implementation can be viewed: http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
The PHP sysvmsg module is a package of System V Message Queuing function families in the System V IPC supported by Linux systems. We need to use the functions provided by the SYSVMSG module to communicate between processes. Let's take a look at a sample code _1:
Copy Code code as follows:

<?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);
Writing to message queues
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 results of this code run as follows:
Copy Code code 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!

You can see that the "hello,world!" has been read successfully from the message queue String
Here's a list of the main functions in the sample code:
Copy Code code as follows:

Ftok (String $pathname, String $proj)
The explanation given in the manual is: Convert a pathname and a project identifier to a System V IPC key. The key value returned by this function uniquely corresponds to a message queue in the Linux system. You need to call this function before you can get a reference to Message Queuing.
Msg_get_queue (int $key [, int $perms])
Msg_get_queue () returns a reference to a message queue based on the key value passed in. If there is no message queue in the Linux system that corresponds to the key value, Msg_get_queue () will create a new message queue. The second parameter of the function needs to pass in an int value as the permission value for the newly created message queue, which defaults to 0666. This permission value is the same as the value used in the Linux command chmod because everything is a file in a Linux system.
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 for the message queue. The information in the Message Queuing metadata is complete, including the number of messages to be read in the message queue, the process ID of the last read-write queue, and so on. The example code calls the number of messages to be read in the queue in the array returned by the function in line 8th Msg_qnum value is 0.
Msg_receive (Resource $queue, int $desiredmsgtype, int & $msgtype, int $maxsize, mixed & $message [, BOOL $unse rialize [, int $flags [, int & $errorcode]])
Msg_receive is used to read data in message queues.
Msg_remove_queue (Resource $queue)
Msg_remove_queue is used to destroy a queue.

Sample code _1 just shows the application of the PHP action Message Queuing function. The following code specifically describes the scenario of interprocess communication
Copy Code code as follows:

<?php
$message _queue_key = Ftok (__file__, ' a ');
$message _queue = Msg_get_queue ($message _queue_key, 0666);
$pids = Array ();
for ($i = 0; $i < 5; $i + +) {
To create a child 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;
Need to determine if the queue is empty and exit if it is empty
Break
} while (true)
?>

The results of the operation are:
Copy Code code as follows:

no.0 child process being created, the PID is 5249
No.1 child process being created, the PID is 5250
No.2 child process being created, the PID is 5251
No.3 child process being created, the PID is 5252
No.4 child process being 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

This program will run differently each time, which illustrates the asynchronous nature of the multiple processes. The message Queue FIFO feature can also be seen from the results.
The above is my study of a little experience. Next, we will continue to study the way PHP uses signals, sockets, etc. for interprocess 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.