国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 後端開發(fā) php教程 php 守護程式(Daemon)

php 守護程式(Daemon)

May 28, 2018 pm 03:49 PM
php 行程

這篇文章給大家分享的內容是關於php 守護進程(Daemon),有著一定的參考價值,有需要的朋友可以參考一下

?守護程式(Daemon)是運行在後臺的一種特殊進程。它獨立於控制終端並且週期性地執(zhí)行某種任務或等待處理某些發(fā)生的事件。守護程式是一種很有用的程式。 php也可以實現(xiàn)守護程式的功能。

?

1、基本概念

????程序

????????????每個行程有一個父程式,子行程退出,父程式能由子程式退出的狀態(tài)。

????進程組

????????????每個行程都屬於一個進程組,且每個進程組都有一個進程組號,該號等於該進程組組長的PID

2.守護程式要點

????1. 在背景運作。????
???? ????為避免以暫??刂平K端將Daemon放入後臺執(zhí)行。方法是在進程中呼叫fork使父進程終止,讓Daemon在子進程中後臺執(zhí)行。 if($pid=pcntl_fork()) exit(0);//是父進程,結束父進程,子進程繼續(xù)
????2. 脫離控制終端,登入會話和進程組?
???????中的進程與控制終端,登入會話和進程組之間的關係:進程屬於一個進程組,進程組號(GID)就是進程組長的進程號(PID)。登入會話可以包含多個進程組。這些進程組共用一個控制終端。這個控制終端通常是創(chuàng)建進程的登入終? 端。控制終端,登入會話和進程組通常是從父進程繼承下來的。我們的目的就是要擺脫它們,使之不受它們??的影響。方法是在第1點的基礎上,呼叫setsid() 使進程成為會話組長: posix_setsid();
????????說明:當進程是會話組長時setsid()呼叫失敗。但第一點已經(jīng)保證進程不是會話組長。 setsid()呼叫成功後,進程成為新的會話組長和新的進程組長,並與原先的登入會話和進程組脫離。由於會話過程對控制終端的獨佔性,進程同時與控制終端脫離。
????3. 禁止程序重新開啟控制終端機
??? ????現(xiàn)在,且已成為無終端機的會話組長。但它可以重新申請打開一個控制終端??梢酝高^讓進程不再成為會話群組長度來禁止進程重新開啟控制終端:?if($pid=pcntl_fork())?exit(0);//結束第一子進程,第二子進程繼續(xù)(第二子程序不再是會話組長)
????4. 關閉開啟的檔案描述子
????????以建立它的父行程繼承了開啟的檔案描述子。如不關閉,將會浪費系統(tǒng)資源,造成進程所在的檔案系統(tǒng)無法卸下以及造成無法預料的錯誤。依下列方法關閉它們:
????????fclose(STDIN),fclose(STDOUT),fclose(STDERR)關閉標準輸入輸出與錯誤顯示。
????5. 改變目前工作目錄
????????處理程序活動時,其工作目錄所在的檔案系統(tǒng)無法移除。一般需要將工作目錄改變到根目錄。對於需要轉儲核心,寫入運行日誌的程序將工作目錄改變到特定目錄如chdir("/")
????6. 重設文件創(chuàng)建掩模
????????進程從創(chuàng)建它的父進程那裡繼承了文件創(chuàng)建掩模。它可能修改守護程式所建立的檔案的存取位。為防止這一點,將檔案建立遮罩清除:umask(0);
????7.處理SIGCHLD訊號
????????處理SIGCHLD訊號並不是必須的。但對於某些進程,特別是伺服器進程往往在請求到來時產(chǎn)生子進程處理請求。如果父行程不等待子行程結束,子行程將成為殭屍行程(zombie)從而佔用系統(tǒng)資源。如果父進程等待子進程結束,將增加父進程的負擔,影? 響伺服器進程的並發(fā)效能。在Linux下可以簡單地將SIGCHLD訊號的操作設為SIG_IGN。 signal(SIGCHLD,SIG_IGN);
????????這樣,核心在子行程結束時不會產(chǎn)生殭屍行程。這點與BSD4不同,BSD4下必須明確等待子程序結束才能釋放殭屍行程。關於訊號的問題請參考Linux 訊號說明清單

  1. <?php  
    /** 
    *@author tengzhaorong@gmail.com 
    *@date 2013-07-25 
    * 后臺腳本控制類 
    */  
    class DaemonCommand{  
       
        private $info_dir="/tmp";  
        private $pid_file="";  
        private $terminate=false; //是否中斷  
        private $workers_count=0;  
        private $gc_enabled=null;  
        private $workers_max=8; //最多運行8個進程  
       
        public function __construct($is_sington=false,$user=&#39;nobody&#39;,$output="/dev/null"){  
       
                $this->is_sington=$is_sington; //是否單例運行,單例運行會在tmp目錄下建立一個唯一的PID  
                $this->user=$user;//設置運行的用戶 默認情況下nobody  
                $this->output=$output; //設置輸出的地方  
                $this->checkPcntl();  
        }  
        //檢查環(huán)境是否支持pcntl支持  
        public function checkPcntl(){  
            if ( ! function_exists(&#39;pcntl_signal_dispatch&#39;)) {  
                // PHP < 5.3 uses ticks to handle signals instead of pcntl_signal_dispatch  
                // call sighandler only every 10 ticks  
                declare(ticks = 10);  
            }  
       
            // Make sure PHP has support for pcntl  
            if ( ! function_exists(&#39;pcntl_signal&#39;)) {  
                $message = &#39;PHP does not appear to be compiled with the PCNTL extension.  This is neccesary for daemonization&#39;;  
                $this->_log($message);  
                throw new Exception($message);  
            }  
            //信號處理  
            pcntl_signal(SIGTERM, array(__CLASS__, "signalHandler"),false);  
            pcntl_signal(SIGINT, array(__CLASS__, "signalHandler"),false);  
            pcntl_signal(SIGQUIT, array(__CLASS__, "signalHandler"),false);  
       
            // Enable PHP 5.3 garbage collection  
            if (function_exists(&#39;gc_enable&#39;))  
            {  
                gc_enable();  
                $this->gc_enabled = gc_enabled();  
            }  
        }  
       
        // daemon化程序  
        public function daemonize(){  
       
            global $stdin, $stdout, $stderr;  
            global $argv;  
       
            set_time_limit(0);  
       
            // 只允許在cli下面運行  
            if (php_sapi_name() != "cli"){  
                die("only run in command line mode\n");  
            }  
       
            // 只能單例運行  
            if ($this->is_sington==true){  
       
                $this->pid_file = $this->info_dir . "/" .__CLASS__ . "_" . substr(basename($argv[0]), 0, -4) . ".pid";  
                $this->checkPidfile();  
            }  
       
            umask(0); //把文件掩碼清0  
       
            if (pcntl_fork() != 0){ //是父進程,父進程退出  
                exit();  
            }  
       
            posix_setsid();//設置新會話組長,脫離終端  
       
            if (pcntl_fork() != 0){ //是第一子進程,結束第一子進程     
                exit();  
            }  
       
            chdir("/"); //改變工作目錄  
       
            $this->setUser($this->user) or die("cannot change owner");  
       
            //關閉打開的文件描述符  
            fclose(STDIN);  
            fclose(STDOUT);  
            fclose(STDERR);  
       
            $stdin  = fopen($this->output, &#39;r&#39;);  
            $stdout = fopen($this->output, &#39;a&#39;);  
            $stderr = fopen($this->output, &#39;a&#39;);  
       
            if ($this->is_sington==true){  
                $this->createPidfile();  
            }  
       
        }  
        //--檢測pid是否已經(jīng)存在  
        public function checkPidfile(){  
       
            if (!file_exists($this->pid_file)){  
                return true;  
            }  
            $pid = file_get_contents($this->pid_file);  
            $pid = intval($pid);  
            if ($pid > 0 && posix_kill($pid, 0)){  
                $this->_log("the daemon process is already started");  
            }  
            else {  
                $this->_log("the daemon proces end abnormally, please check pidfile " . $this->pid_file);  
            }  
            exit(1);  
       
        }  
        //----創(chuàng)建pid  
        public function createPidfile(){  
       
            if (!is_dir($this->info_dir)){  
                mkdir($this->info_dir);  
            }  
            $fp = fopen($this->pid_file, &#39;w&#39;) or die("cannot create pid file");  
            fwrite($fp, posix_getpid());  
            fclose($fp);  
            $this->_log("create pid file " . $this->pid_file);  
        }  
       
        //設置運行的用戶  
        public function setUser($name){  
       
            $result = false;  
            if (empty($name)){  
                return true;  
            }  
            $user = posix_getpwnam($name);  
            if ($user) {  
                $uid = $user[&#39;uid&#39;];  
                $gid = $user[&#39;gid&#39;];  
                $result = posix_setuid($uid);  
                posix_setgid($gid);  
            }  
            return $result;  
       
        }  
        //信號處理函數(shù)  
        public function signalHandler($signo){  
       
            switch($signo){  
       
                //用戶自定義信號  
                case SIGUSR1: //busy  
                if ($this->workers_count < $this->workers_max){  
                    $pid = pcntl_fork();  
                    if ($pid > 0){  
                        $this->workers_count ++;  
                    }  
                }  
                break;  
                //子進程結束信號  
                case SIGCHLD:  
                    while(($pid=pcntl_waitpid(-1, $status, WNOHANG)) > 0){  
                        $this->workers_count --;  
                    }  
                break;  
                //中斷進程  
                case SIGTERM:  
                case SIGHUP:  
                case SIGQUIT:  
       
                    $this->terminate = true;  
                break;  
                default:  
                return false;  
            }  
       
        }  
        /** 
        *開始開啟進程 
        *$count 準備開啟的進程數(shù) 
        */  
        public function start($count=1){  
       
            $this->_log("daemon process is running now");  
            pcntl_signal(SIGCHLD, array(__CLASS__, "signalHandler"),false); // if worker die, minus children num  
            while (true) {  
                if (function_exists(&#39;pcntl_signal_dispatch&#39;)){  
       
                    pcntl_signal_dispatch();  
                }  
       
                if ($this->terminate){  
                    break;  
                }  
                $pid=-1;  
                if($this->workers_count<$count){  
       
                    $pid=pcntl_fork();  
                }  
       
                if($pid>0){  
       
                    $this->workers_count++;  
       
                }elseif($pid==0){  
       
                    // 這個符號表示恢復系統(tǒng)對信號的默認處理  
                    pcntl_signal(SIGTERM, SIG_DFL);  
                    pcntl_signal(SIGCHLD, SIG_DFL);  
                    if(!empty($this->jobs)){  
                        while($this->jobs[&#39;runtime&#39;]){  
                            if(empty($this->jobs[&#39;argv&#39;])){  
                                call_user_func($this->jobs[&#39;function&#39;],$this->jobs[&#39;argv&#39;]);  
                            }else{  
                                call_user_func($this->jobs[&#39;function&#39;]);  
                            }  
                            $this->jobs[&#39;runtime&#39;]--;  
                            sleep(2);  
                        }  
                        exit();  
       
                    }  
                    return;  
       
                }else{  
       
                    sleep(2);  
                }  
       
       
            }  
       
            $this->mainQuit();  
            exit(0);  
       
        }  
       
        //整個進程退出  
        public function mainQuit(){  
       
            if (file_exists($this->pid_file)){  
                unlink($this->pid_file);  
                $this->_log("delete pid file " . $this->pid_file);  
            }  
            $this->_log("daemon process exit now");  
            posix_kill(0, SIGKILL);  
            exit(0);  
        }  
       
        // 添加工作實例,目前只支持單個job工作  
        public function setJobs($jobs=array()){  
       
            if(!isset($jobs[&#39;argv&#39;])||empty($jobs[&#39;argv&#39;])){  
       
                $jobs[&#39;argv&#39;]="";  
       
            }  
            if(!isset($jobs[&#39;runtime&#39;])||empty($jobs[&#39;runtime&#39;])){  
       
                $jobs[&#39;runtime&#39;]=1;  
       
            }  
       
            if(!isset($jobs[&#39;function&#39;])||empty($jobs[&#39;function&#39;])){  
       
                $this->log("你必須添加運行的函數(shù)!");  
            }  
       
            $this->jobs=$jobs;  
       
        }  
        //日志處理  
        private  function _log($message){  
            printf("%s\t%d\t%d\t%s\n", date("c"), posix_getpid(), posix_getppid(), $message);  
        }  
       
    }  
       
    //調用方法1  
    $daemon=new DaemonCommand(true);  
    $daemon->daemonize();  
    $daemon->start(2);//開啟2個子進程工作  
    work();  
       
       
       
       
    //調用方法2  
    $daemon=new DaemonCommand(true);  
    $daemon->daemonize();  
    $daemon->addJobs(array(&#39;function&#39;=>&#39;work&#39;,&#39;argv&#39;=>&#39;&#39;,&#39;runtime&#39;=>1000));//function 要運行的函數(shù),argv運行函數(shù)的參數(shù),runtime運行的次數(shù)  
    $daemon->start(2);//開啟2個子進程工作  
       
    //具體功能的實現(xiàn)  
    function work(){  
          echo "測試1";  
    }  
    ?>

相關推薦:

PHP進程間通訊詳解

總結關於PHP進程通訊注意點


#

以上是php 守護程式(Daemon)的詳細內容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

為什麼我們評論:PHP指南 為什麼我們評論:PHP指南 Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

如何在Windows上安裝PHP 如何在Windows上安裝PHP Jul 15, 2025 am 02:46 AM

安裝PHP在Windows上的關鍵步驟包括:1.下載合適的PHP版本並解壓,推薦使用ThreadSafe版本配合Apache或NonThreadSafe版本配合Nginx;2.配置php.ini文件,將php.ini-development或php.ini-production重命名為php.ini;3.將PHP路徑添加到系統(tǒng)環(huán)境變量Path中以便命令行使用;4.測試PHP是否安裝成功,通過命令行執(zhí)行php-v和運行內置服務器測試解析能力;5.若使用Apache,需在httpd.conf中配置P

PHP語法:基礎知識 PHP語法:基礎知識 Jul 15, 2025 am 02:46 AM

PHP的基礎語法包括四個關鍵點:1.PHP標籤必須使用結束,推薦使用完整標籤;2.輸出內容常用echo和print,其中echo支持多參數(shù)且效率更高;3.註釋方式有//、#和//,用於提升代碼可讀性;4.每條語句必須以分號結尾,空格和換行不影響執(zhí)行但影響可讀性。掌握這些基本規(guī)則有助於寫出清晰穩(wěn)定的PHP代碼。

PHP 8安裝指南 PHP 8安裝指南 Jul 16, 2025 am 03:41 AM

在Ubuntu上安裝PHP8的步驟為:1.更新軟件包列表;2.安裝PHP8及基礎組件;3.檢查版本確認安裝成功;4.按需安裝額外模塊。 Windows用戶可下載ZIP包並解壓,隨後修改配置文件、啟用擴展並將路徑加入環(huán)境變量。 macOS用戶推薦使用Homebrew安裝,依次執(zhí)行添加tap、安裝PHP8、設置默認版本及驗證版本等步驟。不同系統(tǒng)下安裝方式雖有差異,但流程清晰,根據(jù)用途選對方法即可。

什麼是PHP,它是用什麼? 什麼是PHP,它是用什麼? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

python如果還有示例 python如果還有示例 Jul 15, 2025 am 02:55 AM

寫Python的ifelse語句關鍵在於理解邏輯結構與細節(jié)。 1.基礎結構是if條件成立執(zhí)行一段代碼,否則執(zhí)行else部分,else可選;2.多條件判斷用elif實現(xiàn),順序執(zhí)行且一旦滿足即停止;3.嵌套if用於進一步細分判斷,建議不超過兩層;4.簡潔場景可用三元表達式替代簡單ifelse。注意縮進、條件順序及邏輯完整性,才能寫出清晰穩(wěn)定的判斷代碼。

您的第一個PHP腳本:實用介紹 您的第一個PHP腳本:實用介紹 Jul 16, 2025 am 03:42 AM

如何開始編寫第一個PHP腳本?首先設置本地開發(fā)環(huán)境,安裝XAMPP/MAMP/LAMP,使用文本編輯器,了解服務器運行原理。其次,創(chuàng)建一個名為hello.php的文件,輸入基本代碼並運行測試。第三,學習混合使用PHP與HTML以實現(xiàn)動態(tài)內容輸出。最後,注意常見錯誤如缺少分號、引用問題及文件擴展名錯誤,並開啟錯誤報告以便調試。

您如何處理PHP中的文件操作(閱讀/寫作)? 您如何處理PHP中的文件操作(閱讀/寫作)? Jul 16, 2025 am 03:48 AM

tohandlefileoperationsinphp,useApprepreprunctions andModes.1.toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline by line-line-processing.2.towriteToafile,usefile_put_cte_contents(usefile_contents)(

See all articles