- 论坛徽章:
- 0
|
在循环中处理事务,每次开启一个进程或者线程,处理后,将子进程关闭(进程),或者线程退出。
但是这样比较耗费资源,于是有了线程池来创建和回收。
疑问是,为何一些服务器会不停的forek() 子进程,比如下面:- for(;;)
- {
- if ( (connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &cliaddr_len)) == -1 )
- {
- if (errno == EAGAIN || errno == EWOULDBLOCK)
- ;
- else
- perror("accept error");
- continue;
- }
- if (fork() == 0)
- {
- if (recv(connfd, buff, MAXLEN, 0) == -1)
- {
- perror("recv error");
- close(connfd);
- exit(0);
- }
- }
- if (close(connfd) == -1)
- {
- perror("close error");
- continue;
- }
- waitpid(-1, NULL, WNOHANG);
- }
复制代码 1.这样的资源耗费高吗?(当然我们可以用select或者epoll,这只是一个例子)
2.一些启动于后台的程序也是if (fork()) exit(0); 这么做又是为什么呢?搞不懂,还望指教。
谢谢 |
|