Description of the PHP multi-Process Execution task

Source: Internet
Author: User
Tags php error signal handler
This article mainly introduces the PHP multi-process implementation of the task description, has a certain reference value, now share to everyone, the need for friends can refer to

Demand

Executes concurrently on a specified number of tasks and controls the number of processes

Processes & Threads:

Processes are independent of each other and do not affect.

code example:

<?php$task = 0; Task Id$tasknum = 10; Total Tasks $processnumlimit = 2;   Total child process limit while (true) {//generates branch $PROCESSID = Pcntl_fork (); Create Child process failed if ($processid = =-1) {echo "Create process error!       \ n ";   Exit (1);       }//Master process, get child process PID ElseIf ($PROCESSID) {$task + +;//Next Task $currentProcessid = Posix_getpid ();//ID of the current process $parentProcessid = Posix_getppid (); The ID of the parent process $phpProcessid = Getmypid (); The ID of the current PHP process echoes "task:", $task, "\tprocessid:", $processid, "\tcurrentprocessid:", $currentProcessid, "\       Tparentprocessid: ", $parentProcessid," \tphpprocessid: ", $phpProcessid," \ n "; Control the number of processes if ($task >= $processNumLimit) {echo "Wait CHL start!           \ n "; $exitid = pcntl_wait ($status); Wait to exit echo "Wait CHL end!       Extid: ", $exitid," \tstatus: ", $status," \ n "; }//Task total Control if ($task >= $taskNum) {echo "Tasknum enough!           \ n ";       Break      }}//processid=0 for the newly created process else{//simulate different execution duration for different tasks $sleep = rand (1, 5); $currentProcessid = Posix_getpid (); The ID of the current process $parentProcessid = Posix_getppid (); The ID of the parent process $phpProcessid = Getmypid (); The ID of the current PHP process echoes "task:", $task, "\tprocessid:", $processid, "\tcurrentprocessid:", $currentProcessid, "\       Tparentprocessid: ", $parentProcessid," \tphpprocessid: ", $phpProcessid," \tsleep: ", $sleep," \tbegin!\n ";       Sleep ($sleep); echo "Task:", $task, "\tprocessid:", $processid, "\tcurrentprocessid:", $currentProcessid, "\tparentprocessid:", $              Parentprocessid, "\tphpprocessid:", $phpProcessid, "\tsleep:", $sleep, "\tend!\n"; Exit (0); The child process exits after execution, preventing the child process from entering the loop}}

Result of execution:

Task:1 processid:32225 currentprocessid:32224 parentprocessid:15053 phpprocessid:32224task:0 processid:0 currentprocessid:32225 parentprocessid:32224 phpprocessid:32225 sleep:5 begin!task:2 processid:32226 cu rrentprocessid:32224 parentprocessid:15053 phpprocessid:32224wait CHL start!    Task:1 processid:0 currentprocessid:32226 parentprocessid:32224 phpprocessid:32226 sleep:2 begin!task:1 processid:0 currentprocessid:32226 parentprocessid:32224 phpprocessid:32226 sleep:2 end!wait CHL end! extid:32226 status:0task:3 processid:32228 currentprocessid:32224 parentprocessid:15053 phpProcessid:32224w AIT CHL start!    Task:2 processid:0 currentprocessid:32228 parentprocessid:32224 phpprocessid:32228 sleep:1 begin!task:2 processid:0 currentprocessid:32228 parentprocessid:32224 phpprocessid:32228 sleep:1 end!wait CHL end! extid:32228 Status:0task:4 processid:32229 Currentprocessid:32224 parentprocessid:15053 phpprocessid:32224wait CHL start!    Task:3 processid:0 currentprocessid:32229 parentprocessid:32224 phpprocessid:32229 sleep:2 begin!task:0 processid:0 currentprocessid:32225 parentprocessid:32224 phpprocessid:32225 sleep:5 end!wait CHL end! extid:32225 status:0task:5 processid:32270 currentprocessid:32224 parentprocessid:15053 phpProcessid:32224w AIT CHL start!    Task:4 processid:0 currentprocessid:32270 parentprocessid:32224 phpprocessid:32270 sleep:1 begin!task:3 processid:0 currentprocessid:32229 parentprocessid:32224 phpprocessid:32229 sleep:2 end!wait CHL end! extid:32229 status:0task:6 processid:32271 currentprocessid:32224 parentprocessid:15053 phpProcessid:32224w AIT CHL start!    Task:5 processid:0 currentprocessid:32271 parentprocessid:32224 phpprocessid:32271 sleep:4 begin!task:4    processid:0 currentprocessid:32270parentprocessid:32224 phpprocessid:32270 sleep:1 end!wait CHL end! extid:32270 status:0task:7 processid:32273 currentprocessid:32224 parentprocessid:15053 phpProcessid:32224w AIT CHL start!    Task:6 processid:0 currentprocessid:32273 parentprocessid:32224 phpprocessid:32273 sleep:1 begin!task:6 processid:0 currentprocessid:32273 parentprocessid:32224 phpprocessid:32273 sleep:1 end!wait CHL end! extid:32273 status:0task:8 processid:32274 currentprocessid:32224 parentprocessid:15053 phpProcessid:32224w AIT CHL start!    Task:7 processid:0 currentprocessid:32274 parentprocessid:32224 phpprocessid:32274 sleep:2 begin!task:5 processid:0 currentprocessid:32271 parentprocessid:32224 phpprocessid:32271 sleep:4 end!task:7 proces sid:0 currentprocessid:32274 parentprocessid:32224 phpprocessid:32274 sleep:2 end!wait CHL end! extid:32274 Status:0task:9 processid:32277 Currentprocessid:32224 parentprocessid:15053 phpprocessid:32224wait CHL start! Wait CHL end! extid:32271 status:0task:8 processid:0 currentprocessid:32277 parentprocessid:32224 phpProcessid:32277 s Leep:2 begin!task:10 processid:32278 currentprocessid:32224 parentprocessid:15053 phpProcessid:32224wait Ch L start!    Task:9 processid:0 currentprocessid:32278 parentprocessid:32224 phpprocessid:32278 sleep:2 begin!task:8 processid:0 currentprocessid:32277 parentprocessid:32224 phpprocessid:32277 sleep:2 end!task:9 proces sid:0 currentprocessid:32278 parentprocessid:32224 phpprocessid:32278 sleep:2 end!wait CHL end! extid:32277 Status:0tasknum Enough

Perform analysis:

    1. After pcntl_fork the child process is created, the child process executes the function code, and the parent process continues to increment the task ID, creating the next task process;

    2. By pcntl_wait waiting for the execution of the child process to exit, and then create a new process to achieve the total number of processes control;

    3. After the main process has finished creating, exit the loop by break. A child process may continue to execute at this time.

Related functions:

Pcntl_fork

produces a branch (child process) at the current position of the current process. On success, the PID of the resulting child process is returned within the parent process execution thread, returning 0 within the child process execution thread. On failure, the parent process context returns-1, the child process is not created, and a PHP error is raised.

Pcntl_wait

the child process state that waits or returns fork. The wait function shaves the execution of the current process until a child process exits or receives a signal that requires an interrupt to the current process or calls a signal handler function. If a child process has exited (commonly known as a zombie process) when calling this function, this function returns immediately. All system resources used by the child process will be freed.

Other:

1. Problems with the zombie process and the orphan process

Some zombie processes are pcntl_wait to get state and release resources, and the process that is not taken over becomes an orphan process after the main process exits. The orphan process was adopted by the INIT process, completing the Wait state acquisition and resource release.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.