Home >Backend Development >PHP Problem >How to implement multiple processes and close processes in php
php method to close a process: first create a PHP sample file; then close the specified process through the "exec("kill -9 30699");" method.
The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
1. PHP implements multi-process
PHP has a pcntl_fork function that can implement multi-process, but the pcntl extension needs to be loaded. And this extension can only be compiled under Linux.
First code:
<?php $arr = ['30000000','500000000',['7000000000','8000000']]; foreach($arr as $key=>$item){ $pid[$key] = pcntl_fork(); if ($pid[$key] == -1) { die('could not fork'); } else if (!$pid[$key]) { if(is_array($item)){ foreach($item as $k=>$value) { $pid[$k] = pcntl_fork(); if(!$pid[$k]){ for($j=0;$j<$value;$j++){ $con1 = file_get_contents('./'.$value.'.txt'); file_put_contents('./'.$value.'.txt',$con1.'#'.$j); } exit; } } }else{ for($i=0;$i<$item;$i++){ $con = file_get_contents('./'.$item.'.txt'); file_put_contents('./'.$item.'.txt',$con.'#'.$i); } } exit; } } 把这个写在test.php文件里。 在Linux中执行: php -f test.php 查询进程:ps -ef | gerp test 就会查到4个进程
Close the process: kill -9 pid [Recommended: "PHP Video tutorial》】
$pid[$k] = pcntl_fork();//这里的$pid[$k] 就是子进程的进程ID
<?php exec("kill -9 30699");
写在test2.php 里在Linux中执行: php -f test.php 查询进程就会发现 30699的进程被关闭了 参考url:https://zhidao.baidu.com/question/395877542327855005.html 注意:php实现多进程或关闭进程,都需要Linux用户的权限,如果是用web(浏览器上),那需要给web端执行的权限。我这里用root执行Linux语句,所以有权限。
The above is the detailed content of How to implement multiple processes and close processes in php. For more information, please follow other related articles on the PHP Chinese website!