1.什么是守护进程
守护进程是脱离于终端并且在后台运行的进程。守护进程脱离于终端是为了避免进程在执行过程中的信息在任何终端上显示并且进程也不会被任何终端所产生的终端信息所打断。
例如 apache, nginx, mysql 都是守护进程
2.为什么开发守护进程
很多程序以服务形式存在,他没有终端或ui交互,它可能采用其他方式与其他程序交互,如tcp/udp socket, unix socket, fifo。程序一旦启动便进入后台,直到满足条件他便开始处理任务。
3.何时采用守护进程开发应用程序
以我当前的需求为例,我需要运行一个程序,然后监听某端口,持续接受服务端发起的数据,然后对数据分析处理,再将结果写入到数据库中; 我采用zeromq实现数据收发。
如果我不采用守护进程方式开发该程序,程序一旦运行就会占用当前终端窗框,还有受到当前终端键盘输入影响,有可能程序误退出。
4.守护进程的安全问题
我们希望程序在非超级用户运行,这样一旦由于程序出现漏洞被骇客控制,攻击者只能继承运行权限,而无法获得超级用户权限。
我们希望程序只能运行一个实例,不运行同事开启两个以上的程序,因为会出现端口冲突等等问题。
5.怎样开发守护进程
例 1. 守护进程例示
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');    /    
   
 
   