2012-10-29 73 views
0

我正在編寫一個多線程的Web服務器,它必須以守護進程模式運行。我編寫了代碼,但程序在守護進程模式下運行時崩潰。如果我不包含用於守護服務器的代碼,程序運行良好。有人可以告訴我我哪裏出錯了嗎?守護多線程服務器

pid_t pid,cid; 
pid = fork(); 
if(pid<0) 
{ 
exit(EXIT_FAILURE); 
} 
if(pid>0) 
{ 
    exit(EXIT_SUCCESS); 
} 
umask(0); 
cid=setsid(); 
std::cout<<"Process id after:"<<pid<<std::endl; 
std::cout<<"Session id:"<<cid<<std::endl; 
close(STDIN_FILENO); 
close(STDOUT_FILENO); 
close(STDERR_FILENO); 

pthread_t t1,t2; 
pthread_t threads[threadnum]; 
pthread_attr_t attr; 
if ((s = socket(AF_INET, soctype, 0)) < 0) { 
    perror("socket"); 
    exit(1); 
} 
pthread_attr_init(&attr); 
pthread_create(&t1,NULL,setup_server,NULL); // thread for accepting the requests 
pthread_create(&t2,NULL,scheduler,NULL);  // thread for scheduling the requests 
+1

不相關:它看起來不像[標準Unix守護進程](http://www.python.org/dev/peps/pep-3143/#correct-daemon-behaviour) – jfs

+0

這是一個deamonizing C++程序... – user1429322

+0

無論語言 – jfs

回答

0

什麼是下面的代碼行的目的:

if(pid>0) 
{ 
    exit(EXIT_SUCCESS); 
} 

如果您需要子進程立即退出,那麼就不要叉你的程序的。

此外,請發佈函數setup_server()和scheduler()以幫助您處理程序。