我正在编写一个必须在守护程序模式下运行的多线程 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