php多進程pcntl學習

來源:互聯網
上載者:User

標籤:

  pcntl在windows下無法使用,linux編譯php時加上參數--enable-pcntl 即可。第一次使用pcntl模組,遇到了一些坑也慢慢填上了,這裡簡單記錄下。

  1. 子進程之間變數無法共用。

  2. 子進程完成,記得加上exit(),子進程退出,否則會有坑。

 1 <?php 2 $pid_dir = __DIR__.‘/pid_files/‘; 3  4 for ($i=0;$i<3;$i++){ 5     $pid = pcntl_fork(); 6     if ($pid == -1){ 7         var_dump(‘fork failed‘); 8     } 9 10     if ($pid == 0){11         //子進程代碼 ,返回當前進程id12         $pid = posix_getgid();13         //返回當前父進程標識14         $ppid = posix_getppid();15         $r = rand(0,100);  //隨機數16         //建立一個檔案17         file_put_contents($pid_dir."fork_child_process_{$i}_{$ppid}_{$pid}_{$r}",$e);18         //注意這裡子進程完成,記得加上exit(),子進程退出,否則會有坑19         //參考:119000000350367120         exit();21     }22 }23 24 25 $pid = posix_getpid();26 $ppid = posix_getppid();27 $r = rand(0,100); //隨機數28 touch("$pid_dir/fork_process_pid_{$ppid}_{$pid}_$r");

 

  

  

php多進程pcntl學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.