Home  >  Article  >  Backend Development  >  PHP uses process as daemon process, php process daemon_PHP tutorial

PHP uses process as daemon process, php process daemon_PHP tutorial

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

PHP uses the process as a daemon process, php process daemon

This article describes the example of PHP using the process as a daemon process. 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:

<&#63;php
  $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);
    }
  }
&#63;>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970856.htmlTechArticlePHP’s method of using a process as a daemon, php process daemon. This article describes how PHP uses a process as a daemon. method. Share it with everyone for your reference. The specific analysis is as follows:...
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