山有木兮卿有意 2020-06-25
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid<0)
exit(1);
else if (pid>0)
exit(0);
setsid();
pid=fork();
if(pid>0)
exit(0);
else if (pid<0)
exit(1);
for(i=0; i<NOFILE; i++)
close(i);
chdir("/tmp");
umask(0);
return;
}
void main() //这是一个不断往文件里输入日期时间的程序
{
FILE *fp;
time_t t;
printf("pid = %d\n", getpid());
init_daemon();
while(1)
{
sleep(6);
fp=fopen("hello.log","a");
if(fp>=0)
{
time(&t);
fprintf(fp,"current time is:%s\n",asctime(localtime(&t)));
fclose(fp);
}
}
return;
}