PHP是用C编写的,所以它对系统底层API的操做与C很像,同大多数语言同样,PHP进程间通讯的方式有如下几种:消息队列,管道,共享内存,socket和信号。本文是对这几种通讯方式对整理:php
管道用于承载简称之间的通信数据。为了方便理解,能够将管道比做文件,进程A将数据写到管道P中,而后进程B从管道P中读取数据。php提供的管道操做API与操做文件的API基本同样,除了建立管道使用posix_mkfifo函数,读写等操做均与文件操做函数相同。固然,你能够直接使用文件模拟管道,可是那样没法使用管道的特性了。数据结构
经过管道通讯的大概思路是,首先建立一个管道,而后子进程向管道中写入信息,父进程从管道中读取信息,这样就能够作到父子进程直接实现通讯了。socket
<?php // 建立管道 $pipePath = "pipe"; if( !file_exists( $pipePath ) ){ if( !posix_mkfifo( $pipePath, 0666) ){ exit('make pipe false!' . PHP_EOL); } } // 建立进程,子进程写管道,父进程读管道 // 经过 pcntl_fork函数建立一个子进程。 // pcntl_fork 函数 很特殊,它调用一次拥有 多个返回值。 // 在父进程中:它返回 子进程的ID 这个值是 大于0 的。 // 在子进程中,它返回0。当返回 -1 时表示建立进程失败。 $pid = pcntl_fork(); if( $pid == 0 ){ // 子进程写管道 $file = fopen( $pipePath, 'w'); fwrite( $file, 'hello world'); sleep(1); exit; }else{ // 父进程读管道 $file = fopen( $pipePath, 'r'); // 设置成读取非阻塞 // 当读取是非阻塞的状况下,父进程进行读取信息的时候不会等待, // 管道中没有消息也会立马返回。 // stream_set_blocking( $file, False); echo fread( $file, 20) . PHP_EOL; pcntl_wait($status); // 回收子进程 }
消息队列是存放在内存中的一种队列数据结构。函数
<?php // 获取父进程id $parentPid = posix_getpid(); echo "parent progress pid:{$parentPid}\n"; $childList = array(); // 建立消息队列,定义消息类型 $id = ftok(__FILE__, 'm'); $msgQueue = msg_get_queue($id); const MSG_TYEP = 1; // 生产者 function producer() { global $msgQueue; $pid = posix_getpid(); $repeatNum = 5; for ($i = 0; $i <= $repeatNum; $i++) { $str = "({$pid}) progress create! {$i}"; msg_send($msgQueue, MSG_TYEP, $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_TYEP, $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 progresses error\n"); } elseif ($pid == 0) { // 子进程执行程序 $pid = posix_getpid(); $callback(); exit("({$pid})child progress end!\n"); } else { // 父进程 return $pid; } } for ($i = 0; $i < 3; $i++) { $pid = createProgress('producer'); $childList[$pid] = 1; echo "create producer progresses: {$pid}\n"; } for ($i = 0; $i < 2; $i++) { $pid = createProgress('consumer'); $childList[$pid] = 1; echo "create consumer progresses: {$pid}\n"; } while (!empty($childList)) { $childPid = pcntl_wait($status); if ($childPid > 0) { unset($childList[$childPid]); } } echo "({$parentPid})main progress end!\n";
运行结果:ui
create producer progresses: 21432 create producer progresses: 21433 create producer progresses: 21434 create consumer progresses: 21435 (21426) progress create! 2 | consumer(21435) destroy (21424) progress create! 1 | consumer(21436) destroy create consumer progresses: 21436 (21426) progress create! 3 | consumer(21436) destroy (21426) progress create! 4 | consumer(21435) destroy (21425) progress create! 3 | consumer(21436) destroy (21424) progress create! 2 | consumer(21435) destroy (21426) progress create! 5 | consumer(21435) destroy (21424) progress create! 3 | consumer(21436) destroy (21433)child progress end! (21425) progress create! 4 | consumer(21435) destroy (21424) progress create! 4 | consumer(21436) destroy (21434)child progress end! (21424) progress create! 5 | consumer(21435) destroy (21425) progress create! 5 | consumer(21436) destroy (21432)child progress end! (21435)child progress end! (21436)child progress end! (21431)main progress end!
<?php $parentPid = posix_getpid(); echo "parent progress pid:{$parentPid}\n"; // 建立共享内存,建立信号量,定义共享key // ftok(文件路径,资源标识符) 建立一个IPC通讯所需的id $shm_id = ftok(__FILE__, 'm'); $shm_id = ftok(__FILE__, 's'); // shm_attach(id) 建立或者打开一个共享内存 $shareMemory = shm_attach($shm_id); // 返回一个可用户访问系统信号量的id $signal = sem_get($shm_id); const SHARE_KEY = 1; // 生产者 function producer() { global $shareMemory; global $signal; $pid = posix_getpid(); $repeatNum = 5; for ($i = 1; $i <= $repeatNum; $i++) { // 得到信号量 - 阻塞进程,直到信号量被获取到[lock锁机制的关键] sem_acquire($signal); // 检查某个key是否存在与共享内存中 if (shm_has_var($shareMemory, SHARE_KEY)) { // 获取共享内存中的key的值 $count = shm_get_var($shareMemory, SHARE_KEY); $count ++; // 为共享内存中的key赋值 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); } } function createProgress($callback) { $pid = pcntl_fork(); if ($pid == -1) { // 建立失败 exit("fork progress error!\n"); } elseif ($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";
运行结果:
使用信号量来实现共享内存的锁机制code
parent progress pid:31720 create producer child progress: 31721 create producer child progress: 31722 (31721) count: 0 (31721) count: 1 (31721) count: 2 (31721) count: 3 (31721) count: 4 (31721) child progress end! create producer child progress: 31723 (31722) count: 5 (31722) count: 6 (31722) count: 7 (31722) count: 8 (31722) count: 9 (31722) child progress end! (31723) count: 10 (31723) count: 11 (31723) count: 12 (31723) count: 13 (31723) count: 14 (31723) child progress end! (31720) main progress end!
无锁状况队列
Warning: sem_release(): SysV semaphore 4357894312 (key 0x73048925) is not currently acquired in /Users/easyboom/www/example/信号量与共享内存.php on line 38