守护进程及守护进程输出

1 创建一个输出程序
2 创建一个守护进程


1 创建一个输出程序
守护进程不与终端联系,所以,需要另外创建一个程序用于输出。
也可以直接使用/bin/echo

----- example_daemon_help.cc

  1. #include <stdio.h>  
  2.   
  3. int main(int argc, char** argv)  
  4. {  
  5.      if(argc == 1)  
  6.      {  
  7.           printf("only one parameter \n");  
  8.      }  
  9.      else if(argc == 2)  
  10.      {  
  11.           printf("%s \n", argv[1]);  
  12.      }  
  13.      else  
  14.      {  
  15.           printf("too more parameter: %d\n", argc);  
  16.      }  
  17.   
  18.      return 0;  
  19. }  



编译:

g++ -o example_daemon_help example_daemon_help.cc --std=c++11


2 创建一个守护进程

先写一个函数: int daemon_init(const char* pname, int facility);
之后,只要将进程名传入此函数,就会变成守护进程。

----- 来自UNP第十三章, 有改动

  1. int daemon_init(const char* pname, int facility)  
  2. {  
  3.      umask(0);  
  4.   
  5.      pid_t pid;  
  6.   
  7.      if((pid = fork()) < 0)  
  8.           return -1;  
  9.      else if(pid > 0)  
  10.           _exit(0); // parent terminates  
  11.   
  12.      // child 1 continues  
  13.       
  14.      // become session leader  
  15.      if(setsid() < 0)  
  16.           return -1;  
  17.   
  18.      // ignore SIGHUP  
  19.      // signal(SIGHUP, SIG_IGN);  
  20.      struct sigaction sa;  
  21.      sa.sa_handler = SIG_IGN;  
  22.      sigemptyset(&sa.sa_mask);  
  23.      sa.sa_flags = 0;  
  24.   
  25.      if(sigaction(SIGHUP, &sa, nullptr) < 0)  
  26.           _exit(0);  
  27.   
  28.      if((pid = fork()) < 0)  
  29.           return -1;  
  30.      else if(pid > 0)  
  31.           _exit(0);     // child 1 terminates  
  32.   
  33.      // child 2 continues  
  34.       
  35.      helpguy::daemon_flag = 1;  
  36.   
  37.      // changes working directory  
  38.      chdir("/");  
  39.   
  40.      // close off file descriptors  
  41.      rlimit rl;  
  42.      if(getrlimit(RLIMIT_NOFILE, &rl) < 0)  
  43.           return -1;  
  44.   
  45.      int fdMax = rl.rlim_max == RLIM_INFINITY ? 1024 : rl.rlim_max;  
  46.      for(int i = 0; i < fdMax; ++i)  
  47.           close(i);  
  48.   
  49.      // redirect stdin, stdout and stderr to /dev/null  
  50.      open("/dev/null", O_RDONLY);  
  51.      open("/dev/null", O_RDWR);  
  52.      open("/dev/null", O_RDWR);  
  53.   
  54.      openlog(pname, LOG_PID, facility);  
  55.   
  56.      return 0;  
  57. }  

// 关于_exit请看: 
http://blog.csdn.net/alex_my/article/details/39318607

下面是整个程序代码:

----- example_daemon.cc

  1. #include "helpguy.h"  // 含有daemon_init函数  
  2. #include <syslog.h>  
  3.   
  4. int main(int argc, char** argv)  
  5. {  
  6.      // daemon process  
  7.      daemon_init(argv[0], 0);  
  8.   
  9.      int count = 0;  
  10.      while(++count <= 2)  
  11.      {  
  12.           sleep(3);  
  13.            
  14.           // notifice  
  15.           pid_t pid;  
  16.           if((pid = fork()) == 0)  
  17.           {  
  18.                // 子进程继承父进程的文件描述符  
  19.                // 在daemon_init中,父进程的stdout被重定向到/dev/null  
  20.   
  21.                close(1);  
  22.                open("/dev/pts/0", O_WRONLY);  
  23.                 
  24.                // exec  
  25.                execl("/home/alex_my/Code/UNP/example_daemon_help""example_daemon_help""3 sec now", nullptr);  
  26.                // 也可以使用这个  
  27.                // execl("/bin/echo", "echo", "Clock 3", nullptr);  
  28.           }  
  29.            
  30.           // parent  
  31.           waitpid(pid, nullptr, 0);  
  32.      }  
  33.   
  34.      return 0;  
  35. }  

可以将daemon_init放在这个文件中。
编译:
g++ -o example_daemon example_daemon.cc --std=c++11

有使用到nullptr,因此添上 --std=c++11,注意,等号两边不要空格

我是放在libhelpguy.a这个静态库文件中的。
编译:
g++ -o example_daemon example_daemon.cc -L. -lhelpguy --std=c++11

// 关于建立静态库请看
http://blog.csdn.net/alex_my/article/details/43529323

// 关于execl请看:
http://blog.csdn.net/alex_my/article/details/39374447
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值