概述

守护进程是在需要在后台长期运行不受终端控制的进程,通常情况下守护进程在系统启动时自动运行,在服务器关闭的时候自动关闭;守护进程的名称通常以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)   //parentexit(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/nulldup(0); //把0号文件描述符 赋值给空闲的文件描述符 1dup(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 rootif (chdir("/") < 0){err_quit("%s: can't change directory to /",cmd);}//close all open file descriptionif (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下一个简单守护进程的实现 (Daemon)

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

  2. 嵌入式linux系统下简单守护进程(daemon)的编写

    最近公司项目需要,需要在我们的嵌入式linux设备中创建一个守护进程,用于保护系统中的主进程,防止某些不可预期的意外导致主进程异常结束后,系统完全宕机没有任何反应,破坏用户体验感.但是,查阅诸多资料之 ...

  3. Linux系统编程---11(会话,守护进程,创建守护进程)

    会话 创建会话 创建一个会话需要注意以下6点注意事项 调用进程不能是进程组组长,该进程变成新会话首进程 该进程成为一个新进程组的组长进程 需要root权限(nbuntu不需要) 新会话丢弃原有的控制终 ...

  4. Linux系统的护花使者-----守护进程

    **************************************************************************************************** ...

  5. linux守护进程写法_Linux守护进程的深入理解

    守护进程也称为精灵进程,是生存期较长的一种进程,常常在系统自举时启动,仅在系统关闭时终止.没有控制终端,仅仅在后台运行,Linux有很多守护进程执行日常事务活动.是不受终端控制的进程.想要脱离所有终端 ...

  6. Linux -- 多进程编程之 - 守护进程

    内容概要 一.守护进程概述 二.守护进程创建 2.1.创建子进程,父进程退出 2.2.在子进程中创建新会话 2.2.1.进程组和会话期 2.2.2.setsid()函数说明 2.3.改变当前工作目录 ...

  7. linux的基础知识——守护进程

    文章目录 1.守护进程 2.创建守护进程模型 3.创建守护进程的步骤 4.举例:创建一个守护进程 5.守护进程随着系统启动而启动 1.守护进程 2.创建守护进程模型 3.创建守护进程的步骤 4.举例: ...

  8. 守护进程和inetd守护进程

    一.守护进程介绍 守护进程是在后台运行且不与任何控制终端相关联的进程.通常由系统初始化脚本启动,当然也可以在shell提示符下用命令行启动,不过这种守护进程必须亲自脱离于控制终端的关联. 守护进程的启 ...

  9. python守护进程_Python实现守护进程

    考虑如下场景:你编写了一个python服务程序,并且在命令行下启动,而你的命令行会话又被终端所控制,python服务成了终端程序的一个子进程.因此如果你关闭了终端,这个命令行程序也会随之关闭. 要使你 ...

最新文章

  1. linux进入probe目录,Linux基础学习系列——Linux文件目录操作命令
  2. 这是一篇优雅的Springboot2.0使用手册
  3. Android混合推送,MUI框架-推送配置核心代码-个推推送
  4. Android Service基本使用
  5. Android系列之网络(二)----HTTP请求头与响应头
  6. 【干货】Python中几个有趣的函数
  7. LNMP的403问题总结
  8. 生成式预训练之MASS
  9. [2019上海网络赛F题]Rhyme scheme
  10. 基于SSM的闲猫二手商城
  11. 包与四种权限访问修饰符
  12. uniapp 微信小程序 下拉刷新
  13. 人工智能标记语言AIML聊天机器人:产生、种类、应用、实例、AIML概述、知识库、公司、业界(20k字经典收藏版)
  14. 监控服务器系统密码忘了,监控服务器登录密码忘记了怎么办
  15. 魔兽世界单机(芒果3.3.5a)机器人操作命令大全
  16. google是否还能孤独求败,百度 VS google
  17. QT(5.12)+Qgis(3.10) 点图层及线图层渲染
  18. 头条等互联网公司面试准备、HR、Android技术等面试问题汇总
  19. 护卫神备份mysql_护卫神好备份系统数据库怎么备份?
  20. 清华大学计算机专业的cpu,我们研制成功进入世界500强的超级计算机

热门文章

  1. 946. 验证栈序列
  2. 玩转Spring——Spring IOC/DI
  3. 【原】Mysql存储关联数组
  4. 17-取石子-hdu1846(巴什博奕)
  5. 利用ssh-copy-id复制公钥到多台服务器
  6. 数据库自动备份脚本并删除前3天的备份
  7. 插件架构的原理及实现探讨
  8. border-collapse 关于继承问题
  9. 这本书人手一本,杠精能少97%?
  10. 中国离婚大数据:离婚/结婚比东北三省和四大直辖市霸榜