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
4

1 に答える 1

0

次のコード行の目的は何ですか:

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

子プロセスをすぐに終了させる必要がある場合は、プログラムをフォークしないでください。

また、プログラムを支援するために関数 setup_server() および scheduler() を投稿してください。

于 2013-01-24T11:30:11.230 に答える