Linux守护进程
#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;
} 相关推荐
xh0xs 2020-03-16
sunln00 2019-12-17
zfyaixue 2013-09-16
playlinuxxx 2011-03-21
linuxisperfect 2011-09-09
viplinux 2017-04-02
ChasingChasing 2012-03-11
winskingli 2017-04-02
陈星的技术学习 2013-02-27
gongxucheng 2012-11-09
dudang0000 2012-09-10
andylanzhiyong 2011-09-19
傻小烨 2010-12-27
GodLong 2010-07-28
sunln00 2010-05-07
pointfish 2009-02-05
Summer的小屋 2019-07-01
PpikachuP 2019-06-27