Home  >  Article  >  Backend Development  >  Use PHP to build your own stable and reliable daemon

Use PHP to build your own stable and reliable daemon

藏色散人
藏色散人forward
2019-07-15 15:24:293256browse

Build a stable and reliable PHP daemon

what

##● pcntl_fork

● Process/Process Group/Group Long process/session

●Standard input/output/error

why

how

You need to install the pcntl extension to use it

● Background running

if ($pid=pcntl_fork ()) exit (0);// is the parent process, end the parent process , the child process continues

●Leave the control terminal, log in to the session and process group

posix_setsid ();//The child process upgrades the group leader process, and leaves the original session/terminal

● Prohibit the process from reopening the control terminal

if ($pid=pcntl_fork ()) exit (0);//End the first child process and the second child process continues (the second child process is no longer a session Team leader)

● Close the open file descriptor

fclose (STDIN), fclose (STDOUT), fclose (STDERR) // Close standard input and output and error display.

● Change the current working directory

chdir("/")

● Reset the file creation mask

umask (0);//Prevent Inherit the mask left by the parent

Related recommendations: "

PHP Tutorial"

The above is the detailed content of Use PHP to build your own stable and reliable daemon. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete