PHP Multi-Process Small instance

Source: Internet
Author: User

<?php
$pid =pcntl_fork ();//fork is a child process that has been created, both the parent and child processes start down from the fork position, and the difference is that the fork return value is the child process number when the parent process executes, and the child process gets 0.
if ($pid ==-1) {
Die (' Create child process failed ');
Die (' failed ');
}elseif ($pid) {
Echo ' P ';
Pcntl_wait ($status);
$id =getmypid ();
echo "The parent process ID is {$id} and the child process ID is {$pid}";
echo "parentpid:{$id},childpid:{$pid}";
}elseif ($pid ==0) {
$id =getmypid ();//Returns the PID of the current process
echo "sub-process pid {$id}";
echo "childpid:{$id}";
Sleep (30);
}

?>

Above is a PHP multi-process Small instance I wrote, if you need to execute a lot of processes at the same time, in the parent process or child processes continue to use pcntl_fork (), and also do not forget to use the parent process pc_wait ($status), otherwise easily become a zombie process.

PHP Multi-Process Small instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.