2012-10-29 74 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()以帮助您处理程序。