与在网络/IO绑定的应用程序的进程相比,在Linux上工作时的线程限制是什么? [英] What are the thread limitations when working on Linux compared to processes for network/IO-bound apps?

查看:82
本文介绍了与在网络/IO绑定的应用程序的进程相比,在Linux上工作时的线程限制是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在多核服务器上的Linux下,只有1个进程但有多个线程是不可能达到最高性能的,因为Linux在IO上有一些限制,因此1个进程在8核上有8个线程服务器可能比8个进程慢.

I've heard that under linux on multicore server it would be impossible to reach top performance when you have just 1 process but multiple threads because Linux have some limitations on the IO, so that 1 process with 8 threads on 8-core server might be slower than 8 processes.

有何评论?还有其他限制可能会使应用程序变慢吗? 这些应用程序是一个网络C ++应用程序,可为数百个客户端提供服务,并带有一些磁盘IO.

Any comments? Are there other limitation which might slow the applications? The applications is a network C++ application, serving 100s of clients, with some disk IO.

更新:我担心除了我自己实现的锁定以外,还有更多与IO相关的问题...在多个线程中同时进行网络/磁盘IO时没有任何问题吗?

Update: I am concerned that there are some more IO-related issues other than the locking I implement myself... Aren't there any issues doing simultanious network/disk IO in several threads?

推荐答案

线程的缺点

线程:

  • 序列化内存操作.那就是内核,因此MMU必须为诸如mmap()之类的用于执行页面分配的操作提供服务.
  • 共享相同的文件描述符表.该表中涉及锁定,包括更改和执行查找,该表存储文件偏移量和其他标志之类的内容.使用此表的每个系统调用(例如open()accept()fcntl())都必须将其锁定以将fd转换为内部文件句柄以及进行更改.
  • 共享一些计划属性.不断评估流程以确定它们施加在系统上的负载,并据此计划.大量线程意味着更高的CPU负载,而调度程序通常不喜欢这样做,这会增加对该进程的事件的响应时间(例如,读取套接字上的传入数据).
  • 可能共享一些可写的内存.由多个线程写入的任何内存(如果需要花式锁定,则特别慢),将生成各种缓存争用和处理问题.例如,诸如malloc()free()之类的堆操作在全局数据结构上进行操作(在某种程度上可以解决).还有其他全球结构.
  • 共享凭据,这可能是服务类型流程的问题.
  • 共享信号处理,这些将在处理信号时中断整个过程.
  • Serialize on memory operations. That is the kernel, and in turn the MMU must service operations such as mmap() that perform page allocations.
  • Share the same file descriptor table. There is locking involved making changes and performing lookups in this table, which stores stuff like file offsets, and other flags. Every system call made that uses this table such as open(), accept(), fcntl() must lock it to translate fd to internal file handle, and when make changes.
  • Share some scheduling attributes. Processes are constantly evaluated to determine the load they're putting on the system, and scheduled accordingly. Lots of threads implies a higher CPU load, which the scheduler typically dislikes, and it will increase the response time on events for that process (such as reading incoming data on a socket).
  • May share some writable memory. Any memory being written to by multiple threads (especially slow if it requires fancy locking), will generate all kinds of cache contention and convoying issues. For example heap operations such as malloc() and free() operate on a global data structure (that can to some degree be worked around). There are other global structures also.
  • Share credentials, this might be an issue for service-type processes.
  • Share signal handling, these will interrupt the entire process while they're handled.
  • 如果要简化调试,请使用线程.
  • 如果您使用的是Windows,请使用线程. (Windows中的进程非常繁重).
  • 如果稳定性非常重要,请尝试使用流程. (仅需一个SIGSEGV/PIPE.)
  • 如果线程不可用,请使用进程. (现在还不常见,但是确实发生了.)
  • 如果您的线程共享无法在多个进程中使用的资源,请使用线程. (或提供IPC机制以允许与资源的所有者"线程进行通信).
  • 如果您使用的资源仅基于每个进程一个(而每个上下文仅一个),那么显然使用进程.
  • 如果您的处理上下文绝对不共享任何内容(例如,套接字服务器在accept()连接时生成并忘记了连接),而CPU是瓶颈,则应使用进程和单线程运行时(它们不存在任何种类)诸如在堆上和其他地方的强烈锁定).
  • 线程和进程之间最大的区别之一是:线程使用软件构造来保护数据结构,进程使用硬件(显着地更快).
  • If you want to make debugging easier, use threads.
  • If you are on Windows, use threads. (Processes are extremely heavyweight in Windows).
  • If stability is a huge concern, try to use processes. (One SIGSEGV/PIPE is all it takes...).
  • If threads aren't available, use processes. (Not so common now, but it did happen).
  • If your threads share resources that can't be use from multiple processes, use threads. (Or provide an IPC mechanism to allow communicating with the "owner" thread of the resource).
  • If you use resources that are only available on a one-per-process basis (and you one per context), obviously use processes.
  • If your processing contexts share absolutely nothing (such as a socket server that spawns and forgets connections as it accept()s them), and CPU is a bottleneck, use processes and single-threaded runtimes (which are devoid of all kinds of intense locking such as on the heap and other places).
  • One of the biggest differences between threads and processes is this: Threads use software constructs to protect data structures, processes use hardware (which is significantly faster).
  • pthreads(7)
  • About Processes and Threads (MSDN)
  • Threads vs. Processes

这篇关于与在网络/IO绑定的应用程序的进程相比,在Linux上工作时的线程限制是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