概述

守护进程是在需要在后台长期运行不受终端控制的进程,通常情况下守护进程在系统启动时自动运行,在服务器关闭的时候自动关闭;守护进程的名称通常以d结尾,比如sshd、xinetd、crond、atd等。

守护进程编程规则

调用umask将文件模式创建屏蔽字设置为一个已知值(通常是0)

调用fork(),创建新进程,它会是将来的守护进程

然后使父进程exit,保证子进程不是进程组组长

调用setsid创建新的会话

会话:是一个或者多个进程组的集合,通常一个会话开始与用户登录,终止于用户退出。在此期间,该用户运行的所有进程都属于这个会话期。

将进程的当前目录改为根目录 (如果把当前目录作为守护进程的目录,当前目录不能被卸载,它作为守护进程的工作目录了。)

关闭不再需要的文件描述符

将标准输入、标准输出、标准错误重定向到/dev/null

setsid

pid_t setsid(void);

setsid() creates a new session if the calling process is not a process group leader.  The calling process is the leader of the new session, the process group  leader  of  the  new process  group,  and has no controlling terminal.  The process group ID and session ID of the calling process are set to the PID of the calling process.  The calling process  will be the only process in this new process group and in this new session.

/*当调用进程不是一个进程组的组长时,Setsid创建一个新的会话;调用者进程会是这个会话期唯一的一个进程,且是该进程组的组长;调用者进程id是组id,也是会话期的id。不能用进程组组长去调用setsid函数*/

//示例:
int main()
{
    pid_t pid = fork();
    if (pid == -1)
        err_exit("fork error");
    else if (pid != 0)
        exit(EXIT_SUCCESS);

//    //查看下面这一部分代码在注释的前后有什么变化
//    pid_t id = setsid();
//    if (id == -1)
//        err_exit("setsid error");
//    else
//        cout << "new session id = " << id << endl;

    cout << "getpid = " << getpid() << endl;
    cout << "getpgid = " << getpgid(getpid()) << endl;

    return 0;
}

RETURN VALUE

On  success,  the  (new)  session  ID  of  the  calling  process  is returned.  On error, (pid_t) -1 is returned, and errno is set to indicate the error.

Linux中的守护进程API

int daemon(int nochdir, int noclose);

参数:

nochdir:=0将当前目录更改至“/”

noclose:=0将标准输入、标准输出、标准错误重定向至“/dev/null”

DESCRIPTION

The  daemon()  function is for programs wishing to detach themselves from the controlling terminal and run in the background as system daemons. If nochdir is zero, daemon() changes the calling process's current working  directory  to the root directory ("/"); otherwise, the current working directory is left unchanged. If noclose is zero, daemon() redirects standard input, standard output and standard error to /dev/null; otherwise, no changes are made to these file descriptors.

//示例:自己动手写daemon函数(一个比较简单的示例)
bool myDaemon(bool nochdir, bool noclose)
{
    umask(0);

    pid_t pid = fork();
    if (pid == -1)
        err_exit("fork error");
    else if (pid != 0)   //parent
        exit(0);

    setsid();

    if (nochdir == 0)
        chdir("/");
    if (noclose == 0)
    {
        int i;
        for (i=0; i < 3; ++i)
            close(i);
        open("/dev/null", O_RDWR);  //相当于把0号文件描述符指向/dev/null
        dup(0); //把0号文件描述符 赋值给空闲的文件描述符 1
        dup(0); //把0号文件描述符 赋值给空闲的文件描述符 2
    }

    return true;
}

//测试
int main(int argc, char *argv[])
{
    myDaemon(0, 0); //0表示做出改变(当前目录,文件描述符),1表示不改变
    printf("test ...\n");
    while (true)
    {
        sleep(1);
    }
    return 0;
}


//一个比较经典和完善的实例;来自《APUE》
#include "apue.h"
#include <syslog.h>
#include <fcntl.h>
#include <sys/resource.h>

bool myDaemon(const char *cmd);

int main(int argc, char *argv[])
{
    myDaemon("xiaofang");
    while (true)
    {
        sleep(1);
    }

    return 0;
}

bool myDaemon(const char *cmd)
{
    umask(0);

    //Get maximum number of file descriptors.
    rlimit rlt;
    if (getrlimit(RLIMIT_NOFILE,&rlt) < 0)
    {
        err_quit("%s: can't get file limit",cmd);
    }

    //Become a session leader to lose controlling TTY.
    pid_t pid = fork();
    if (pid == -1)
    {
        err_quit("%s: can't fork",cmd);
    }
    if (pid != 0)   //parent
    {
        exit(0);
    }
    setsid();

    //Ensure future opens won't allocate controlling TTYs.
    struct sigaction sa;
    sa.sa_handler = SIG_IGN;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(SIGHUP,&sa,NULL) < 0)
    {
        err_quit("%s can't ignore SIGHUP",cmd);
    }
    if ((pid = fork()) < 0)
    {
        err_quit("%s: can't fork",cmd);
    }
    else if (pid != 0)  //Second Parent
    {
        exit(EXIT_SUCCESS);
    }

    //change the current working directory to the root
    if (chdir("/") < 0)
    {
        err_quit("%s: can't change directory to /",cmd);
    }

    //close all open file description
    if (rlt.rlim_max == RLIM_INFINITY)
    {
        rlt.rlim_max = 1024;
    }
    for (unsigned int i = 0; i < rlt.rlim_max; ++i)
    {
        close(i);
    }

    //Attach file descriptors 0, 1, and 2 to /dev/null.
    int fd0 = open("/dev/null",O_RDWR);
    int fd1 = dup(0);
    int fd2 = dup(0);

    //Initialize the log file.
    openlog(cmd,LOG_CONS,LOG_DAEMON);
    if (fd0 != 0 || fd1 != 0 || fd2 != 0)
    {
        syslog(LOG_ERR,"unexpected file descriptors %d %d %d",
        fd0,fd1,fd2);
        exit(EXIT_FAILURE);
    }

    return true;
}

