abstract:這篇文章主要介紹了PHP編寫daemon process 實(shí)例詳解的相關(guān)資料。PHP編寫daemon process今天下午在segmentfault.com看到一個(gè)提問(wèn),提問(wèn)標(biāo)題是“PHP怎么做服務(wù)化”,其中問(wèn)道php是不是只能以web方式調(diào)用。其實(shí)很多人對(duì)PHP的使用場(chǎng)景都有誤解,認(rèn)為php只能用于編寫web腳本,實(shí)際上,從PHP4開(kāi)始,php的使用場(chǎng)景早已不限于處理web請(qǐng)求。 從php的架
這篇文章主要介紹了PHP編寫daemon process 實(shí)例詳解的相關(guān)資料。
PHP編寫daemon process
今天下午在segmentfault.com看到一個(gè)提問(wèn),提問(wèn)標(biāo)題是“PHP怎么做服務(wù)化”,其中問(wèn)道php是不是只能以web方式調(diào)用。其實(shí)很多人對(duì)PHP的使用場(chǎng)景都有誤解,認(rèn)為php只能用于編寫web腳本,實(shí)際上,從PHP4開(kāi)始,php的使用場(chǎng)景早已不限于處理web請(qǐng)求。 從php的架構(gòu)體系來(lái)說(shuō),php分為三個(gè)層次:sapi、php core和zend engine。php core本身和web沒(méi)有任何耦合,php通過(guò)sapi與其它應(yīng)用程序通信,例如mod_php就是為apache編寫的sapi實(shí)現(xiàn),同樣,fpm是一個(gè)基于fastcgi協(xié)議的sapi實(shí)現(xiàn),這些sapi都是與web server配合用于處理web請(qǐng)求的。但是也有許多sapi與web無(wú)關(guān),例如cli sapi可以使得在命令行環(huán)境下直接執(zhí)行php,embed sapi可以將php嵌入其它語(yǔ)言(如Lua)那樣。這里我并不打算詳細(xì)討論php的架構(gòu)體系和sapi的話題,只是說(shuō)明從架構(gòu)體系角度目前的php早已被設(shè)計(jì)為支持各種環(huán)境,而非為web獨(dú)有。 除了架構(gòu)體系的支持外,php豐富的擴(kuò)展模塊也為php在不同環(huán)境發(fā)揮作用提供了后盾,例如本文要提到的pcntl模塊和posix模塊配合可以實(shí)現(xiàn)基本的進(jìn)程管理、信號(hào)處理等操作系統(tǒng)級(jí)別的功能,而sockets模塊可以使php具有socket通信的能力。因此php完全可以用于編寫類似于shell或perl常做的工具性腳本,甚至是具有server性質(zhì)的daemon process。 為了展示php如何編寫daemon server,我用php編寫了一個(gè)簡(jiǎn)單的http server,這個(gè)server以daemon process的形式運(yùn)行。當(dāng)然,為了把重點(diǎn)放在如何使用php編寫daemon,我沒(méi)有為這個(gè)http server實(shí)現(xiàn)具體業(yè)務(wù)邏輯,但它可以監(jiān)聽(tīng)指定端口,接受http請(qǐng)求并返回給客戶端一條固定的文本,整個(gè)過(guò)程通過(guò)socket實(shí)現(xiàn),全部由php編寫而成。
代碼實(shí)例
下面是這個(gè)程序的完整代碼:
<?php //Accpet the http client request and generate response content. //As a demo, this function just send "PHP HTTP Server" to client. function handle_http_request($address, $port) { $max_backlog = 16; $res_content = "HTTP/1.1 200 OK Content-Length: 15 Content-Type: text/plain; charset=UTF-8 PHP HTTP Server"; $res_len = strlen($res_content); //Create, bind and listen to socket if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE) { echo "Create socket failed!\n"; exit; } if((socket_bind($socket, $address, $port)) === FALSE) { echo "Bind socket failed!\n"; exit; } if((socket_listen($socket, $max_backlog)) === FALSE) { echo "Listen to socket failed!\n"; exit; } //Loop while(TRUE) { if(($accept_socket = socket_accept($socket)) === FALSE) { continue; } else { socket_write($accept_socket, $res_content, $res_len); socket_close($accept_socket); } } } //Run as daemon process. function run() { if(($pid1 = pcntl_fork()) === 0) //First child process { posix_setsid(); //Set first child process as the session leader. if(($pid2 = pcntl_fork()) === 0) //Second child process, which run as daemon. { //Replaced with your own domain or address. handle_http_request('www.codinglabs.org', 9999); } else { //First child process exit; exit; } } else { //Wait for first child process exit; pcntl_wait($status); } } //Entry point. run(); ?>
這里我假設(shè)各位對(duì)Unix環(huán)境編程都比較了解,所以不做太多細(xì)節(jié)的解釋,只梳理一下。簡(jiǎn)單來(lái)看,這個(gè)程序主要由兩個(gè)部分組成,handle_http_request函數(shù)負(fù)責(zé)處理http請(qǐng)求,其編寫方法與用C編寫的tcp server類似:創(chuàng)建socket、綁定、監(jiān)聽(tīng),然后通過(guò)一個(gè)循環(huán)處理每個(gè)connect過(guò)來(lái)的客戶端,一旦accept到一個(gè)連接...
更多關(guān)于PHP編寫daemon process 實(shí)例詳解請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其它文章!