标签:守护进程
本文,主要讲解守护进程的定义和相关的原理,并通过一个用户实例来说明如何编写守护进程。
?返回所在进程的文件描述符表的项数,即该进程打开的文件数目
code:
#include "stdio.h"
/* #include "type.h" */
#include <unistd.h>
#include <fcntl.h>
/* #include < sys/wait.h> */
#include <sys/types.h>
#include <string.h>
int main(int argc, char *argv[])
{
pid_t pid;
int i, fd;
char *buf = "This is a deamon program";
if( (pid = fork()) < 0){
printf("fork error!");
exit(1);
}
else if(pid >0){
exit(0);
}
/* setsid(); */
chdir("/");
umask(0);
for (int i = 0; i < getdtablesize(); ++i){
close(i);
}
while(1){
if( ( fd=open("/tmp/daemon.log", O_CREAT| O_WRONLY | O_APPEND, 0600)) < 0){
}
write(fd, buf, strlen(buf)+1);
close(fd);
sleep(10);
printf("never");
}
return 0;
}标签:守护进程
原文地址:http://blog.csdn.net/trochiluses/article/details/40076779