引用:#include
#include
#include
#include
#include
void init_daemon(void){
pid_t pid;
int i;
if((pid=fork())==-1){exit(1);}
if(pid>0){exit(0);}
setsid();
if((pid=fork())==-1){exit(1);}
if(pid>0){exit(0);}
for(i=0;ichdir("/tmp");
umask(0);
return;
}
2.test.c程序清单:
引用:#include
#include
#include
void init_daemon(void);
main(){
FILE *fp;
FILE *p;
char buf[256];
init_daemon();
while(1){
sleep(30);
if((fp=fopen("test.log","a"))>=0){
p=popen("ps -ef","r");
while(fgets(buf,sizeof(buf),p)!=0){fprintf(fp,"%s\n",buf);}
pclose(p);
}
fclose(fp);
}
}