#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int a,char *b[]){
pid_t pid;
pid = fork();
printf("hahah");
if(pid<0)
{
perror("");
_exit(1);
}
if(pid==0)
{
printf("this is a child process");
//set new session in a child process
setsid();
//change current director into root director
chdir("/");
//reset file permissions
umask(0);
//close opened file descriptor
int i,fd_w;
char buf[] = {"hello world \n"};
for(i=0;i<getdtablesize();i++)
{
close(i);
}
fd_w = open("/liupan_test.txt",O_CREAT | O_WRONLY | O_APPEND,0666);
if(fd_w<0)
{
perror("");
_exit(1);
}
while(1)
{
write(fd_w,buf,strlen(buf));
//close(fd_w);
sleep(3);
}
}
else
{
printf("this is a parent process");
_exit(0);
}
//return 0;
}
Linux创建守护进程
最新推荐文章于 2024-06-21 20:47:02 发布