Home >Backend Development >PHP Tutorial >How to operate Linux message queue to complete inter-process communication under PHP_PHP tutorial

How to operate Linux message queue to complete inter-process communication under PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:35:231237browse

For the concept and implementation of process communication in Linux system, please view: http://www.ibm.com/developerworks/cn/linux/l-ipc/
About the concept and implementation of message queue in Linux system, please view: http:/ /www.ibm.com/developerworks/cn/linux/l-ipc/part4/
PHP’s sysvmsg module is an encapsulation of the System V message queue function family in System V IPC supported by Linux systems. We need to use the functions provided by the sysvmsg module for inter-process communication. Let’s first look at a sample code_1:

Copy code 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 the message queue
msg_send($message_queue, 1, "Hello, World!");
$message_queue_status = msg_stat_queue($message_queue);
print_r($ message_queue_status);
//Read from the message queue
msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
print_r($message."rn");
msg_remove_queue($message_queue);
?>

The result of running this code is as follows:
Copy code The 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] => [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] => ] => 0
)
Hello, World!


You can see that the "Hello, World!" string has been successfully read from the message queue
Listed below Main functions in the sample code:


Copy code
The code is 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. This function needs to be called before obtaining a reference to the message queue.
msg_get_queue ( int $key [, int $perms ] )
msg_get_queue() will return a reference to the message queue based on the passed in key value. If there is no message queue corresponding to the key value in the Linux system, 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 of the newly created message queue. The default is 0666. This permission value has the same meaning 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 to the message queue data.
msg_stat_queue ( resource $queue )
This function will return the metadata of the message queue. The information in the message queue metadata is very complete, including the number of messages to be read in the message queue, the process ID of the last read and write queue, etc. The sample code calls the function on line 8 and returns the number of messages to be read in the queue. The msg_qnum value is 0.
msg_receive ( resource $queue , int $desiredmsgtype , int &$msgtype , int $maxsize , mixed &$message [, bool $unserialize [, int $flags [, int &$errorcode ]]] )
msg_receive Used to read data in the message queue.
msg_remove_queue ( resource $queue )
msg_remove_queue is used to destroy a queue.


Sample code_1 just shows the application of PHP's message queue function. The following code specifically describes the inter-process communication scenario


Copy the code
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 child process
$pids[$i] = pcntl_fork();
if ($pids[$ i]) {
echo "No.$i child process was created, the pid is $pids[$i]rn";
} elseif ($pids[$i] == 0) {
$pid = posix_getpid();
echo "process.$pid is writing nowrn";
msg_send($message_queue, 1, "this is process.$pid's datarn");
posix_kill($pid, SIGTERM);
}
}
do {
msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
echo $message;
/ /Need to determine whether the queue is empty, and exit if it is empty
//break;
} while(true)
?>

The running result is:
Copy code 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 results of this program will be different every time it is run, which illustrates the asynchronous nature of multiple processes. The FIFO characteristics of the 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, etc. for inter-process communication.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322208.htmlTechArticleFor information about the concept and implementation of process communication in Linux systems, please view: http://www.ibm.com/developerworks/ cn/linux/l-ipc/ For information about the concept and implementation of message queues in Linux systems, please view: http://www.ib...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn