我目前正在爲一些項目編寫一個多進程客戶端和一個多線程服務器。Debian上的Daemonize()問題
該服務器是一個守護進程。 爲了實現這個目標,我使用下面的守護進程()代碼:
static void daemonize(void)
{
pid_t pid, sid;
/* already a daemon */
if (getppid() == 1) return;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* At this point we are executing as the child process */
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
/* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
/* Redirect standard files to /dev/null */
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
}
int main(int argc, char *argv[]) {
daemonize();
/* Now we are a daemon -- do the work for which we were paid */
return 0;
}
我有一個奇怪的副作用測試在Debian(Ubuntu的)服務器時。
的accept()函數總是失敗接受連接,將PID返回-1
我不知道是什麼造成這一點,因爲在redhat & CentOS的效果很好。
當我刪除對daemonize()的調用時,在Debian上一切正常,當我添加它時,同樣的accept()錯誤重現。
我一直在監視/ proc // fd,一切看起來不錯。
daemonize()和Debian發行版中的東西似乎不起作用。 (Debian GNU/Linux 5.0,Linux 2.6.26-2-286#1 SMP)
任何想法是什麼造成這種情況?
謝謝
在您的應用程序中運行strace -f,幷包含從啓動到第一個accept()失敗的所有內容,希望這些內容會包含一些線索。 – nos 2010-04-07 13:34:15
爲了更好地確定accept(2)返回-1的原因,打印/記錄errno的值可能會有幫助。 – jschmier 2010-04-08 06:08:12