How to use a process as a daemon in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:02:14
Original
815 people have browsed it

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...
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