PHP System Programming--02.php Daemon

Source: Internet
Author: User

What is a daemon process?

A daemon usually complements a background task that does not control the terminal. It has three distinct features: running in the background, disconnected from the startup process, and without the need to control the terminal. The common way to achieve this isfork() -> setsid() -> fork()

There is a function daemon in glibc. Call this function, you can make the current process out of the terminal into a daemon, the concrete content see man daemon. PHP does not have this function temporarily, PHP program implementation Daemon 2 ways:

Using System Commands Nohup

nohup php myprog.php > log.txt &

&, so that the execution of the program is run in the background, but is actually dependent on the terminal, when the user exits the terminal process will be killed. Need to use Nohup to implement

PHP script Function implementation:
<?phpfunction daemon(){    $pid = pcntl_fork();    if($pid < 0){        die("fork(1) failed!\n");    }elseif($pid > 0){        exit; //让终端启动的进程退出    }     chdir("/"); //改变当前目录为根目录    umask(0); //重设文件权限掩码    //建立一个有别于终端的新session以脱离终端    $sid = posix_setsid();    if (!$sid) {        die("setsid failed!\n");    }    $pid = pcntl_fork();    if($pid < 0){        die("fork(1) failed!\n");    }elseif($pid > 0){        exit; //父进程退出, 剩下子进程成为最终的独立进程    }    //关闭标准I/O流    if (defined(‘STDIN‘)) {        fclose(STDIN);    }    if (defined(‘STDOUT‘)){        fclose(STDOUT);    }    if (defined(‘STDERR‘)) {        fclose(STDERR);    }}daemon();sleep(1000);

The key two PHP functions here are pcntl_fork () and Posix_setsid ()

    • Fork () is a process that creates a copy of the running process, which is considered a child process, and the original process is considered a parent process. When fork () runs, it can be detached from initiating his process and terminal control, and also means that the parent process is free to exit.
    • Setsid (), which first makes the new process the "leader" of a session, and finally makes the process no longer control the terminal, which is the most critical step in becoming a daemon, which means that the process will not be forced to quit as the terminal shuts down. This is a critical step for a permanent process that will not be interrupted.
    • The last fork (), this step is not necessary, but usually do so, its greatest significance is to prevent access to the control terminal. (The control terminal is obtained when a terminal device is opened directly without the use of the O_NOCTTY flag)

Other Notes:

    • The ChDir () daemon inherits the current working directory of the parent process by default and will cause a lot of trouble when the system disk Umount, usually "/" as the current working directory of the daemon, can avoid the above problems
    • The Umask () daemon inherits the file permission mask of the parent process by default, which creates a lot of trouble for the child process to use the file. Therefore, setting the file permission mask to 0 can greatly enhance the daemon's flexibility
    • Fclose (STDIN), fclose (STDOUT), fclose (STDERR) Turn off standard I/O streams. A new child process with the fork function inherits some files that have already been opened from the parent process. These open files may never be read and written by the daemon, but they consume system resources as well, and may cause the file system in which they reside to fail to be unloaded.



From for notes (Wiz)



PHP System Programming--02.php Daemon

Related Article

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.