Home Backend Development PHP Tutorial workerman notes-php creates daemon process

workerman notes-php creates daemon process

Jul 29, 2016 am 08:51 AM
exception fork pid posix

data-id="1190000005056078" data-license="sa">

Use function

<code>pcntl_fork();//创建子进程
posix_setsid();//设置当前进程为进程组长

posix_getpid();//获取进程id</code>
Copy after login

Example

workerman Lieutenant General process

<code>/**
 * Run as deamon mode.
 *
 * @throws Exception
 */
protected static function daemonize()
{
    if (!self::$daemonize) {
        return;
    }
    /**
     * 重设文件权限掩码
     * 子进程从父进程继承了文件权限
     * 若子进程不涉及到文件创建,可取消
     */
    umask(0);
    $pid = pcntl_fork();//创建子进程
    if (-1 === $pid) {
        throw new Exception('fork fail');
    } elseif ($pid > 0) {
        exit(0); //父进程退出
    }
    /**
     * 更改子进程为进程组长
     * 使子进程摆脱父进程控制
     */
    if (-1 === posix_setsid()) {
        throw new Exception("setsid fail");
    }
    // Fork again avoid SVR4 system regain the control of terminal.
    $pid = pcntl_fork();
    if (-1 === $pid) {
        throw new Exception("fork fail");
    } elseif (0 !== $pid) {
        exit(0);
    }
}</code>
Copy after login

Other instructions

Basic concept

Daemon process: background service process in Linux. It is a long-lived process that is usually independent of the controlling terminal and periodically performs some task or waits to handle certain events that occur. Daemons are often started when the system is booted and terminated when the system is shut down.

Process group: It is a collection of one or more processes. A process group is uniquely identified by a process group ID. In addition to the process number (PID), the process group ID is also a necessary attribute of a process. Each process group has a leader process, and the process number of the leader process is equal to the process group ID. And the process group ID will not be affected by the exit of the group leader process.

Session cycle: A session is a collection of one or more process groups. Usually, a session starts when the user logs in and ends when the user logs out. During this period, all processes run by the user belong to this session.

Create process

  1. fork the child process, the parent process exits

  2. Change the child process to the team leader process

  3. Change the current directory to the root directory (chdir())

  4. Reset file permission mask Code

  5. Close the file descriptor

  6. The daemon exits and handles the SIGCHLD signal

Signal processing

//TODO

References

PHP implements the daemon

The above introduces the workerman notes-php creation daemon process, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Causes and solutions to ConcurrentModificationException exceptions in Java Causes and solutions to ConcurrentModificationException exceptions in Java Jun 25, 2023 am 10:33 AM

In Java, when multiple threads operate a collection object at the same time, a ConcurrentModificationException exception may occur. This exception usually occurs when traversing the collection when modifying or deleting elements. This will cause the state of the collection to be inconsistent, thus throwing abnormal. This article will delve into the causes and solutions to this exception. 1. Causes of Exception Normally, ConcurrentModificationException exception

How to use PHP's POSIX extension? How to use PHP's POSIX extension? Jun 03, 2023 am 08:01 AM

The POSIX extensions for PHP are a set of functions and constants that allow PHP to interact with POSIX-compliant operating systems. POSIX (PortableOperatingSystemInterface) is a set of operating system interface standards designed to allow software developers to write applications that can run on various UNIX or UNIX-like operating systems. This article will introduce how to use POSIX extensions for PHP, including installation and use. 1. Install the POSIX extension of PHP in

How to display the pid in Win7 Task Manager? The editor will teach you how to display it. How to display the pid in Win7 Task Manager? The editor will teach you how to display it. Jan 11, 2024 pm 07:00 PM

Many friends may be unfamiliar with the pid identifier, and you can check it in the task manager. However, some users cannot find the PID identifier when they open the Task Manager. In fact, if the user wants to view the process PID identifier, he or she needs to set the relevant settings of the "Task Manager" to see it. The following editor will take the win7 system as an example. How to view the process PID identifier. The PID identifier is a unique sequential number automatically assigned by the Windows operating system to running programs. After the process is terminated, the PID is recycled by the system and may continue to be assigned to newly running programs. When users need to view the process, they will use the task Manager to check, so how to check the process PID identifier? Let me share it with you below

Linux and BSD alternative Redox OS reaches version 0.9.0 with COSMIC desktop applications and multiple optimizations Linux and BSD alternative Redox OS reaches version 0.9.0 with COSMIC desktop applications and multiple optimizations Sep 12, 2024 pm 12:18 PM

On April 20, 2015, Redox OS surfaced as a new microkernel operating system "with a focus on safety, freedom, reliability, correctness, and pragmatism." Written in Rust and assembly language, this project was inspired by pieces of code such

Common errors and solutions for fork failure in PHP PCNTL Common errors and solutions for fork failure in PHP PCNTL Feb 28, 2024 am 11:06 AM

Common errors and solutions for fork failure in PHPPCNTL When using the PHPPCNTL extension for process management, you often encounter the problem of fork failure. Fork is a method of creating a child process. In some cases, the fork operation may fail due to some errors. This article will introduce some common fork failure errors and corresponding solutions, and provide specific code examples to help readers better understand and deal with these problems. 1. Possible error message for insufficient memory: Can

How to deal with UnsupportedEncodingException in Java? How to deal with UnsupportedEncodingException in Java? Jun 25, 2023 am 08:02 AM

How to deal with UnsupportedEncodingException in Java? In Java programming, you may encounter UnsupportedEncodingException. This exception is usually caused by incorrect encoding conversion or an unsupported encoding. In this article, we will introduce the causes of UnsupportedEncodingException exception and how to deal with it. What is UnsupportedE

Solution to PHP Fatal error: Uncaught exception 'PDOException' Solution to PHP Fatal error: Uncaught exception 'PDOException' Jun 23, 2023 pm 12:09 PM

In PHP development, you may encounter errors such as "PHPFatalerror:Uncaughtexception'PDOException'". This is an exception caused by an error when PHP operates the database. If this error is not handled in time, it will cause program interruption or unexpected errors. So how to solve this problem? Here are some common solutions. 1. Check the database parameters. First, we need to check the parameters passed when connecting to the database.

Analysis of the reasons why PHP PCNTL extended fork function failed Analysis of the reasons why PHP PCNTL extended fork function failed Feb 28, 2024 pm 09:42 PM

Analysis of the reasons for the failure of the PHPPCNTL extension fork function In PHP, the PCNTL extension provides a series of functions for handling process control, of which the fork function is one of the commonly used functions. Through the fork function, we can create a child process to perform a certain task, which is very useful when writing concurrent handlers. However, when using the PCNTL extended fork function, sometimes you will encounter fork failure. This article will analyze the reasons for this situation and give specific codes.

See all articles