性能-多线程或多进程应用程序 [英] performance - multithreaded or multiprocess applications

查看:103
本文介绍了性能-多线程或多进程应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Linux上开发高度网络密集型服务器应用程序,首选哪种架构?这个想法是,该应用通常可以在具有多个内核(虚拟或物理)的机器上运行.考虑到性能是关键标准,采用多线程应用程序还是采用多进程设计是更好的选择?我确实知道资源共享和从多个进程访问此类资源的同步是很多编程开销,但是如前所述,整体性能是关键要求,因此我们可以忽略这些事情.编程语言将是C/C ++.

In order to develop a highly network intensive server application on linux, what sort of architecture is preferred? The idea is that this app would typically run on machines with multiple cores (either virtual or physical). Considering that performance is the key criteria, is it better to go for a multi-threaded application or the one with multi-process design? I do know that sharing of resources and synchronization to access of such resources from multiple processes is a lot of programming overhead, but as mentioned earlier overall performance is the key requirement and so we can ignore those things. And the programming language would be C/C++.

我听说,即使多线程应用程序(单个进程)也可以利用多个内核,并在不同的内核上独立运行每个线程(只要没有同步问题).而此调度是由内核完成的.如果是这样,那么多线程应用程序和多进程应用程序之间的性能没有太大区别吗? Nginx使用多进程架构,而且速度很快,但是使用多线程应用程序可以获得相同的性能吗?

I have heard that even the multi-threaded applications (single process) can take advantage of multiple cores and run each thread on a different core independently (as long as there is no sync issues). And this scheduling is done by the kernel. If so, is there not much difference in performance between multi-threaded applications and multi-process applications? Nginx uses a multi-process architecture and is really quick, but can one get the same performance with multi-threaded applications?

谢谢.

推荐答案

Linux上的进程和线程彼此非常相似-主要区别在于整个虚拟内存是共享的,并且某些事情(例如信号处理)也有所不同.

Processes and threads on linux are very similar to each other - the main difference is that the whole virtual memory is shared as well as certain things like signal handling differ.

这样可以在线程之间进行更便宜的上下文切换(不需要昂贵的MMU重新加载等),但并不一定会导致速度差异很大(尤其是在线程创建之外).

This makes for cheaper context switches between threads (no need for costly MMU reloads etc.) but doesn't necessarily cause much difference in speed (especially outside of thread creation).

对于设计高度网络密集的应用程序,基本上 only 解决方案基本上是使用事件架构(否则,您将使系统陷入大量进程/线程的泥潭,并在它们上花费更多的时间)管理,而不是实际运行的工作代码),您可以在其中对套接字上的I/O做出反应,并根据哪个套接字表现出活动来进行适当的操作.

For designing a highly network intensive application, basically the only solution is to use an evented architecture (otherwise you'll bog down the system with huge amount of processes/threads and spend more time on their management than actually running work code), where you react to I/O on sockets and based on which sockets exhibit activity do apropriate operations.

有关此类情况下面临的问题的著名文章是"C10k问题" ,可从 http://www.kegel.com/c10k.html -它描述了不同的I/O方法,因此尽管有些陈旧,但这还是一个很好的介绍.

A famous writeup about the problems faced in such situations is "The C10k problem", available from http://www.kegel.com/c10k.html - it describes different I/O approaches, so despite being a bit dated, it's a very good introduction.

不过,在深入进入类似反应堆的设计之前要小心-它们会变得笨拙和复杂,因此请查看您是否不能使用能够提供更好抽象的库/语言(Erlang是我个人最喜欢的,具有协程的语言(例如Go)也可能有用).

Be careful before jumping deeply into reactor-like designs, though - they can get unwieldy and complex, so see if you can't use library/language that provides a nicer abstraction over it (Erlang is my personal favourite in this, languages with coroutines like Go can be useful too).

这篇关于性能-多线程或多进程应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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