PHP multi-process solves the problem of code resident in memory

WBOY
Release: 2016-07-25 08:58:33
Original
1951 people have browsed it
  1. #!/bin/env php
  2. /**A example denoted muti-process application in php
  3. * @filename fork.php
  4. * @edit bbs.it-home.org
  5. * @version 1.0.0
  6. */
  7. /**Make sure this function can only be run in SHELL*/
  8. if
  9. (substr(php_sapi_name(), 0, 3) ! == 'cli')
  10. {
  11. die("This Programe can only be run in CLI mode");
  12. }
  13. /**Turn off the maximum execution event limit. In CLI mode, this statement is actually unnecessary.*/
  14. set_time_limit(0);
  15. $pid = posix_getpid(); / /Get the main process ID
  16. $user = posix_getlogin(); //Get the user name
  17. echo
  18. <<USAGE: [command | expression]
  19. input php code to execute by fork a new process
  20. input quit to exit
  21. Shell Executor version 1.0.0 by laruence
  22. EOD;
  23. while
  24. (true)
  25. {
  26. $prompt = "n{$user}$ ";
  27. $input = readline($prompt);
  28. readline_add_history($input );
  29. if
  30. ($input == 'quit')
  31. {
  32. break;
  33. }
  34. process_execute($input . ';');
  35. }
  36. exit(0);
  37. function
  38. process_execute($input)
  39. {
  40. $pid = pcntl_fork(); //Create child process
  41. if
  42. ($pid == 0)
  43. {//Subprocess
  44. $pid = posix_getpid();
  45. echo
  46. "* Process {$pid} was created, and Executed:nn";
  47. eval($input); //Parse the command
  48. exit;
  49. }
  50. else
  51. {//Main process
  52. $pid = pcntl_wait($status, WUNTRACED); //Get the end status of the child process
  53. if
  54. (pcntl_wifexited($status))
  55. {
  56. echo
  57. "nn* Sub process: {$return['pid']} exited with {$status}";
  58. }
  59. }
  60. }
  61. ?>
Copy the code

Please pay attention to the comments in the above code, which can help everyone understand and are also the essence of the code.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!