当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP进程间通信的几种方法详解

发布:smiling 来源: PHP粉丝网  添加日期:2023-09-16 16:24:08 浏览: 评论:0 

PHP是用C编写的,因此它对系统底层API的操作与C很像,同大多数语言一样,PHP进程间通信的方式有以下几种:消息队列,管道,共享内存,socket和信号,本文是对这几种通信方式对整理。

管道通信PIPE

管道用于承载简称之间的通讯数据。为了方便理解,可以将管道比作文件,进程A将数据写到管道P中,然后进程B从管道P中读取数据。php提供的管道操作API与操作文件的API基本一样,除了创建管道使用posix_mkfifo函数,读写等操作均与文件操作函数相同。当然,你可以直接使用文件模拟管道,但是那样无法使用管道的特性了。

通过管道通信的大概思路是,首先创建一个管道,然后子进程向管道中写入信息,父进程从管道中读取信息,这样就可以做到父子进程直接实现通信了。

  1. <?php 
  2. // 创建管道 
  3. $pipePath = "pipe"; 
  4. if( !file_exists( $pipePath ) ){ 
  5.     if( !posix_mkfifo( $pipePath, 0666) ){ 
  6.         exit('make pipe false!' . PHP_EOL); 
  7.     } 
  8. } 
  9. // 创建进程,子进程写管道,父进程读管道 
  10. // 通过 pcntl_fork函数创建一个子进程。 
  11. // pcntl_fork 函数 很特殊,它调用一次拥有 多个返回值。 
  12. // 在父进程中:它返回 子进程的ID 这个值是 大于0 的。 
  13. // 在子进程中,它返回0。当返回 -1 时表示创建进程失败。 
  14. $pid = pcntl_fork(); 
  15. if( $pid == 0 ){ 
  16.     // 子进程写管道 
  17.     $file = fopen( $pipePath, 'w'); 
  18.     fwrite( $file, 'hello world'); 
  19.     sleep(1); 
  20.     exit; 
  21. }else{ 
  22.     // 父进程读管道 
  23.     $file = fopen( $pipePath, 'r'); 
  24.     // 设置成读取非阻塞 
  25.     // 当读取是非阻塞的情况下,父进程进行读取信息的时候不会等待, 
  26.     // 管道中没有消息也会立马返回。 
  27.     // stream_set_blocking( $file, False);  
  28.     echo fread( $file, 20) . PHP_EOL; 
  29.     pcntl_wait($status); // 回收子进程 
  30. } 

消息队列

消息队列是存放在内存中的一种队列数据结构。

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

运行结果:

  1. create producer progresses: 21432 
  2.  
  3. create producer progresses: 21433 
  4. create producer progresses: 21434 
  5. create consumer progresses: 21435 
  6. (21426) progress create! 2 | consumer(21435) destroy 
  7. (21424) progress create! 1 | consumer(21436) destroy 
  8. create consumer progresses: 21436 
  9. (21426) progress create! 3 | consumer(21436) destroy 
  10. (21426) progress create! 4 | consumer(21435) destroy 
  11. (21425) progress create! 3 | consumer(21436) destroy 
  12. (21424) progress create! 2 | consumer(21435) destroy 
  13. (21426) progress create! 5 | consumer(21435) destroy 
  14. (21424) progress create! 3 | consumer(21436) destroy 
  15. (21433)child progress end! 
  16. (21425) progress create! 4 | consumer(21435) destroy 
  17. (21424) progress create! 4 | consumer(21436) destroy 
  18. (21434)child progress end! 
  19. (21424) progress create! 5 | consumer(21435) destroy 
  20. (21425) progress create! 5 | consumer(21436) destroy 
  21. (21432)child progress end! 
  22. (21435)child progress end! 
  23. (21436)child progress end! 
  24. (21431)main progress end! 

