守护进程的创建与程序

创建一个守护进程,进程的内容为往一个文件里写入一句话,每隔一秒钟写入一句话

查看进程ps -x 

结束进程kill ??

#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXFILE 65535

int main()
{
        pid_t pid;
        FILE *fp;
        char *ch="this is a test \n";
        int i=0,len;
        len=strlen(ch);
        pid=fork();
        if(pid<0) printf("fork faile \n");
        else if(pid>0)
                exit(0);
        setsid();
        chdir("/");
        umask(0);
        for(i=0;i<MAXFILE;i++)
                close(i);

        while(1){
                if((fp=fopen("/tmp/dameon.log","ab"))<0){
                        perror("open");
                        exit(1);
                }
         }                                                     1,1          顶端
}

猜你喜欢

转载自blog.csdn.net/cxyzyywoaini/article/details/87278078