Comparison between threads and processes

Source: Internet
Author: User
Fish or bear's paw: multi-process and multi-thread Selection
Author: yah99wolf
(1 Article)
Date: October 13, 2010 at AM

With regard to multi-process and multi-thread, the most typical sentence in the textbook is "process is the smallest unit of resource allocation, and thread is the smallest unit of CPU scheduling". This sentence is enough for the exam, however, if you encounter a similar choice problem at work, it is not that easy. Poor selection will cause you to suffer.
I often see some xdjm on the network and ask "is multi-process good or multi-thread good ?" "Is multi-process or multi-thread used in Linux ?" I can only say that there is no best, but better. Determine which one is more appropriate and which one is better.
Let's look at the comparison of multithreading and multi-process based on multiple different dimensions (Note: because it is a perceptual comparison, it is relative, not to say it is too good, another difference is intolerable ).

It looks simple. The advantage is that the "thread 3.5 V 2.5 process" is compared. Do we just select the thread?
Well, I don't have to waste a bit of effort here for such a simple sentence. There is no such thing as good or bad, and there is only one more suitable question. Let's take a look at how to make a proper decision in practical applications.
1) priority threads that require frequent creation and destruction
For the reason, see the comparison above.
The most common application of this principle is the Web server. When a connection is established to create a thread, the thread will be destroyed when the thread is disconnected. If a process is used, the cost of creation and destruction will be hard to bear.
2) priority threads that require a large amount of Computing
The so-called large amount of computing, of course, consumes a lot of CPU and frequent switching. In this case, the thread is the most suitable.
The most common principles are image processing and algorithm processing.
3) strongly related processing threads and weak related processing processes
What is strong correlation or weak correlation? Theoretically, it is difficult to define. Just give a simple example to understand.
Generally, the server needs to complete the following tasks: Message sending and receiving and message processing. "Message sending and receiving" and "message processing" are weak-related tasks, and "message processing" may be divided into "Message decoding ",
"Business processing", these two tasks are relatively more relevant. Therefore, "message sending and receiving" and "message processing" can be designed by process, and "Message decoding" and "service processing" can be designed by thread.
Of course, this division method is not static and can be adjusted according to the actual situation.
4) process may be extended to multi-host distribution, and threads used for multi-core Distribution
For the reason, see the comparison above.
5) use the method you are most familiar with to meet your needs.
I can only say how to choose between the "complex and simple" dimensions of "data sharing, synchronization", "programming, debugging", and "reliability: there is no clear choice method. But I can tell you a selection principle: if both multi-process and multi-thread can meet the requirements, select the one you are most familiar with and best at hand.
It should be noted that although I have given so many selection principles, the actual application is basically a combination of "process + thread". Do not really fall into a misunderstanding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.