守护进程运用实例

1)守护进程可以直接在程序起始部分通过调用unistd中的daemon来将本进程设置为守护进程

2)可以用ps -axj | grep xx查看自己的守护进程启动情况

3)用kill可以杀死自己启动的守护进程

 

除上述直接调用,也可以自己编写自己想要的守护进程,下面是一个example,注意其中的fork两次,设置新会话setsid,更改目录到根目录chdir和重设文件权限掩码umask和关闭文件描述符: 

/// \brief closeAll close all FDs >= a specified value
///
/// \param fd
void closeAll(int fd){
    int fdlimit = sysconf(_SC_OPEN_MAX);

    while(fd < fdlimit ){
        close(fd++);
    }
}

/// \brief daemon detach process from user and disapper into the background
//returns -1 on failure, but you cannot do much except exit in that case
//since we may already have forked. this is based on the BSD version, so
//the caller is responsible for things like the umask, etc.
///
/// \param nochdir
/// \param noclose
///
/// \return
int daemon_own(int nochdir, int noclose){
 //   struct rlimit rl;
    struct sigaction sa;
    int fd0, fd1, fd2;

    umask(0);   /*clear file creation mask*/

    /*get maximum number of file descriptors*/
//    if(getrlimit(RLIMIT_NOFILE, &rl) < 0){
//#ifdef DEBUG
//        std::cout << "cannot get file limit" << std::endl;
//#endif
//        perror("getrlimit");
//    }

    /*Become a session leader to lose controlling TTY*/
    switch(fork()){
        case 0:  /*child*/
            break;
        case -1:
            return -1;
        default:   /*parent*/
            _exit(0);   /*exit the original process*/
    }

    if(setsid() < 0){   /*new session & new group, shouldn't fail*/
        return -1;
    }

    /*Ensure future opens won't allocate controlling TTYs*/
    sa.sa_handler = SIG_IGN;    /*ignore*/
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if(sigaction(SIGHUP, &sa, NULL) < 0){
#ifdef  DEBUG
        std::cout << "cannot ignore SIGHUP" << std::endl;
#endif
        perror("sigaction");
    }

    /*dyke out this switch if you want to acquire a control tty in
     * the future -- not normally advisable for daemons*/

    switch(fork()){
        case 0:
            break;
        case -1:
            return -1;
        default:
            _exit(0);
    }

    /*change the current working directory to the root so
     * we won't prevent file systems from being unmounted*/
    if(!nochdir)
        chdir("/");

    if(!noclose){
        closeAll(0);
        fd0 = open("/dev/null", O_RDWR);  /*#include <fcntl.h>*/
        fd1 = dup(0);
        fd2 = dup(0);
    }

    /*Initialize the log file*/
    openlog("test", LOG_CONS, LOG_DAEMON);
    if(fd0 != 0 || fd1 != 1 || fd2 != 2){
        syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2);
        exit(1);
    }
    return 0;
}

 

 

int main(int argc, char **argv){
    if((daemon_own(0, 0)) < 0){
#ifdef DEBUG
        cout << "daemon error from daemon(int, int)" << endl;
#endif
        perror("daemon");
        exit(2);
    }
process(); return 0; }

 

转载于:https://www.cnblogs.com/zlcxbb/p/6525840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值