Multi-process application in PHP CLI mode

Source: Internet
Author: User
Tags exit php cli php code

PHP is often not suitable for resident shell processes, he does not have a dedicated GC routines, and there is no effective memory management path. So if you use PHP as a resident shell, you will often be abort and unhappy by memory exhaustion.

Moreover, if the input data is illegal, and the script is not detected, causing abort, it will also make you very unhappy.

That? What do we do?

Oh, don't worry, many processes to help you!

So, what is this for?

Advantages:

1. Using multiple processes, the kernel is responsible for reclaiming resources after the child process finishes

2. Using multiple processes, the exception of a subprocess exit does not cause the entire process thread to exit. The parent process also has the opportunity to rebuild the process.

3. A resident main process, which is only responsible for the distribution of tasks, has a clearer logic.

Then, how do you do it?

Next, we use the POSIX and PCNTL series functions provided by PHP to implement a PHP command parser that accepts user input and then executes the fork subprocess and is responsible for echoing the end state of the child process.

Code as follows, I added a note, if there is no understanding of the place, you can read the manual related functions, or reply to the message.

#!/bin/env PHP

/** A Example denoted muti-process application in PHP

* @filename fork.php

* @touch Date Wed June 2009 10:25:51 PM CST

* @author Laruence

* @license http://www.zend.com/license/3_0.txt PHP License 3.0

* @version 1.0.0

*/

/** Make sure that this function only runs in the shell.

If

(Substr (Php_sapi_name (), 0, 3)!== ' CLI ')

{

Die ("This programe can is run in CLI mode");

}

/** shutdown maximum Execution event limit, in CLI mode, this statement is not really necessary.

Set_time_limit (0);

$pid = Posix_getpid (); Get main Process ID

$user = Posix_getlogin (); Get user Name

Echo

<<

USAGE: [command | expression]

Input PHP code to execute by fork a new process

Input quit to exit

Shell Executor version 1.0.0 by laruence

EOD;

While

(true)

{

$prompt = "n{$user}$";

$input = ReadLine ($prompt);

Readline_add_history ($input);

If

($input = = ' quit ')

{

Break

}

Process_execute ($input. ';');

}

Exit (0);

function

Process_execute ($input)

{

$pid = Pcntl_fork (); To create a child process

If

($pid = = 0)

{//Child processes

$pid = Posix_getpid ();

Echo

"* Process {$pid} was created, and Executed:nn";

eval ($input); Parsing commands

Exit

}

Else

{//main process

$pid = pcntl_wait ($status, wuntraced); To get the end state of a child process

If

(pcntl_wifexited ($status))

{

Echo

"nn* Sub process: {$return [' pid ']} exited with {$status}";

}

}

}

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.