在我们日常编程中,常见的例子比如 php think 需要不断执行的任务,比如 php arts… 和 php yii…,都会通过 nohup 挂载到后台,以保持长期运行状态。同样,在workerman中,使用类似php index.php start的命令来启动进程,但不同的是,它不需要使用nohup来挂载并在后台运行。有的朋友可能会好奇是怎么实现的呢?为了解决小伙伴们关心的问题,今天我们重点深入分析workerman守护进程的实现原理。
我们先来了解一些流程相关的知识:
父进程:父进程是生成其他进程的进程。当一个进程创建另一个进程时,创建者称为父进程,创建的进程成为子进程。父进程可以通过进程标识符(pid)来识别它创建的子进程。
子进程:子进程是父进程创建的新进程。子进程继承了父进程的一些属性,比如环境变量、文件描述符等。子进程独立于父进程运行,可以执行自己的代码,拥有自己的资源和内存空间。
进程组:进程组是关联进程的集合。每个进程组都有一个唯一的进程组 id (pgid),用于标识该进程组。进程组通常由父进程创建,并包括与父进程具有相同会话 id (sid) 的所有子进程。
会话:会话是相关进程的集合,通常在用户登录系统时开始,在用户注销或关闭终端时结束。会话中的进程共享同一个控制终端。每个会话都有一个唯一的会话 id (sid),用于标识该会话。一个会话通常包含一个或多个进程组,第一个进程组成为会话的主进程组。
这些概念俗称八足文,从来都不容易理解。让我们看一个例子。执行命令“php index.html”后php’,生成了进程61052。该进程的父进程是bash进程8243,所以这里无需担心。然后,通过fork创建了一个子进程61053,其父进程为61052。这两个进程有共同的进程组61052和会话8243。调用posix_setsid函数会为子进程打开一个新的进程组61053和一个新的会话61053进程61053,其中的会话可以理解为新的命令窗口终端。最后,子进程61053通过fork创建了子进程61054,进程61053升级为父进程。再次fork的原因是为了避免与终端控制的进程相关联。这个进程61052是在终端模式下创建的,从此以后,进程61054就形成了守护进程。
[ manongsen@root phpwork]$ php index.php [parent] process id: 61052, parent process id: 8243, process group id: 61052, session id: 8243 [parent1] process id: 61052, parent process id: 8243, process group id: 61052, session id: 8243 exited the process [child1] process id: 61053, parent process id: 61052, process group id: 61052, session id: 8243 [child1] process id: 61053, parent process id: 61052, process group id: 61053, session id: 61053 [parent2] process id: 61053, parent process id: 61052, process group id: 61053, session id: 61053 exited the process [child2] process id: 61054, parent process id: 61053, process group id: 61053, session id: 61053. this process is retained [ manongsen@root phpwork]$ ps aux | grep index.php root 66064 0.0 0.0 408105040 1472 s080 s+10:00 pm 00:00 grep index.php root 61054 0.0 438073488 280?? s 10:00 pm 00:00 php index.php
登录后复制
上面提到的进程信息就是通过这段代码的执行而生成的。如果你仔细阅读这段代码,你会发现为什么 posix_setsid 函数不是在第一个 fork 之前调用,而是在第二个 fork 之前调用。这样就不用fork两次了?原因是leader进程无法创建会话,而进程组id 61052与进程id 61052相同,即当前进程是leader进程。因此需要一个子进程来创建新的session,这一点需要特别注意。
php function echomsg($prefix, $suffix="") { //process id $pid = getmypid(); //process group id $pgid = posix_getpgid($pid); //session id $sid = posix_getsid($pid); //parent process id $ppid = posix_getppid(); echo "[{$prefix}] process id: {$pid}, parent process id: {$ppid}, process group id: {$pgid}, session id: {$sd} {$suffix}". php_eol; } //[parent] process id: 61052, parent process id: 8243, process group id: 61052, session id: 8243 echomsg("parent"); //the first fork process $pid = pcntl_fork(); if ( $pid 0 ) { //[parent1] process id: 61052, parent process id: 8243, process group id: 61052, session id: 8243 exited the process echoes ("parent1", "exited the process"); exit; } //the child process id created is 61053, but the process group, session, and parent process are still the same //[child1] process id: 61053, parent process id: 61052, process group id: 61052, session id: 8243 echomsg("child1"); //calling the posix_setsid function will create a new session and process group, and set the process group id and session id to that process id if (-1 === \posix_setsid()) { throw new exception("setsid fail"); } //now you will find that both the process group id and session id have changed to 61053, which is equivalent to starting a session window similar to a linux terminal //[child1] process id: 61053, parent process id: 61052, process group id: 61053, session id: 61053 echomsg("child1"); //second fork process //the reason for requiring a secondary fork process here is to avoid being associated with terminal controlled processes. this process 61052 was created in terminal mode //need to detach from process 61052 to ensure the stability of the daemon process $pid = pcntl_fork(); if ( $pid 0 ) { //[parent2] process id: 61053, parent process id: 61052, process group id: 61053, session id: 61053 exited the process echoes ("parent2", "exited the process"); exit; } //at this point, the process has broken free from the control of the terminal process and formed a daemon process //[child2] process id: 61054, parent process id: 61053, process group id: 61053, session id: 61053. this process is retained echoes ("child2", "this process is reserved"); sleep(100);
登录后复制
最好有时间的朋友自己执行代码分析一下,会有不同的奖励。这里假装你已经实践过了,我们看一下workerman的worker.php文件第554行runall方法中的static::daemon()函数。实现的流程逻辑与上面的例子几乎相同。不过这里也用到了umask函数,它的主要作用是给进程创建的文件或目录授予相应的权限,保证有对文件或目录进行操作的权限。
// workerman/Worker.php:554 /** * Run all worker instances. *Run process * @return void */ public static function runAll() { static::checkSapiEnv(); static::init(); static::parseCommand(); static::lock(); //Create a process and form a daemon process static::daemonize(); static::initWorkers(); static::installSignal(); static::saveMasterPid(); static::lock(\LOCK_UN); static::displayUI(); static::forkWorkers(); static::resetStd(); static::monitorWorkers(); } // workerman/Worker.php:1262 /** * Run as daemon mode. *Run in daemon mode * @throws Exception */ protected static function daemonize() { //Determine whether it is already in a guarded state and whether the current system is in a Linux environment if (! static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) { return; } //If umask is set to 0, the file permissions created by the current process will be 777, which has the highest permission \umask(0); //The first time creating a process $pid = \pcntl_fork(); if (-1 === $pid) { //Process creation failed throw new Exception('Fork fail'); } elseif ($pid > 0) { //Main process exits exit(0); } //The child process continues to execute //By calling the posix_setsid function, a process can detach from its parent process and transform into a daemon process if (-1 === \posix_setsid()) { throw new Exception("Setsid fail"); } //The second creation process, in a System V based system, exits by forking the parent process again //Ensure that the formed daemon process does not become the first session process and does not have control terminals $pid = \pcntl_fork(); if (-1 === $pid) { //Process creation failed throw new Exception("Fork fail"); } elseif (0 !== $pid) { //Main process exits exit(0); } //The child process continues to execute }
登录后复制
守护进程也是workerman的重要组成部分,保证了workerman进程的稳定性。与我们通过nohup启动的命令不同,这些命令有时会在后台挂起而没有人注意到,朋友们可能已经经历过这一点。当然,市面上也有一些开源的守护进程管理软件,比如supervisors,也有人使用screen、tmux等会话终端来实现。事实上,守护进程的实现方式有很多种。这里仅介绍一个php中实现守护进程模式的例子来分析workerman中守护进程的实现原理。希望本内容能够对您有所帮助。
如果您想体验新版本php或者切换php版本,欢迎使用servbay部署php开发环境。这是我正在开发的产品,我会长期维护这个工具。
立即学习“PHP免费学习笔记(深入)”;
以上就是深入理解PHP高性能框架中Workerman守护进程原理的详细内容,更多请关注至强加速其它相关文章!
本文来源于互联网,如若侵权,请联系管理员删除,本文链接:https://www.9969.net/35440.html