Linux守护进程
Posted dhfly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ", getpid()); init_daemon(); while(1) { sleep(6); fp=fopen("hello.log","a"); if(fp>=0) { time(&t); fprintf(fp,"current time is:%s ",asctime(localtime(&t))); fclose(fp); } } return; }
以上是关于Linux守护进程的主要内容,如果未能解决你的问题,请参考以下文章