国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

PHP基于進程控制函數(shù)實現(xiàn)多線程

瀏覽:131日期:2022-09-08 15:10:25

php有一組進程控制函數(shù)(編譯時需要?enable-pcntl與posix擴展),使得php能在nginx系統(tǒng)中實現(xiàn)跟c一樣的創(chuàng)建子進程、使用exec函數(shù)執(zhí)行程序、處理信號等功能。

CentOS 6 下yum安裝php的,默認是不安裝pcntl的,因此需要單獨編譯安裝,首先下載對應版本的php,解壓后

cd php-version/ext/pcntl phpize ./configure && make && make install cp /usr/lib/php/modules/pcntl.so /usr/lib64/php/modules/pcntl.so echo 'extension=pcntl.so' >> /etc/php.ini /etc/init.d/httpd restart

方便極了。

下面是示例代碼:

<?php header(’content-type:text/html;charset=utf-8’ ); // 必須加載擴展 if (!function_exists('pcntl_fork')) { die('pcntl extention is must !'); } //總進程的數(shù)量 $totals = 3; // 執(zhí)行的腳本數(shù)量 $cmdArr = array(); // 執(zhí)行的腳本數(shù)量的數(shù)組 for ($i = 0; $i < $totals; $i++) { $cmdArr[] = array('path' => __DIR__ . '/run.php', ’pid’ =>$i ,’total’ =>$totals); } /* 展開:$cmdArr Array ( [0] => Array ( [path] => /var/www/html/company/pcntl/run.php [pid] => 0 [total] => 3 ) [1] => Array ( [path] => /var/www/html/company/pcntl/run.php [pid] => 1 [total] => 3 ) [2] => Array ( [path] => /var/www/html/company/pcntl/run.php [pid] => 2 [total] => 3 ) ) */ pcntl_signal(SIGCHLD, SIG_IGN); //如果父進程不關心子進程什么時候結(jié)束,子進程結(jié)束后,內(nèi)核會回收。 foreach ($cmdArr as $cmd) { $pid = pcntl_fork(); //創(chuàng)建子進程 //父進程和子進程都會執(zhí)行下面代碼 if ($pid == -1) { //錯誤處理:創(chuàng)建子進程失敗時返回-1. die(’could not fork’); } else if ($pid) { //父進程會得到子進程號,所以這里是父進程執(zhí)行的邏輯 //如果不需要阻塞進程,而又想得到子進程的退出狀態(tài),則可以注釋掉pcntl_wait($status)語句,或?qū)懗桑? pcntl_wait($status,WNOHANG); //等待子進程中斷,防止子進程成為僵尸進程。 } else { //子進程得到的$pid為0, 所以這里是子進程執(zhí)行的邏輯。 $path = $cmd['path']; $pid = $cmd[’pid’] ; $total = $cmd[’total’] ; echo exec('/usr/bin/php {$path} {$pid} {$total}').'n'; exit(0) ; } } ?>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: PHP
相關文章:
主站蜘蛛池模板: 临海市| 民乐县| 攀枝花市| 肥乡县| 武城县| 呼伦贝尔市| 邯郸市| 太白县| 新龙县| 岢岚县| 自贡市| 巴东县| 噶尔县| 张家港市| 崇义县| 汉源县| 柘城县| 和田市| 西和县| 浠水县| 顺昌县| 手游| 黑水县| 晴隆县| 台北县| 鄂托克前旗| 陕西省| 勃利县| 沾化县| 海盐县| 哈尔滨市| 青铜峡市| 泾川县| 东阿县| 云阳县| 德江县| 肥西县| 荆门市| 大冶市| 江源县| 翁牛特旗|