PHP高级编程实例:编写守护进程


Posted in PHP onSeptember 02, 2014

1.什么是守护进程

守护进程是脱离于终端并且在后台运行的进程。守护进程脱离于终端是为了避免进程在执行过程中的信息在任何终端上显示并且进程也不会被任何终端所产生的终端信息所打断。

例如 apache, nginx, mysql 都是守护进程

2.为什么开发守护进程

很多程序以服务形式存在,他没有终端或UI交互,它可能采用其他方式与其他程序交互,如TCP/UDP Socket, UNIX Socket, fifo。程序一旦启动便进入后台,直到满足条件他便开始处理任务。

3.何时采用守护进程开发应用程序

以我当前的需求为例,我需要运行一个程序,然后监听某端口,持续接受服务端发起的数据,然后对数据分析处理,再将结果写入到数据库中; 我采用ZeroMQ实现数据收发。

如果我不采用守护进程方式开发该程序,程序一旦运行就会占用当前终端窗框,还有受到当前终端键盘输入影响,有可能程序误退出。

4.守护进程的安全问题

我们希望程序在非超级用户运行,这样一旦由于程序出现漏洞被骇客控制,攻击者只能继承运行权限,而无法获得超级用户权限。

我们希望程序只能运行一个实例,不运行同事开启两个以上的程序,因为会出现端口冲突等等问题。

5.怎样开发守护进程

例 1. 守护进程例示

<?php
class ExampleWorker extends Worker {

 #public function __construct(Logging $logger) {
 # $this->logger = $logger;
 #}

 #protected $logger;
 protected static $dbh;
 public function __construct() {

 }
 public function run(){
  $dbhost = '192.168.2.1';  // 数据库服务器
  $dbport = 3306;
   $dbuser = 'www';  // 数据库用户名
 $dbpass = 'qwer123';    // 数据库密码
  $dbname = 'example';  // 数据库名

  self::$dbh = new PDO("mysql:host=$dbhost;port=$dbport;dbname=$dbname", $dbuser, $dbpass, array(
   /* PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'', */
   PDO::MYSQL_ATTR_COMPRESS => true,
   PDO::ATTR_PERSISTENT => true
   )
  );

 }
 protected function getInstance(){
 return self::$dbh;
  }

}

/* the collectable class implements machinery for Pool::collect */
class Fee extends Stackable {
 public function __construct($msg) {
  $trades = explode(",", $msg);
  $this->data = $trades;
  print_r($trades);
 }

 public function run() {
  #$this->worker->logger->log("%s executing in Thread #%lu", __CLASS__, $this->worker->getThreadId() );

  try {
   $dbh = $this->worker->getInstance();
   
   $insert = "INSERT INTO fee(ticket, login, volume, `status`) VALUES(:ticket, :login, :volume,'N')";
   $sth = $dbh->prepare($insert);
   $sth->bindValue(':ticket', $this->data[0]);
   $sth->bindValue(':login', $this->data[1]);
   $sth->bindValue(':volume', $this->data[2]);
   $sth->execute();
   $sth = null;
   
   /* ...... */
   
   $update = "UPDATE fee SET `status` = 'Y' WHERE ticket = :ticket and `status` = 'N'";
   $sth = $dbh->prepare($update);
   $sth->bindValue(':ticket', $this->data[0]);
   $sth->execute();
   //echo $sth->queryString;
   //$dbh = null;
  }
  catch(PDOException $e) {
   $error = sprintf("%s,%s\n", $mobile, $id );
   file_put_contents("mobile_error.log", $error, FILE_APPEND);
  }
 }
}

class Example {
 /* config */
 const LISTEN = "tcp://192.168.2.15:5555";
 const MAXCONN = 100;
 const pidfile = __CLASS__;
 const uid = 80;
 const gid = 80;
 
 protected $pool = NULL;
 protected $zmq = NULL;
 public function __construct() {
  $this->pidfile = '/var/run/'.self::pidfile.'.pid';
 }
 private function daemon(){
  if (file_exists($this->pidfile)) {
   echo "The file $this->pidfile exists.\n";
   exit();
  }
  
  $pid = pcntl_fork();
  if ($pid == -1) {
    die('could not fork');
  } else if ($pid) {
    // we are the parent
    //pcntl_wait($status); //Protect against Zombie children
   exit($pid);
  } else {
   // we are the child
   file_put_contents($this->pidfile, getmypid());
   posix_setuid(self::uid);
   posix_setgid(self::gid);
   return(getmypid());
  }
 }
 private function start(){
  $pid = $this->daemon();
  $this->pool = new Pool(self::MAXCONN, \ExampleWorker::class, []);
  $this->zmq = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
  $this->zmq->bind(self::LISTEN);
  
  /* Loop receiving and echoing back */
  while ($message = $this->zmq->recv()) {
   //print_r($message);
   //if($trades){
     $this->pool->submit(new Fee($message));
     $this->zmq->send('TRUE'); 
   //}else{
   // $this->zmq->send('FALSE'); 
   //}
  }
  $pool->shutdown(); 
 }
 private function stop(){

  if (file_exists($this->pidfile)) {
   $pid = file_get_contents($this->pidfile);
   posix_kill($pid, 9); 
   unlink($this->pidfile);
  }
 }
 private function help($proc){
  printf("%s start | stop | help \n", $proc);
 }
 public function main($argv){
  if(count($argv) < 2){
   printf("please input help parameter\n");
   exit();
  }
  if($argv[1] === 'stop'){
   $this->stop();
  }else if($argv[1] === 'start'){
   $this->start();
  }else{
   $this->help($argv[0]);
  }
 }
}

