#include
#include
#include
#include
#include
#include
#include
#define MAXFILE 65535
int main()
{
pid_t pc;
int i,fd,len;
char *buf="this is a Dameon \n";
len=strlen(buf);
pc=fork();
if(pc<0)
{
perror("fork error!");
exit(1);
}
else if(pc>0)
{
exit(0);
}
setsid();
chdir("/");
umask(0);
for(i=0;i close(i);
while(1)
{
if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0)
exit(1);
write(fd,buf,len+1);
close(fd);
sleep(10);
}
}
[root@rhel5a domeon]# gcc -o daemon.o daemon.c
[root@rhel5a domeon]# ./daemon.o
[root@rhel5a domeon]# ps -ef|grep daemon.o
root 2931 1 0 15:48 ? 00:00:00 ./daemon.o
root 2935 2858 0 15:49 pts/2 00:00:00 grep daemon.o
[root@rhel5a domeon]# kill -9 2931
阅读(1209) | 评论(0) | 转发(0) |