当前位置:Gxlcms > PHP教程 > PHP进程间通信详解

PHP进程间通信详解

时间:2021-07-01 10:21:17 帮助过:13人阅读

进程是一个具有独立功能的程序关于某个数据集合的一次运行活动。换句话说就是,在系统调度多个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次,退出程序。

  1. $parentPid = posix_getpid();
  2. echo "parent progress pid:{$parentPid}\n";
  3. $childList = array();
  4. $pid = pcntl_fork();
  5. if ( $pid == -1) {
  6. // 创建失败
  7. exit("fork progress error!\n");
  8. } else if ($pid == 0) {
  9. // 子进程执行程序
  10. $pid = posix_getpid();
  11. $repeatNum = 5;
  12. for ( $i = 1; $i <= $repeatNum; $i++) {
  13. echo "({$pid})child progress is running! {$i} \n";
  14. $rand = rand(1,3);
  15. sleep($rand);
  16. }
  17. exit("({$pid})child progress end!\n");
  18. } else {
  19. // 父进程执行程序
  20. $childList[$pid] = 1;
  21. }
  22. // 等待子进程结束
  23. pcntl_wait($status);
  24. echo "({$parentPid})main progress end!";

完美,终于创建了一个子进程,一个父进程。完了么?没有,各个进程之间相互独立的,没有任何交集,使用范围严重受到现在。怎么办,哪就进程间通信(interprogress communication)呗。

四、进程间通信(IPC)

通常linux中的进程通信方式有:消息队列、信号量、共享内存、信号、管道、socket。

1.消息队列

消息队列是存放在内存中的一个队列。如下代码将创建3个生产者子进程,2个消费者子进程。这5个进程将通过消息队列通信。

  1. $parentPid = posix_getpid();
  2. echo "parent progress pid:{$parentPid}\n";$childList = array();
  3. // 创建消息队列,以及定义消息类型(类似于数据库中的库)
  4. $id = ftok(__FILE__,'m');
  5. $msgQueue = msg_get_queue($id);
  6. const MSG_TYPE = 1;
  7. // 生产者
  8. function producer(){
  9. global $msgQueue;
  10. $pid = posix_getpid();
  11. $repeatNum = 5;
  12. for ( $i = 1; $i <= $repeatNum; $i++) {
  13. $str = "({$pid})progress create! {$i}";
  14. msg_send($msgQueue,MSG_TYPE,$str);
  15. $rand = rand(1,3);
  16. sleep($rand);
  17. }
  18. }
  19. // 消费者
  20. function consumer(){
  21. global $msgQueue;
  22. $pid = posix_getpid();
  23. $repeatNum = 6;
  24. for ( $i = 1; $i <= $repeatNum; $i++) {
  25. $rel = msg_receive($msgQueue,MSG_TYPE,$msgType,1024,$message);
  26. echo "{$message} | consumer({$pid}) destroy \n";
  27. $rand = rand(1,3);
  28. sleep($rand);
  29. }
  30. }
  31. function createProgress($callback){
  32. $pid = pcntl_fork();
  33. if ( $pid == -1) {
  34. // 创建失败
  35. exit("fork progress error!\n");
  36. } else if ($pid == 0) {
  37. // 子进程执行程序
  38. $pid = posix_getpid();
  39. $callback();
  40. exit("({$pid})child progress end!\n");
  41. }else{
  42. // 父进程执行程序
  43. return $pid;
  44. }
  45. }
  46. // 3个写进程
  47. for ($i = 0; $i < 3; $i ++ ) {
  48. $pid = createProgress('producer');
  49. $childList[$pid] = 1;
  50. echo "create producer child progress: {$pid} \n";
  51. }
  52. // 2个写进程
  53. for ($i = 0; $i < 2; $i ++ ) {
  54. $pid = createProgress('consumer');
  55. $childList[$pid] = 1;
  56. echo "create consumer child progress: {$pid} \n";
  57. }
  58. // 等待所有子进程结束
  59. while(!empty($childList)){
  60. $childPid = pcntl_wait($status);
  61. if ($childPid > 0){
  62. unset($childList[$childPid]);
  63. }
  64. }
  65. echo "({$parentPid})main progress end!\n";

由于消息队列去数据是,只有一个进程能去到,所以不需要额外的锁或信号量。

2. 信号量与共享内存

信号量:是系统提供的一种原子操作,一个信号量,同时只有你个进程能操作。一个进程获得了某个信号量,就必须被该进程释放掉。

共享内存:是系统在内存中开辟的一块公共的内存区域,任何一个进程都可以访问,在同一时刻,可以有多个进程访问该区域,为了保证数据的一致性,需要对该内存区域加锁或信号量。

