Linux?????????????????
???????????? ???????[ 2013/1/23 10:13:48 ] ????????
??????????????????
???????????????????????????????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??”I’m here at %s ”??asctime(localtime(&t)) );
fclose(fp);
}
}
}
?????????????RedHat Linux6.0?±???????????????£?
????????
$ gcc –g –o test init.c test.c
??У?./test
???????ps –ef
????????????????test???????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11