简体   繁体   English

Linux中的线程/套接字限制

[英]Threads/Sockets limits in Linux

First of all: sorry for my english. 首先:对不起我的英语。 Guys, I have a trouble with POSIX sockets and/or pthreads. 伙计们,我在POSIX套接字和/或pthreads上遇到了麻烦。 I'm developing on embedded device(ARM9 CPU). 我正在嵌入式设备(ARM9 CPU)上进行开发。 On the device will work multithread tcp server. 在设备上将可以运行多线程tcp服务器。 And it will be able to process a lot of incoming connections. 而且它将能够处理很多传入连接。 Server gets connection from client and increase counter variable(unsigned int counter). 服务器从客户端获取连接并增加计数器变量(无符号int计数器)。 Clients routines will run in separate threads. 客户端例程将在单独的线程中运行。 All clients will use 1 singleton class instance(in this class will be opened and closed same files). 所有客户端将使用1个单例类实例(在该类中将打开和关闭相同的文件)。 Clients works with files, then client thread closes connection socket, and calls pthread_exit(). 客户端使用文件,然后客户端线程关闭连接套接字,并调用pthread_exit()。 So, my tcp server can't handle more than 250 threads(counter = 249 +1(server thread). And I got "Resource temporary unavailable". What's the problem? 因此,我的tcp服务器不能处理超过250个线程(计数器= 249 +1(服务器线程)。我得到了“资源临时不可用”。这是什么问题?

Whenever you hit the thread limit - or as mentioned run out of virtual process address space due to the number of threads - you're.... doing it wrong. 每当达到线程限制时-或由于线程数而导致虚拟进程地址空间用尽时-就是在做错了。 More threads don't scale. 更多线程无法扩展。 Especially not when doing embedded programming. 尤其是在进行嵌入式编程时。 You can handle requests on a thread pool instead. 您可以改为在线程池上处理请求。 Use poll(2) to handle many connections on fewer threads. 使用poll(2)在更少的线程上处理许多连接。 This is prettty well-trod territory and libraries (like ACE, asio) have been leveraging this model for good reason 这是一个颇有名气的地区,图书馆(例如ACE,asio)已经充分利用了该模型

The 'thread-per-request' model is mainly popular because of it's (perceived) simple design. “每个请求线程”模型主要是因为(设计)简单的设计而受到欢迎。

As long as you keep connections on a single logical thread (sometimes known as a strand ) there is no real difference, though. 只要您将连接保持在单个逻辑线程(有时称为绞线 )上,就没有真正的区别。

Also, if the handling of a request involves no blocking operations, you can never do better than polling and handling on a single thread after all: you can use the 'backlog' feature of bind/accept to let the kernel worry about pending connections for you! 同样,如果请求的处理不涉及任何阻塞操作,那么您毕竟比在单个线程上进行轮询和处理要好得多:您可以使用bind / accept的“ backlog”功能让内核担心挂起的连接您! (Note: this assumed a single core CPU, on a dual core CPU this kind of processing would be optimal with one thread per CPU) (注意:这假设是单核CPU,在双核CPU上,这种处理将是最佳的,每个CPU一个线程)

Edit Addition Re: 编辑附加内容:

ulimit shows how much threads can OS handle, right? ulimit显示OS可以处理多少个线程,对吗? If yes, ulimit does not solve my problem because my app uses ~10-15 threads in same time. 如果是,则ulimit不能解决我的问题,因为我的应用程序同时使用了约10-15个线程。

If that's the case, you should really double check that you are joining or detaching all threads properly. 如果是这样,您应该仔细检查您是否正确地连接分离了所有线程。 Also think of the synchronization objects; 还考虑同步对象; if you consistently forget to call the relevant pthread *_destroy functions, you'll run into the limits even without needing it. 如果您始终忘记调用相关的pthread * _destroy函数,那么即使不需要它,您也会遇到限制。 That would of course be a resource leak . 那当然是资源泄漏 Some tools may be able to help you spot them (vlagrind/helgrind come to mind) 一些工具也许可以帮助您发现它们(想到vlagrind / helgrind)

Use ulimit -n to check the number of file system handles. 使用ulimit -n检查文件系统句柄的数量。 You can increase it for your current session if the number is too low. 如果数量太少,可以增加当前会话的数量。

Also you can edit /etc/security/limits.conf and to set a permanent limit 您也可以编辑/etc/security/limits.conf并设置永久限制

Usually, the first limit you are hitting on 32-bit systems is that you are running out of virtual address space when using default stack sizes. 通常,在32位系统上遇到的第一个限制是使用默认堆栈大小时虚拟地址空间不足。

Try explicitly specifying the stack size when creating threads (to less than 1 MB) or setting the default stack size with "ulimit -s". 创建线程时(小于1 MB)或使用“ ulimit -s”设置默认堆栈大小时,尝试显式指定堆栈大小。

Also note that you need to either pthread_detach or pthread_join your threads so that all resources will be freed. 还要注意,您需要pthread_detach或pthread_join线程,以便释放所有资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM