Home >Backend Development >PHP Tutorial >How to use a process as a daemon in PHP_PHP Tutorial

How to use a process as a daemon in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:02:14845browse

How PHP uses processes as daemons

This article mainly introduces the method of PHP using processes as daemons. It analyzes the implementation techniques of daemons in PHP with examples. Be sure to refer to the reference value. Friends who need it can refer to it

The examples in this article describe how PHP uses processes as daemons. Share it with everyone for your reference. The specific analysis is as follows:

Usage of posix_setsid() in php

The documentation explanation is โ€œMake the current process a session leaderโ€

Reference documentation: http://linux.die.net/man/2/setsid

It means that the process that calls this function between a process group (parent process and child process) will be elected as the leader of the process group

So the way to make a process a daemon is:

1 fork out a child process

2 In the child process posix_setsid()

3 Exit the parent process

There is an example in the documentation:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$pid = pcntl_fork(); // fork

if ($pid < 0)

exit;

else if ($pid) // parent

exit;

else { // child

$sid = posix_setsid();

if ($sid < 0)

exit;

for($i = 0; $i <= 60; $i ) {//do something for 5 minutes

sleep(5);

}

}

?>

1 2

3

4 5

67 8 9 10 11 12
13
14
15
<๐ŸŽœ>$pid = pcntl_fork(); // fork<๐ŸŽœ> <๐ŸŽœ>if ($pid < 0)<๐ŸŽœ> <๐ŸŽœ>exit;<๐ŸŽœ> <๐ŸŽœ>else if ($pid) // parent<๐ŸŽœ> <๐ŸŽœ>exit;<๐ŸŽœ> <๐ŸŽœ>else { // child<๐ŸŽœ> <๐ŸŽœ>$sid = posix_setsid();<๐ŸŽœ> <๐ŸŽœ>if ($sid < 0)<๐ŸŽœ> <๐ŸŽœ>exit;<๐ŸŽœ> <๐ŸŽœ>for($i = 0; $i <= 60; $i ) {//do something for 5 minutes<๐ŸŽœ> <๐ŸŽœ>sleep(5);<๐ŸŽœ> <๐ŸŽœ>}<๐ŸŽœ> <๐ŸŽœ>}<๐ŸŽœ> <๐ŸŽœ>?>
I hope this article will be helpful to everyoneโ€™s PHP programming design. http://www.bkjia.com/PHPjc/970756.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970756.htmlTechArticleHow PHP uses a process as a daemon process This article mainly introduces how PHP uses a process as a daemon process, with examples Analyzed the implementation skills of daemon process in php, which has certain reference value...
Statement๏ผš
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