日期:2014-05-16 浏览次数:20737 次
void init_daemon( void )
{
int pid;
int i;
if( pid = fork() )
exit( 0 );
else if( pid < 0 )
exit( 1 );
setsid();
if( pid = fork() )
exit( 0 );
else if( pid < 0 )
exit( 1 );
for( i = 0; i < NOFILE; i++ )
close( i );
chdir( "/tmp" );
umask( 0 );
}
int main( void )
{
FILE *fp;
time_t tt;
init_daemon();
while( 1 ) {
sleep( 5 );
if( fp = fopen("test.log", a) ) {
tt = time( 0 );
fprintf( fp, "I'm here at %s\n", asctime(localtime(&tt)) );
fclose( fp );
}
}
}