守护进程

来源:互联网 发布:德普达软件怎么使用 编辑:程序博客网 时间:2024/05/01 18:00
 

    一,创建守护进程
        步骤:
            1,在父进程中执行fork并且父进程exit。
            2,在子进程中调用setsid
            3,让/目录成为工作目录
            4,把子进程的umask变为0
            5,关闭多余的文件描述符
        为了消除控制终端,调用setsid创建一个新的会话。
            #include <unistd.h>
            pid_t setsid(void);
        setsid创建一个新的会话和进程组,然后守护进程会成为新的会话领导,以及信进程组的进程组领导。他还会保证新的会话没有控制终端,但是如果调用进程已经是一个进程组的领导进程,则调用会失败。setsid调用成功是会返回新的会话ID,失败返回-1,并设置errno。
    二,出错处理
        #include <syslog.h>
        void openlog(const char *ident, int option, int facility); /*打开日志 */
        void syslog(int priority, const char *format, ...);     /*写入消息*/
        void closelog(void); /*关闭日志*/
        ident是要向每条消息加入的字符串,典型的是程序的名字。option是下面值中的一个或多个相或的结果:
            LOG_CONS    如果系统日志服务器不能用,则写入控制台
            LOG_NDELAY  立即打开连接,正常情况下,直到发送第一条消息时才打开连接
            LOG_PERROR  打印输出到stderr
            LOG_PID     在每条消息中包含进程的PID
        facility用来指定程序发送消息的类型:
            LOG_AUTHPRIV    安全授权消息
            LOG_CRON    时钟守护进程cron和at
            LOG_DAEMON  其他系统守护进程
            LOG_KERN    内核消息
            LOG_LOCAL[0-7]  为本地使用而保留
            LOG_LPR     行打印机子系统
            LOG_MAIL    邮件子系统
            LOG_NEWS    Usenet新闻组子系统
            LOG_SYSLOG  syslogd产生的消息
            LOG_USER    默认
            LOG_UUCP    UUCP
        priority用来指明消息的重要性
            LOG_EMERG   system is unusable                 系统不能使用
            LOG_ALERT   action must be taken immediately   立即采取措施
            LOG_CRIT    critical conditions                紧急条件
            LOG_ERR     error conditions                   出错条件
            LOG_WARNING warning conditions                 警告条件
            LOG_NOTICE  normal, but significant, condition 正常,但重大事件
            LOG_INFO    informational message              信息消息
            LOG_DEBUG   debug-level message                调试信息
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <time.h>

#include <string.h>

 

void error(const char * str);

int main(void)

{

    pid_t pid, sid;

 

    if (0 > (pid = fork()))

        error("failed on fork\n");

   

    if (pid > 0)/* parent exit */

        exit(EXIT_SUCCESS);

    if (0 > (sid = setsid())) /*set sid*/

        error("failed on setsid\n");

    if (0 > chdir("/")) /* change direntory. */

        error("failed on chdir\n");

    umask(0); /* reset the file mode */

    close(STDIN_FILENO); /* close unneeded file */

    close(STDOUT_FILENO);

    close(STDERR_FILENO);

   

    { /*Our Own Work.*/

        int fd;

        time_t timebuf;

        int len = strlen(ctime(&timebuf));

        char buf[100];

 

        while (1)

        {

            if (0 > (fd = open("/var/log/test.log",

                O_CREAT | O_WRONLY | O_APPEND, 0600)))

                error("Failed on open\n");

            strcpy(buf, ctime(&timebuf));

            write(fd, buf, len);

            close(fd);

            sleep(30);

        }

    }

   

    return 0;

   

}

void error(const char * str)

{

    perror(str);

    exit(EXIT_FAILURE);

}

#include <syslog.h>

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

 

int main(void)

{

    pid_t pid, sid;

 

    if (0 > (pid = fork()))

    {

        syslog(LOG_ERR, "%s\n", "fork");

        exit(EXIT_FAILURE);

    }

    if (0 < pid)

        exit(EXIT_SUCCESS);

    openlog("test", LOG_PID, LOG_DAEMON);

 

    { /*START TO READ CONFIG FILE.*/

        int rcfd;

        char buf[256];

 

        rcfd = open("/etc/testing.conf", O_RDONLY);

        read(rcfd, buf, 256);

        close(rcfd);

        puts(buf);

    }

 

    if (0 > (sid = setsid()))

    {

        syslog(LOG_ERR, "%s\n", "setsid");

        exit(EXIT_FAILURE);

    }

    if (0 > chdir("/"))

    {

        syslog(LOG_ERR, "%s\n", "chdir");

        exit(EXIT_FAILURE);

    }

    umask(0);

    close(STDIN_FILENO);

    close(STDOUT_FILENO);

    close(STDERR_FILENO);

   

    {

        int fd;

        int i = 0;

        char buf[30];

       

        while (1)

        {

            sprintf(buf,"Testing... i = %d\n",i++);

            if (0 > (fd = open("/var/log/test.log",

                O_CREAT | O_WRONLY | O_APPEND, 0600)))

            {

                syslog(LOG_ERR, "open");

                exit(EXIT_FAILURE);

            }

            write(fd, buf, strlen(buf));

            close(fd);

            sleep(30);

        }

    }

   

    return 0;

}

原创粉丝点击