无详细内容 无 #include stdio.h#include unistd.h#include stdlib.h#include time.h#include fcntl.h#include string.h#include sys/stat.hvoid creat_daemon(void);int main(void){ time_t t; int fd; creat_daemon(); //创建daemon进程 while(1){ fd = ope
#include
#include
#include
#include
#include
#include
#include
void creat_daemon(void);
int main(void)
{
time_t t;
int fd;
creat_daemon(); //创建daemon进程
while(1){
fd = open("./daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);
if(fd == -1){
printf("open error!\n");
break;
}
t = time(0);
char *buf = "this is a test";
write(fd,buf,strlen(buf));
close(fd);
sleep(1);
}
return 0;
}
void creat_daemon(void)
{
pid_t pid;
pid = fork();
if( pid == -1){
printf("fork error!\n");
exit(EXIT_FAILURE);
}else if(pid > 0){ //父进程退出,子进程由init进程接管
exit(EXIT_SUCCESS);
}{//子进程
if(setsid() == -1){ //创建新的会话,脱离终端的控制
printf("setsid error!\n");
}
//子进程继承了父进程的工作目录,这里也要重置
//chdir("/home/chenlinzhong/trunk/demo/linenoise");
int i;
for( i = 0; i < 3; ++i)
{
close(i); //关闭 stdin stdout stderror
}
umask(0); //子进程继承了父进程文件掩码,需要重置
}
return;
}