Linux进程实践(5) --守护进程的更多相关文章

  1. Linux编程之《守护进程》

    Intro ----- 守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常 ...

  2. Linux 下Qt实现守护进程实例(转)

     原文地址:Linux守护进程的编程方法(含实例) 作者:lingdxuyan 参考文献 Linux信号列表(zz) Linux 守护进程的编程方法 linux上编写守护进程的例程 Linux下后台守 ...

  3. Linux进程托管与守护进程设置

    引言 在上一篇<Linux启动之旅>中,我们了解了Linux启动过程,在该过程的最后一步,init进程拉起/etc/init.d/rcN.d/目录下指定的守护进程(daemon).假若自定 ...

  4. Linux下一个简单守护进程的实现 (Daemon)

    在Linux/UNIX系统引导的时候会开启很多服务,这些服务称为守护进程(也叫Daemon进程).守护进程是脱离于控制终端并且在后台周期性地执行某种任务或等待处理某些事件的进程,脱离终端是为了避免进程 ...

  5. 一只简单的网络爬虫(基于linux C/C++)————守护进程

    守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常常在系统引导装入时启动, ...

  6. (七) 一起学 Unix 环境高级编程(APUE) 之 进程关系 和 守护进程

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  7. python 进程锁 生产者消费者模型 队列 (进程其他方法,守护进程,数据共享,进程隔离验证)

    #######################总结######### 主要理解 锁      生产者消费者模型 解耦用的   队列 共享资源的时候 是不安全的 所以用到后面的锁 守护进程:p.daem ...

  8. python开发 *进程数据隔离.守护进程,进程同步工具 * 180725

    进程数据隔离.守护进程,进程同步工具 一.进程之间的数据隔离: from multiprocessing import Process n=100 #主程序中变量n= def func(): glob ...

  9. Python 之并发编程之进程中(守护进程(daemon)、锁(Lock)、Semaphore(信号量))

    五:守护进程 正常情况下,主进程默认等待子进程调用结束之后再结束守护进程在主进程所有代码执行完毕之后,自动终止kill -9 进程号 杀死进程.守护进程的语法:进程对象.daemon = True设置 ...

随机推荐

  1. 在ubuntu上安装最新稳定版本的node及npm

    背景 通过ubuntu官方apt安装工具安装的node是最新LTS版本的,而本人是个有点强迫症的人,喜欢追求新的东西,也就是想方设法想要去安装最新版本的node,所以本文也就产生了,附上ubuntu安 ...

  2. [Matlab+C/C++] 读写二进制文件

    introduction 因为Matlab操作简单.方便,它被应用于很多领域:音频处理,图像处理,数值计算等.尽管MATLAB容易操作,但受限于他的语言解释机制,MATLAB的执行速度通常较低.C/C ...

  3. [CSDN_Markdown] 使用LaTeX写矩阵

    简介 LaTeX 的公式功能非常强大,一次性讲全不是件容易的事情.将LaTeX 的这些功能分成较小的相互独立的部分来讲,一方面方便大家单独查阅:另一方面,所有[CSDN_Markdown]相关的文章都 ...

  4. How to Collect Bne Log Files for GL Integrators

    In this Document   Goal   Solution APPLIES TO: Oracle General Ledger - Version 11.0 and laterInforma ...

  5. JobTracker,TaskTracker简述

    JobTracker 负责接收用户提交的作业,负责启动.跟踪任务执行.JobSubmissionProtocol是JobClient与JobTracker通信的接口.InterTrackerProto ...

  6. Dynamics CRM Trace Reader for Microsoft Dynamics CRM

    CRM中抓取日志的视窗工作叫做Diagnastics Tools For Dyanmics CRM,这个工具我们只是作为一个开关来用就不做多介绍了,日志生成后是个文本文档可读性是很差的,那就需要个视窗 ...

  7. Hive-RCFile文件存储格式

    在新建Hive表时,可以使用stored as rcfile来指定hive文件的存储方式为RCFile. 一.RCFile文件结构 下图是一个RCFile的文件结构形式. 从上图可以看出: 1)一张表 ...

  8. GDAL 2.0版本RPC校正速度测试

    GDAL2.0版本的更新日志中提到了对RPC校正的优化,今天测试了一下,发现提升的速度还是蛮快的,测试的数据是一个IRS-P5的数据. 单线程测试 首先使用一个线程进行测试,使用下面的批处理进行运行, ...

  9. MySQL 视图技术

    以前也只是知道数据库中有视图这么个概念,但是没有去深究,今天正好有时间,就来总结一下吧. 视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚拟存在的表.视图就像一个窗口(数据展示的窗口),通过 ...

  10. UNIX网络编程——epoll 系列函数简介、与select、poll 的区别

    前面博客<<UNIX环境高级编程--epoll函数使用详解>>有关于epoll函数的讲解. 一.epoll 系列函数简介 #include <sys/epoll.h> ...