PHP _php techniques as a way to daemon processes

Source: Internet
Author: User

The example in this article describes how PHP processes a process as a daemon. Share to everyone for your reference. The specific analysis is as follows:

Usage of posix_setsid () in PHP

The document explanation is "make the current process a session leader"

Reference Document: Http://linux.die.net/man/2/setsid

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

So the way to make a process a daemon is to:

1 Fork out a child process

2 in the subprocess Posix_setsid ()

3 Exiting the parent process

There is an example in the document:

<?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 <= $i + +) {//do Something for 5 minutes sleep
      (5);
    }
? >

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.