一个daemon的问题?
acqy 2003-12-19 02:40:46 在我的应用程序中是使用下面的方法使程序成为DAEMON的,可是运行后并没有在后台,用ps -ef看不到这个进程,为什么?
int ___g_init_daemon_FUNCTION (const char *path)
{
pid_t pid;
// if this is the main process
// then stop it
pid = fork();
if (pid > 0)
exit(1);
// if fork invoke failed
else if (pid < 0)
return RETURN_ERR;
// set the process to be
// the leader of the session
setsid ();
// fork again
pid = fork();
if (pid > 0)
exit(2);
else if (pid < 0)
return RETURN_ERR;
for (int i=0; i<NOFILE; i++)
close (i);
// set permission
chdir (path);
umask (0);
return RETURN_OK;
}