【轉】PHP程序及程序間通訊
轉,原文:https://www.cnblogs.com/qixidi/p/10414689.html
-----------------
一、引言
程序是一個具有獨立功能的程式關於某個資料集合的一次執行活動。換句話說就是,在系統排程多個cpu的時候,一個程式的基本單元。程序對於大多數的語言都不是一個陌生的概念,作為"世界上最好的語言PHP"當然也例外。
二、環境
php中的程序是以擴充套件的形式來完成。通過這些擴充套件,我們能夠很輕鬆的完成程序的一系列動作。
- pcntl擴充套件:主要的程序擴充套件,完成程序創建於等待操作。
- posix擴充套件:完成posix相容機通用api,如獲取程序id,殺死程序等。
- sysvmsg擴充套件:實現system v方式的程序間通訊之訊息佇列。
- sysvsem擴充套件:實現system v方式的訊號量。
- sysvshm擴充套件:實現system v方式的共享記憶體。
- sockets擴充套件:實現socket通訊。
這些擴充套件只能在linux/mac中使用,window下是不支援。最後建議php版本為5.5+。
相關程式碼:
程序相關程式碼
三、簡單的例子
一個簡單的PHP多程序例子,該例子中,一個子程序,一個父程序。子程序輸出5次,退出程式。
$parentPid = posix_getpid();
echo "parent progress pid:{$parentPid}\n";
$childList = array();
$pid = pcntl_fork();
if ( $pid == -1) {
// 建立失敗
exit("fork progress error!\n");
} else if ($pid == 0) {
// 子程序執行程式
$pid = posix_getpid();
$repeatNum = 5;
for ( $i = 1; $i <= $repeatNum; $i++) {
echo "({$pid})child progress is running! {$i} \n";
$rand = rand(1,3);
sleep($rand);
}
exit("({$pid})child progress end!\n");
} else {
// 父程序執行程式
$childList[$pid] = 1;
}
// 等待子程序結束
pcntl_wait($status);
echo "({$parentPid})main progress end!";
完美,終於建立了一個子程序,一個父程序。完了麼?沒有,各個程序之間相互獨立的,沒有任何交集,使用範圍嚴重受到現在。怎麼辦,哪就程序間通訊(interprogress communication)唄。
四、程序間通訊(IPC)
通常linux中的程序通訊方式有:訊息佇列、訊號量、共享記憶體、訊號、管道、socket。
1.訊息佇列
訊息佇列是存放在記憶體中的一個佇列。如下程式碼將建立3個生產者子程序,2個消費者子程序。這5個程序將通過訊息佇列通訊。
$parentPid = posix_getpid();
echo "parent progress pid:{$parentPid}\n";$childList = array();
// 建立訊息佇列,以及定義訊息型別(類似於資料庫中的庫)
$id = ftok(__FILE__,'m');
$msgQueue = msg_get_queue($id);
const MSG_TYPE = 1;
// 生產者
function producer(){
global $msgQueue;
$pid = posix_getpid();
$repeatNum = 5;
for ( $i = 1; $i <= $repeatNum; $i++) {
$str = "({$pid})progress create! {$i}";
msg_send($msgQueue,MSG_TYPE,$str);
$rand = rand(1,3);
sleep($rand);
}
}
// 消費者
function consumer(){
global $msgQueue;
$pid = posix_getpid();
$repeatNum = 6;
for ( $i = 1; $i <= $repeatNum; $i++) {
$rel = msg_receive($msgQueue,MSG_TYPE,$msgType,1024,$message);
echo "{$message} | consumer({$pid}) destroy \n";
$rand = rand(1,3);
sleep($rand);
}
}
function createProgress($callback){
$pid = pcntl_fork();
if ( $pid == -1) {
// 建立失敗
exit("fork progress error!\n");
} else if ($pid == 0) {
// 子程序執行程式
$pid = posix_getpid();
$callback();
exit("({$pid})child progress end!\n");
}else{
// 父程序執行程式
return $pid;
}
}
// 3個寫程序
for ($i = 0; $i < 3; $i ++ ) {
$pid = createProgress('producer');
$childList[$pid] = 1;
echo "create producer child progress: {$pid} \n";
}
// 2個寫程序
for ($i = 0; $i < 2; $i ++ ) {
$pid = createProgress('consumer');
$childList[$pid] = 1;
echo "create consumer child progress: {$pid} \n";
}
// 等待所有子程序結束
while(!empty($childList)){
$childPid = pcntl_wait($status);
if ($childPid > 0){
unset($childList[$childPid]);
}
}
echo "({$parentPid})main progress end!\n";
由於訊息佇列去資料是,只有一個程序能去到,所以不需要額外的鎖或訊號量。
2. 訊號量與共享記憶體
訊號量
:是系統提供的一種原子操作,一個訊號量,同時只有你個程序能操作。一個程序獲得了某個訊號量,就必須被該程序釋放掉。
共享記憶體
:是系統在記憶體中開闢的一塊公共的記憶體區域,任何一個程序都可以訪問,在同一時刻,可以有多個程序訪問該區域,為了保證資料的一致性,需要對該記憶體區域加鎖或訊號量。
以下,建立多個程序修改記憶體中的同一個值。
$parentPid = posix_getpid();
echo "parent progress pid:{$parentPid}\n";
$childList = array();
// 建立共享記憶體,建立訊號量,定義共享key
$shm_id = ftok(__FILE__,'m');
$sem_id = ftok(__FILE__,'s');
$shareMemory = shm_attach($shm_id);
$signal = sem_get($sem_id);
const SHARE_KEY = 1;
// 生產者
function producer(){
global $shareMemory;
global $signal;
$pid = posix_getpid();
$repeatNum = 5;
for ( $i = 1; $i <= $repeatNum; $i++) {
// 獲得訊號量
sem_acquire($signal);
if (shm_has_var($shareMemory,SHARE_KEY)){
// 有值,加一
$count = shm_get_var($shareMemory,SHARE_KEY);
$count ++;
shm_put_var($shareMemory,SHARE_KEY,$count);
echo "({$pid}) count: {$count}\n";
}else{
// 無值,初始化
shm_put_var($shareMemory,SHARE_KEY,0);
echo "({$pid}) count: 0\n";
}
// 用完釋放
sem_release($signal);
$rand = rand(1,3);
sleep($rand);
}
}
function createProgress($callback){
$pid = pcntl_fork();
if ( $pid == -1) {
// 建立失敗
exit("fork progress error!\n");
} else if ($pid == 0) {
// 子程序執行程式
$pid = posix_getpid();
$callback();
exit("({$pid})child progress end!\n");
}else{
// 父程序執行程式
return $pid;
}
}
// 3個寫程序
for ($i = 0; $i < 3; $i ++ ) {
$pid = createProgress('producer');
$childList[$pid] = 1;
echo "create producer child progress: {$pid} \n";
}
// 等待所有子程序結束
while(!empty($childList)){
$childPid = pcntl_wait($status);
if ($childPid > 0){
unset($childList[$childPid]);
}
}
// 釋放共享記憶體與訊號量
shm_remove($shareMemory);
sem_remove($signal);
echo "({$parentPid})main progress end!\n";
3.訊號
訊號是一種系統呼叫。通常我們用的kill命令就是傳送某個訊號給某個程序的。具體有哪些訊號可以在liunx/mac中執行kill -l
檢視。下面這個例子中,父程序等待5秒鐘,向子程序傳送sigint訊號。子程序捕獲訊號,掉訊號處理函式處理。
$parentPid = posix_getpid();
echo "parent progress pid:{$parentPid}\n";
// 定義一個訊號處理函式
function sighandler($signo) {
$pid = posix_getpid();
echo "{$pid} progress,oh no ,I'm killed!\n";
exit(1);
}
$pid = pcntl_fork();
if ( $pid == -1) {
// 建立失敗
exit("fork progress error!\n");
} else if ($pid == 0) {
// 子程序執行程式
// 註冊訊號處理函式
declare(ticks=10);
pcntl_signal(SIGINT, "sighandler");
$pid = posix_getpid();
while(true){
echo "{$pid} child progress is running!\n";
sleep(1);
}
exit("({$pid})child progress end!\n");
}else{
// 父程序執行程式
$childList[$pid] = 1;
// 5秒後,父程序向子程序傳送sigint訊號.
sleep(5);
posix_kill($pid,SIGINT);
sleep(5);
}
echo "({$parentPid})main progress end!\n";
4.管道(有名管道)
管道是比較常用的多程序通訊手段,管道分為無名管道與有名管道,無名管道只能用於具有親緣關係的程序間通訊,而有名管道可以用於同一主機上任意程序。這裡只介紹有名管道。下面的例子,子程序寫入資料,父程序讀取資料。
// 定義管道路徑,與建立管道
$pipe_path = '/data/test.pipe';
if(!file_exists($pipe_path)){
if(!posix_mkfifo($pipe_path,0664)){
exit("create pipe error!");
}
}
$pid = pcntl_fork();
if($pid == 0){
// 子程序,向管道寫資料
$file = fopen($pipe_path,'w');
while (true){
fwrite($file,'hello world');
$rand = rand(1,3);
sleep($rand);
}
exit('child end!');
}else{
// 父程序,從管道讀資料
$file = fopen($pipe_path,'r');
while (true){
$rel = fread($file,20);
echo "{$rel}\n";
$rand = rand(1,2);
sleep($rand);
}
}
5.socket
socket即我們常說的套接字程式設計。這個待補充。
原文地址:https://www.jianshu.com/p/08bcf724196b