信号量与共享内存

  1. <?php 
  2. $parentPid = posix_getpid(); 
  3. echo "parent progress pid:{$parentPid}\n"; 
  4. // 创建共享内存,创建信号量,定义共享key 
  5. // ftok(文件路径,资源标识符) 创建一个IPC通信所需的id 
  6. $shm_id = ftok(__FILE__, 'm'); 
  7. $shm_id = ftok(__FILE__, 's'); 
  8. // shm_attach(id) 创建或者打开一个共享内存 
  9. $shareMemory = shm_attach($shm_id); 
  10. // 返回一个可用户访问系统信号量的id 
  11. $signal = sem_get($shm_id); 
  12. const SHARE_KEY = 1; 
  13. // 生产者 
  14. function producer() { 
  15.     global $shareMemory; 
  16.     global $signal; 
  17.     $pid = posix_getpid(); 
  18.     $repeatNum = 5; 
  19.     for ($i = 1; $i <= $repeatNum; $i++) { 
  20.         // 获得信号量 - 阻塞进程,直到信号量被获取到[lock锁机制的关键] 
  21.         sem_acquire($signal); 
  22.         // 检查某个key是否存在与共享内存中 
  23.         if (shm_has_var($shareMemory, SHARE_KEY)) { 
  24.             // 获取共享内存中的key的值 
  25.             $count = shm_get_var($shareMemory, SHARE_KEY); 
  26.             $count ++; 
  27.             // 为共享内存中的key赋值 
  28.             shm_put_var($shareMemory, SHARE_KEY, $count); 
  29.             echo "({$pid}) count: {$count}\n"; 
  30.         } else { 
  31.             // 初始化 
  32.             shm_put_var($shareMemory, SHARE_KEY, 0); 
  33.             echo "({$pid}) count: 0\n"; 
  34.         } 
  35.         // 释放 
  36.         sem_release($signal); 
  37.     } 
  38. } 
  39. function createProgress($callback) { 
  40.     $pid = pcntl_fork(); 
  41.     if ($pid == -1) { 
  42.         // 创建失败 
  43.         exit("fork progress error!\n"); 
  44.     } elseif ($pid == 0) { 
  45.         // 子进程 
  46.         $pid = posix_getpid(); 
  47.         $callback(); 
  48.         exit("({$pid}) child progress end!\n"); 
  49.     } else { 
  50.         // 父进程 
  51.         return $pid; 
  52.     } 
  53. } 
  54. // 3个写进程 
  55. for ($i = 0; $i < 3; $i ++) { 
  56.     $pid = createProgress('producer'); 
  57.     $childList[$pid] = 1; 
  58.     echo "create producer child progress: {$pid} \n"; 
  59. } 
  60. // 等待所有子进程 
  61. while (!emptyempty($childList)) { 
  62.     $childPid = pcntl_wait($status); 
  63.     if ($childPid > 0) { 
  64.         unset($childList[$childPid]); 
  65.     } 
  66. } 
  67. // 释放共享内存与信号量 
  68. shm_remove($shareMemory); 
  69. sem_remove($signal); 
  70. echo "({$parentPid}) main progress end!\n"; 

运行结果:

使用信号量来实现共享内存的锁机制

  1. parent progress pid:31720 
  2. create producer child progress: 31721  
  3. create producer child progress: 31722  
  4. (31721) count: 0 
  5. (31721) count: 1 
  6. (31721) count: 2 
  7. (31721) count: 3 
  8. (31721) count: 4 
  9. (31721) child progress end! 
  10. create producer child progress: 31723  
  11. (31722) count: 5 
  12. (31722) count: 6 
  13. (31722) count: 7 
  14. (31722) count: 8 
  15. (31722) count: 9 
  16. (31722) child progress end! 
  17. (31723) count: 10 
  18. (31723) count: 11 
  19. (31723) count: 12 
  20. (31723) count: 13 
  21. (31723) count: 14 
  22. (31723) child progress end! 
  23. (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

Tags: PHP进程间通信

分享到: