首页 > 系统服务 > 详细

进程编程:守护进程(daemon)

时间:2017-03-19 16:06:22      阅读:194      评论:0      收藏:0      [点我收藏+]

技术分享

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/stat.h>
#define MAXFILE 65535
int main(){
    pid_t pc;
    int i,fd,len;
    char *buf = "this is a Daemon\n";
    len = strlen(buf);
    pc = fork(); /*第一步*/
    if(pc<0){
        printf("error fork\n");
        exit(1);
    }else if(pc>0){
        exit(0);
    }
    setsid(); /*第二步*/
    chdir("/"); /*第三步*/
    umask(0); /*第四步*/
    for(i=0;i<MAXFILE;i++) /*第五步*/
        close(i);
    while(1){
        if((fd=open("/tmp/daemon5.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
            perror("open");
            exit(1);
        }
        write(fd,buf,len);
        close(fd);
        sleep(10);
    }
    return 0;
}

进程编程:守护进程(daemon)

原文:http://www.cnblogs.com/ducong/p/6580339.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!