Linux创建守护进程

来源:互联网 发布:检查网络用ping命令 编辑:程序博客网 时间:2024/05/19 12:24
#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;}

0 0
原创粉丝点击