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 相关文章推荐
hessian 在PHP中的使用介绍
Dec 13 PHP
浅析THINKPHP的addAll支持的最大数据量
Feb 03 PHP
php解析http获取的json字符串变量总是空白null
Mar 02 PHP
PHP使用适合阅读的格式显示文件大小的方法
Mar 05 PHP
经典PHP加密解密函数Authcode()修复版代码
Apr 05 PHP
PHP封装的字符串加密解密函数
Dec 18 PHP
优化WordPress中文章与评论的时间显示
Jan 12 PHP
phpmailer简单发送邮件的方法(附phpmailer源码下载)
Jun 13 PHP
PHP入门教程之表单与验证实例详解
Sep 11 PHP
PHP+MySQL实现消息队列的方法分析
May 09 PHP
thinkphp框架表单数组实现图片批量上传功能示例
Apr 04 PHP
Thinkphp 框架配置操作之配置加载与读取配置实例分析
May 15 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
cakephp常见知识点汇总
2017/02/24 PHP
JavaScript游戏之优化篇
2010/11/08 Javascript
table行随鼠标移动变色示例
2014/05/07 Javascript
如何获取网站icon有哪些可行的方法
2014/06/05 Javascript
jQuery实现的一个自定义Placeholder属性插件
2014/08/11 Javascript
js实现网页右上角滑出会自动消失大幅广告的方法
2015/02/27 Javascript
JavaScript的类型、值和变量小结
2015/07/09 Javascript
JavaScript中函数表达式和函数声明及函数声明与函数表达式的不同
2015/11/15 Javascript
jQuery EasyUI基础教程之EasyUI常用组件(推荐)
2016/07/15 Javascript
基于cookie实现zTree树刷新后展开状态不变
2017/02/28 Javascript
使用vux实现上拉刷新功能遇到的坑
2018/02/08 Javascript
layui table 参数设置方法
2018/08/14 Javascript
浅析JS中什么是自定义react数据验证组件
2018/10/19 Javascript
原生JS实现列表内容自动向上滚动效果
2019/05/22 Javascript
原生javascript中this几种常见用法总结
2020/02/24 Javascript
[38:32]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第二局
2016/02/26 DOTA
[01:28:44]DOTA2-DPC中国联赛定级赛 RNG vs iG BO3第一场 1月10日
2021/03/11 DOTA
跟老齐学Python之有容乃大的list(3)
2014/09/15 Python
Python的Flask框架应用调用Redis队列数据的方法
2016/06/06 Python
一个基于flask的web应用诞生 用户注册功能开发(5)
2017/04/11 Python
python学习教程之Numpy和Pandas的使用
2017/09/11 Python
django 环境变量配置过程详解
2019/08/06 Python
python 叠加等边三角形的绘制的实现
2019/08/14 Python
详解Python中的分支和循环结构
2020/02/11 Python
Python unittest工作原理和使用过程解析
2020/02/24 Python
Pytorch上下采样函数--interpolate用法
2020/07/07 Python
Python字符串及文本模式方法详解
2020/09/10 Python
python 无损批量压缩图片(支持保留图片信息)的示例
2020/09/22 Python
python 批量下载bilibili视频的gui程序
2020/11/20 Python
Superdry极度乾燥官网:日本街头风格,纯英国制造品牌
2016/10/31 全球购物
blueseventy官网:铁人三项和比赛泳衣
2021/02/06 全球购物
应届毕业生通用的自荐书范文
2014/02/07 职场文书
篮球比赛拉拉队口号
2014/06/10 职场文书
自查自纠工作情况报告
2014/10/29 职场文书
教师师德表现自我评价
2015/03/05 职场文书
政工师工作总结2015
2015/05/26 职场文书