???????? ??????????
???????????????????????????????test.c??????????init.c??????????????????/tmp??????е????test.log?????????????????????е?init_daemon???????????????????????????????init_daemon?????????????????????
????1?? init.c?嵥
#include < unistd.h >
#include < signal.h >
#include < sys/param.h >
#include < sys/types.h >
#include < sys/stat.h >
void init_daemon(void)
{
int pid;
int i;
if(pid=fork())
exit(0);//??????????????????
else if(pid< 0)
exit(1);//fork???????
//????????????????????
setsid();//???????????μ???鳤??????鳤
//?????????????
if(pid=fork())
exit(0);//??????????????????????
else if(pid< 0)
exit(1);//fork???????
//??????????????
//???????????????鳤
for(i=0;i< NOFILE;++i)//???????????д??????
close(i);
chdir("/tmp");//??乤??????е?/tmp
umask(0);//??????????????
return;
}
????2?? test.c?嵥
#include < stdio.h >
#include < time.h >
void init_daemon(void);//???????????????
main()
{
FILE *fp;
time_t t;
init_daemon();//??????Daemon
while(1)//??????????test.log?????????
{
sleep(60);//????????
if((fp=fopen("test.log"??"a")) >=0)
{
t=time(0);
fprintf(fp??"Im here at %s/n"??asctime(localtime(&t)) );
fclose(fp);
}
}
}
?????????????RedHat Linux6.0?±???????????????????£?
????????gcc -g -o test init.c test.c
??????У?./test
???????????ps -ef
????????????????test???????????????????????????