如何让一个进程守护 [英] how to make a process daemon

查看:111
本文介绍了如何让一个进程守护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解我怎样才能使我的程序daemon.So有些事情我碰到的一般情况下,一个程序执行以下步骤成为守护程序:


  1. 呼叫叉()

  2. 在父,通话退出()。这保证了原始的父(守护进程的
    祖父母)信纳其子终止,守护进程的父无
    长时间运行,并守护进程不是一个进程组的领导者。最后一点
    是为下一步骤的成功完成的要求。


  3. 呼叫 setsid(),给守护一个新的进程组和会话,两者的
    它有它的领导者。这也确保了过程没有关联
    控制终端(如进程刚刚创建了一个新的会话,并不会指派
    一个)。


  4. 通过 CHDIR更改工作目录的根目录()。这个做完了
    因为继承工作目录可以在文件系统中的任何地方。
    守护程序往往为系统的正常运行期间运行,你不想
    让一些随机目录打开,从而$ ​​P $ pvent从管理员
    卸载包含该目录文件系统。


  5. 关闭所有文件描述符。


  6. 开启文件描述符0,1,2(标准,标准输出和标准错误)
    并重定向到的/ dev / null的

 的#include< SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&unistd.h中GT;
#包括LT&; Linux的/ fs.h文件>INT主要(无效)
{
    将为pid_t PID;
    INT I;    / *创建新进程* /
    PID =叉();
    如果(PID == -1)
        返回-1;
    否则如果(PID!= 0)
        出口(EXIT_SUCCESS);    / *创建新的会话和进程组* /
    如果(setsid()== - 1)
        返回-1;    / *设置工作目录到根目录* /
    如果(CHDIR(/)== - 1)
        返回-1;    / *关闭所有打开的文件 - NR_OPEN是矫枉过正,但工程* /
    对于(i = 0; I< NR_OPEN;我++)
        接近(ⅰ);    / *重定向FD的0,1,2到/ dev / null的* /
    打开(的/ dev / null的,O_RDWR);
    / *标准输入* /
    DUP(0);
    / * *标准输出/
    DUP(0);
    / * * stderror /    / *做的事情守护... * /    返回0;
}

能有人给我一个链接,例如Apache某些程序的现有源$ C ​​$ C,这样我可以理解更深入了这个过程。


解决方案

如果你正在寻找一个干净的方法,请考虑使用标准的API - INT守护进程(INT nochdir,诠释NOCLOSE); 。手册页pretty简单,自我解释。 手册页。一个行之有效的API远远超过我们自己的实现interms便携性和稳定性。

I am trying to understand how can I make my program a daemon.So some things which I came across are In general, a program performs the following steps to become a daemon:

  1. Call fork( ).
  2. In the parent, call exit( ). This ensures that the original parent (the daemon's grandparent) is satisfied that its child terminated, that the daemon's parent is no longer running, and that the daemon is not a process group leader. This last point is a requirement for the successful completion of the next step.

  3. Call setsid( ), giving the daemon a new process group and session, both of which have it as leader. This also ensures that the process has no associated controlling terminal (as the process just created a new session, and will not assign one).

  4. Change the working directory to the root directory via chdir( ). This is done because the inherited working directory can be anywhere on the filesystem. Daemons tend to run for the duration of the system's uptime, and you don't want to keep some random directory open, and thus prevent an administrator from unmounting the filesystem containing that directory.

  5. Close all file descriptors.

  6. Open file descriptors 0, 1, and 2 (standard in, standard out, and standard error) and redirect them to /dev/null.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>

int main (void)
{
    pid_t pid;
    int i;

    /* create new process */
    pid = fork ( );  
    if (pid == -1)  
        return -1;  
    else if (pid != 0)  
        exit (EXIT_SUCCESS);  

    /* create new session and process group */  
    if (setsid ( ) == -1)  
        return -1;  

    /* set the working directory to the root directory */  
    if (chdir ("/") == -1)  
        return -1;  

    /* close all open files--NR_OPEN is overkill, but works */  
    for (i = 0; i < NR_OPEN; i++)  
        close (i);  

    /* redirect fd's 0,1,2 to /dev/null */  
    open ("/dev/null", O_RDWR);  
    /* stdin */  
    dup (0);  
    /* stdout */  
    dup (0);  
    /* stderror */  

    /* do its daemon thing... */  

    return 0;  
}

Can some one give me a link to existing source code of some program like Apache so that I can understand this process in more depth.

解决方案

If you are looking for a clean approach please consider using standard api- int daemon(int nochdir, int noclose);. Man page pretty simple and self explanatory. man page. A well tested api far outweigh our own implementation interms of portability and stability.

这篇关于如何让一个进程守护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