一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

PHP下操作Linux消息隊列完成進(jìn)程間通信的方法

關(guān)于Linux系統(tǒng)進(jìn)程通信的概念及實現(xiàn)可查看:http://www.ibm.com/developerworks/cn/linux/l-ipc/
  關(guān)于Linux系統(tǒng)消息隊列的概念及實現(xiàn)可查看:http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
  php的sysvmsg模塊是對Linux系統(tǒng)支持的System V IPC中的System V消息隊列函數(shù)族的封裝。我們需要利用sysvmsg模塊提供的函數(shù)來進(jìn)進(jìn)程間通信。先來看一段示例代碼_1:
復(fù)制代碼 代碼如下:
<?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);
//向消息隊列中寫
msg_send($message_queue, 1, "Hello,World!");
$message_queue_status = msg_stat_queue($message_queue);
print_r($message_queue_status);
//從消息隊列中讀
msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
print_r($message."/r/n");
msg_remove_queue($message_queue);
?>

這段代碼的運行結(jié)果如下:
復(fù)制代碼 代碼如下:
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!

可以看到已成功從消息隊列中讀取“Hello,World!”字符串
  下面列舉一下示例代碼中的主要函數(shù):
復(fù)制代碼 代碼如下:
ftok ( string $pathname , string $proj )
手冊上給出的解釋是:Convert a pathname and a project identifier to a System V IPC key。這個函數(shù)返回的鍵值唯一對應(yīng)linux系統(tǒng)中一個消息隊列。在獲得消息隊列的引用之前都需要調(diào)用這個函數(shù)。
msg_get_queue ( int $key [, int $perms ] )
msg_get_queue()會根據(jù)傳入的鍵值返回一個消息隊列的引用。如果linux系統(tǒng)中沒有消息隊列與鍵值對應(yīng),msg_get_queue()將會創(chuàng)建一個新的消息隊列。函數(shù)的第二個參數(shù)需要傳入一個int值,作為新創(chuàng)建的消息隊列的權(quán)限值,默認(rèn)為0666。這個權(quán)限值與linux命令chmod中使用的數(shù)值是同一個意思,因為在linux系統(tǒng)中一切皆是文件。
msg_send ( resource $queue , int $msgtype , mixed $message [, bool $serialize [, bool $blocking [, int &$errorcode ]]] )
顧名思義,該函數(shù)用來向消息隊列中寫數(shù)據(jù)。
msg_stat_queue ( resource $queue )
這個函數(shù)會返回消息隊列的元數(shù)據(jù)。消息隊列元數(shù)據(jù)中的信息很完整,包括了消息隊列中待讀取的消息數(shù)、最后讀寫隊列的進(jìn)程ID等。示例代碼在第8行調(diào)用該函數(shù)返回的數(shù)組中隊列中待讀取的消息數(shù)msg_qnum值為0。
msg_receive ( resource $queue , int $desiredmsgtype , int &$msgtype , int $maxsize , mixed &$message [, bool $unserialize [, int $flags [, int &$errorcode ]]] )
msg_receive用于讀取消息隊列中的數(shù)據(jù)。
msg_remove_queue ( resource $queue )
msg_remove_queue用于銷毀一個隊列。

示例代碼_1只是展示了php操作消息隊列函數(shù)的應(yīng)用。下面的代碼具體描述了進(jìn)程間通信的場景
復(fù)制代碼 代碼如下:
<?php
$message_queue_key = ftok(__FILE__, 'a');
$message_queue = msg_get_queue($message_queue_key, 0666);
$pids = array();
for ($i = 0; $i < 5; $i++) {
//創(chuàng)建子進(jìn)程
$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;
//需要判斷隊列是否為空,如果為空就退出
//break;
} while(true)
?>

運行結(jié)果為:
復(fù)制代碼 代碼如下:
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

這段程序每次的運行結(jié)果都會不同,這正說明了多進(jìn)程的異步性。從結(jié)果也能看出消息隊列FIFO特性。
以上便是我研究的一點心得。接下來將會繼續(xù)研究php利用信號、socket等進(jìn)行進(jìn)程間通信的方法。

php技術(shù)PHP下操作Linux消息隊列完成進(jìn)程間通信的方法,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美精品福利视频 | 国产精品特级毛片一区二区三区 | 亚洲欧洲无码一区二区三区 | 亚洲国产精品婷婷久久久久 | 国产一区二区三区免费播放 | 久久国产精品久久久 | 99久久免费国产特黄 | 久久国产免费一区二区三区 | 中文字幕一区二区三区不卡 | 手机在线色视频 | 日韩欧美成人乱码一在线 | 久久久91精品国产一区二区三区 | 日韩亚洲欧美日本精品va | 色婷婷精品大全在线视频 | 欧洲成人在线观看 | 久久精品亚洲综合一品 | 久久精品视频2 | 久久久久久久网站 | 婷婷亚洲久悠悠色在线播放 | 香蕉一区二区三区观 | 国产午夜免费视频片夜色 | 在线观看激情 | 色婷婷一区二区三区四区成人网 | 久草小区二区三区四区网页 | 中文字幕不卡一区 二区三区 | 亚洲一区不卡 | 国产一区二区不卡视频 | 色视频在线观看在线播放 | 在线一区二区观看 | 五月深爱网| 色美女网站 | 美女视频国产 | 丁香六月综合网 | 国内在线精品 | 国产一区二区高清在线 | 福利精品 | 久久精品国产亚洲麻豆 | 日韩视频免费一区二区三区 | 国内久久久久久久久久 | 成人在线观看免费视频 | 思思久热re6这里有精品 |