【发布时间】:2011-05-11 08:45:37
【问题描述】:
我必须创建一个简单的客户端服务器通信以使用 C 语言 (Linux) 传输文件。
服务器接受10000端口的连接,我不知道是每个请求创建一个新线程还是创建一个固定数量的线程并使用异步技术更好。
CASE A:
client --> server --> (new thread) --> process the request
CASE B:
SERVER --> create thread 1 - thread 2 - thread 3
then
client1 --> server --> thread 1
client2 --> server --> thread 2
client3 --> server --> thread 3
client4 --> server --> thread 1
client5 --> server --> thread 2
client6 --> server --> thread 3
在这种情况下,线程 1 可以处理许多客户端的请求
我的考虑:
CASE 1:速度更快但浪费大量内存
CASE 2:速度较慢但内存不足
我错了吗?
【问题讨论】:
-
它应该取决于客户端的数量和使用率。通常,使用固定数量的线程。但是,如果您有很多性能要求而不升级硬件,请增加线程数。我相信,需要根据预期用途实现最佳性能/内存平衡
标签: c linux network-programming