守护进程

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<fcntl.h>
 5 #include<unistd.h>
 6 #include<sys/wait.h>
 7 #include<sys/types.h>
 8 #include<sys/stat.h>
 9 #define MAXFILE 65535
10 int main(){
11         pid_t pc;
12         int i,fd,len;
13         char *buf="this id a Demeon!
";
14         len = strlen(buf);
15         pc = fork();
16         if(pc<0){
17                 printf("error fork!
");
18                 exit(0);
19         }else if(pc>0){
20                 exit(0);
21         }
22         setsid();
23         chdir("/");
24         umask(0);
25         for(i = 0; i < MAXFILE; i++){
26                 close(i);
27         }
28         while(1){
29                 if((fd =open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
30                         perror("open");
31                         exit(0);
32                 }
33                 write(fd,buf,len+1);
34                 close(fd);
35                 sleep(10);
36         }
37         return 0;
38 }
原文地址:https://www.cnblogs.com/--lr/p/11290061.html