消息队列是消息的链接表(一种常见的数据结构),但是这种消息队列存储于系统内核中(不是用户态),一般我们外部程序使用一个key来对消息队列进行读写操作。 在PHP中,是通过msg_*系列函数完成消息队列操作
<?php
// 创建消息队列键
$key = ftok( __DIR__, 'a' );
// 创建消息队列
$queue = msg_get_queue( $key, 0666 );
$pid = pcntl_fork();
if( $pid < 0 ){
exit( 'fork error'.PHP_EOL );
} else if( $pid > 0 ) {
// 读取消息队列
msg_receive( $queue, 0, $msgtype, 1024, $message );
echo $message.PHP_EOL;
// 清除消息队列
msg_remove_queue( $queue );
pcntl_wait( $status );
} else if( 0 == $pid ) {
// 写入消息队列
msg_send( $queue, 1, "helloword" );
exit;
}
执行
image.png
网友评论