$cgse = new Example();
$cgse->main($argv);

5.1. 程序启动

下面是程序启动后进入后台的代码

通过进程ID文件来判断,当前进程状态,如果进程ID文件存在表示程序在运行中,通过代码file_exists($this->pidfile)实现,但而后进程被kill需要手工删除该文件才能运行

private function daemon(){
  if (file_exists($this->pidfile)) {
   echo "The file $this->pidfile exists.\n";
   exit();
  }
  
  $pid = pcntl_fork();
  if ($pid == -1) {
    die('could not fork');
  } else if ($pid) {
   // we are the parent
   //pcntl_wait($status); //Protect against Zombie children
   exit($pid);
  } else {
   // we are the child
   file_put_contents($this->pidfile, getmypid());
   posix_setuid(self::uid);
   posix_setgid(self::gid);
   return(getmypid());
  }
 }

程序启动后,父进程会推出,子进程会在后台运行,子进程权限从root切换到指定用户,同时将pid写入进程ID文件。

5.2. 程序停止

程序停止,只需读取pid文件,然后调用posix_kill($pid, 9); 最后将该文件删除。

private function stop(){

  if (file_exists($this->pidfile)) {
   $pid = file_get_contents($this->pidfile);
   posix_kill($pid, 9); 
   unlink($this->pidfile);
  }
 }
PHP 相关文章推荐
MVC模式的PHP实现
Oct 09 PHP
让你的网站首页自动选择语言转跳
Dec 06 PHP
php zend 相对路径问题
Jan 12 PHP
mayfish 数据入库验证代码
Apr 30 PHP
关于初学PHP时的知识积累总结
Jun 07 PHP
php多用户读写文件冲突的解决办法
Nov 06 PHP
php类中的各种拦截器用法分析
Nov 03 PHP
学习php设计模式 php实现桥梁模式(bridge)
Dec 07 PHP
PHP汉字转换拼音的函数代码
Dec 30 PHP
制作个性化的WordPress登陆界面的实例教程
May 21 PHP
php版微信公众号接口实现发红包的方法
Oct 14 PHP
php对象工厂类完整示例
Aug 09 PHP
php输入流php://input使用浅析
Sep 02 #PHP
php获取URL中带#号等特殊符号参数的解决方法
Sep 02 #PHP
PHP中提问频率最高的11个面试题和答案
Sep 02 #PHP
PHP处理Json字符串解码返回NULL的解决方法
Sep 01 #PHP
PHP实现更新中间关联表数据的两种方法
Sep 01 #PHP
重新认识php array_merge函数
Aug 31 #PHP
浅析PHP中strlen和mb_strlen的区别
Aug 31 #PHP
You might like
使用PHP批量生成随机用户名
2008/07/10 PHP
百度实时推送api接口应用示例
2014/10/21 PHP
php自定义加密与解密程序实例
2014/12/31 PHP
PHP实现原生态图片上传封装类方法
2016/11/08 PHP
Open and Print a Word Document
2007/06/15 Javascript
js下将字符串当函数执行的方法
2011/07/13 Javascript
jquery右下角弹出提示框示例代码
2013/10/08 Javascript
JS实现超简单的仿QQ折叠菜单效果
2015/09/21 Javascript
JS实现可展开折叠层的鼠标拖曳效果
2015/10/09 Javascript
谈谈JavaScript自定义回调函数
2015/10/18 Javascript
JavaScript的React框架中的JSX语法学习入门教程
2016/03/05 Javascript
基于BootStrap的图片轮播效果展示实例代码
2016/05/23 Javascript
JQuery DIV 动态隐藏和显示的方法
2016/06/23 Javascript
浅谈js的url解析函数封装
2016/06/28 Javascript
使用Vue开发动态刷新Echarts组件的教程详解
2018/03/22 Javascript
jQuery实现当拉动滚动条到底部加载数据的方法分析
2019/01/24 jQuery
nodejs微信开发之接入指南
2019/03/17 NodeJs
Vue模板语法中数据绑定的实例代码
2019/05/17 Javascript
express + jwt + postMan验证实现持久化登录
2019/06/05 Javascript
使用layui实现的左侧菜单栏以及动态操作tab项方法
2019/09/10 Javascript
layui 对table中的数据进行转义的实例
2019/09/12 Javascript
VUEX采坑之路之获取不到$store的解决方法
2019/11/08 Javascript
记录微信小程序 height: calc(xx - xx);无效问题
2019/12/30 Javascript
[01:13]2015国际邀请赛线下观战现场
2015/08/08 DOTA
Python requests模块实例用法
2019/02/11 Python
PyCharm2019安装教程及其使用(图文教程)
2019/09/29 Python
python中return如何写
2020/06/18 Python
CSS3 制作旋转的大风车(充满童年回忆)
2013/01/30 HTML / CSS
异常和异常类的概念
2014/09/12 面试题
string = null 和string = ''的区别
2013/04/28 面试题
园林设计专业毕业生求职信
2014/03/23 职场文书
合作协议书
2014/04/23 职场文书
2014年机关党建工作总结
2014/11/11 职场文书
公司趣味运动会开幕词
2016/03/04 职场文书
MySQL大小写敏感的注意事项
2021/05/24 MySQL
JavaScript圣杯布局与双飞翼布局实现案例详解
2022/08/05 Javascript