以下,创建多个进程修改内存中的同一个值。

  1. $parentPid = posix_getpid();
  2. echo "parent progress pid:{$parentPid}\n";
  3. $childList = array();
  4. // 创建共享内存,创建信号量,定义共享key
  5. $shm_id = ftok(__FILE__,'m');
  6. $sem_id = ftok(__FILE__,'s');
  7. $shareMemory = shm_attach($shm_id);
  8. $signal = sem_get($sem_id);
  9. const SHARE_KEY = 1;
  10. // 生产者
  11. function producer(){
  12. global $shareMemory;
  13. global $signal;
  14. $pid = posix_getpid();
  15. $repeatNum = 5;
  16. for ( $i = 1; $i <= $repeatNum; $i++) {
  17. // 获得信号量
  18. sem_acquire($signal);
  19. if (shm_has_var($shareMemory,SHARE_KEY)){
  20. // 有值,加一
  21. $count = shm_get_var($shareMemory,SHARE_KEY);
  22. $count ++;
  23. shm_put_var($shareMemory,SHARE_KEY,$count);
  24. echo "({$pid}) count: {$count}\n";
  25. }else{
  26. // 无值,初始化
  27. shm_put_var($shareMemory,SHARE_KEY,0);
  28. echo "({$pid}) count: 0\n";
  29. }
  30. // 用完释放
  31. sem_release($signal);
  32. $rand = rand(1,3);
  33. sleep($rand);
  34. }
  35. }
  36. function createProgress($callback){
  37. $pid = pcntl_fork();
  38. if ( $pid == -1) {
  39. // 创建失败
  40. exit("fork progress error!\n");
  41. } else if ($pid == 0) {
  42. // 子进程执行程序
  43. $pid = posix_getpid();
  44. $callback();
  45. exit("({$pid})child progress end!\n");
  46. }else{
  47. // 父进程执行程序
  48. return $pid;
  49. }
  50. }
  51. // 3个写进程
  52. for ($i = 0; $i < 3; $i ++ ) {
  53. $pid = createProgress('producer');
  54. $childList[$pid] = 1;
  55. echo "create producer child progress: {$pid} \n";
  56. }
  57. // 等待所有子进程结束
  58. while(!empty($childList)){
  59. $childPid = pcntl_wait($status);
  60. if ($childPid > 0){
  61. unset($childList[$childPid]);
  62. }
  63. }
  64. // 释放共享内存与信号量
  65. shm_remove($shareMemory);
  66. sem_remove($signal);
  67. echo "({$parentPid})main progress end!\n";

3.信号

信号是一种系统调用。通常我们用的kill命令就是发送某个信号给某个进程的。具体有哪些信号可以在liunx/mac中运行kill -l查看。下面这个例子中,父进程等待5秒钟,向子进程发送sigint信号。子进程捕获信号,掉信号处理函数处理。

  1. $parentPid = posix_getpid();
  2. echo "parent progress pid:{$parentPid}\n";
  3. // 定义一个信号处理函数
  4. function sighandler($signo) {
  5. $pid = posix_getpid();
  6. echo "{$pid} progress,oh no ,I'm killed!\n";
  7. exit(1);
  8. }
  9. $pid = pcntl_fork();
  10. if ( $pid == -1) {
  11. // 创建失败
  12. exit("fork progress error!\n");
  13. } else if ($pid == 0) {
  14. // 子进程执行程序
  15. // 注册信号处理函数
  16. declare(ticks=10);
  17. pcntl_signal(SIGINT, "sighandler");
  18. $pid = posix_getpid();
  19. while(true){
  20. echo "{$pid} child progress is running!\n";
  21. sleep(1);
  22. }
  23. exit("({$pid})child progress end!\n");
  24. }else{
  25. // 父进程执行程序
  26. $childList[$pid] = 1;
  27. // 5秒后,父进程向子进程发送sigint信号.
  28. sleep(5);
  29. posix_kill($pid,SIGINT);
  30. sleep(5);
  31. }
  32. echo "({$parentPid})main progress end!\n";

4.管道(有名管道)

管道是比较常用的多进程通信手段,管道分为无名管道与有名管道,无名管道只能用于具有亲缘关系的进程间通信,而有名管道可以用于同一主机上任意进程。这里只介绍有名管道。下面的例子,子进程写入数据,父进程读取数据。

  1. // 定义管道路径,与创建管道
  2. $pipe_path = '/data/test.pipe';
  3. if(!file_exists($pipe_path)){
  4. if(!posix_mkfifo($pipe_path,0664)){
  5. exit("create pipe error!");
  6. }
  7. }
  8. $pid = pcntl_fork();
  9. if($pid == 0){
  10. // 子进程,向管道写数据
  11. $file = fopen($pipe_path,'w');
  12. while (true){
  13. fwrite($file,'hello world');
  14. $rand = rand(1,3);
  15. sleep($rand);
  16. }
  17. exit('child end!');
  18. }else{
  19. // 父进程,从管道读数据
  20. $file = fopen($pipe_path,'r');
  21. while (true){
  22. $rel = fread($file,20);
  23. echo "{$rel}\n";
  24. $rand = rand(1,2);
  25. sleep($rand);
  26. }
  27. }

相关推荐:

PHP进程锁如何实现

总结关于PHP进程通信注意点

PHP进程通信基础之信号量与共享内存通信

以上就是PHP进程间通信详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行