In addition, if the input data is invalid and the script does not detect it, resulting in abort, it will make you very unhappy.
So? What should we do?
Don't worry. multi-process will help you!
So, why?
Advantages:
1. When a multi-process is used, the kernel will be responsible for recycling resources after the sub-process ends.
2. If a multi-process is used, abnormal exit of the sub-process will not cause the thread of the whole process to exit. The parent process also has the opportunity to rebuild the process.
3. A resident process is only responsible for task distribution, and the logic is clearer.
Then, how to do it?
Next, we use POSIX and pcntl series functions provided by PHP to implement a PHP command parser. The main process is responsible for receiving user input and then Fork sub-process execution, and is responsible for returning the end status of the sub-process.
CodeAs shown below, I added a comment. If you do not know anything, you can refer to the related functions of the manual or reply to the message. Copy codeThe Code is as follows :#! /Bin/ENV PHP
<? PHP
/** A example denoted muti-process application in PHP
* @ Filename fork. php
* @ Touch date wed 10 Jun 2009 10:25:51 pm CST
* @ Author laruence <laruence@baidu.com>
* @ License http://www.zend.com/license/3_0.txt PHP license 3.0
* @ Version 1.0.0
*/
/** Make sure this function can only run in Shell */
If
(Substr (php_sapi_name (), 0, 3 )! = 'Cli ')
{
Die ("this programe can only be run in CLI mode ");
}
/** Disable the maximum execution event limit. In CLI mode, this statement is not necessary */
Set_time_limit (0 );
$ Pid = posix_getpid (); // obtain the master process ID
$ User = posix_getlogin (); // obtain the user name
Echo
<EOD
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 (); // create a sub-process
If
($ Pid = 0)
{// Sub-process
$ Pid = posix_getpid ();
Echo
"* Process {$ PID} was created, and executed: \ n ";
Eval ($ input); // resolution command
Exit;
}
Else
{// MAIN PROCESS
$ Pid = pcntl_wait ($ status, wuntraced); // get the sub-process end status
If
(Pcntl_wifexited ($ status ))
{
Echo
"\ N * sub process: {$ return ['pid ']} exited with {$ status }";
}
}
}
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