Pros and cons of "go" multi-process and multithreading

Source: Internet
Author: User

programming in Linux is more multi-process programming and less multithreaded programming .

IBM has a guy who did a test and found that Windows is a lot faster than Linux when switching threads. In and out of the fastest locks (windows2k's critical section and Linux Pthread_mutex), Windows is about five times times faster than Linux. Of course, this is not to say that Linux is not good, and after the actual programming, in general I think Linux is more suitable for high performance server, but in the specific area of multithreading, Linux is less than Windows. This should be justifiable, after all, the Unix family is from a multi-process, and windows from the beginning is multi-threaded.

If the Unix/linux environment, the use of multithreading is not necessary.

Multithreading is higher than multi-process performance? Mislead!

It should be said that multithreading is less expensive than multi-process, but less performance .

In the UNIX environment, there is no significant difference between multi-process scheduling overhead and multithreaded scheduling overhead, that is, UNIX process scheduling efficiency is very high. In terms of memory consumption, both the global data area, now memory is very cheap, server memory is a few g, is not a problem at all.

Multi-process is a three-dimensional traffic system, although the cost is high, uphill downhill more oil, but not traffic jams.

Multithreading is a plane traffic system, low cost, but too many traffic lights, old traffic jams.

We are now open sports cars, oil (main frequency) Some, not afraid of uphill downhill, afraid of traffic jams.

High-performance transactional server middleware, such as Tuxedo, is a multi-process proposition. The actual test shows that tuxedo performance and concurrency efficiency are very high. Tuxedo is Bell Labs, and Unix ancestry, should be the most profound understanding of UNIX, their views should have a great reference significance.

====================================================================================================

Advantages of Multithreading:

There is no need for cross-process boundaries; The program logic and control methods are simple; All threads can share memory and variables directly, and threads consume total resources better than processes; Multithreading Disadvantages:

Each thread has a common address space with the main program, is limited to the 2GB address space, and the synchronization and lock control between threads is troublesome; a thread crash can affect the stability of the whole program; When you reach a certain number of threads, you cannot improve performance even if you increase the CPU, for example, Windows Server 2003, about 1500 or so the number of threads is almost the limit (thread stack set to 1M), if the set thread stack is 2M, also less than 1500 threads total, the thread can improve the total performance is limited, and after the thread is more, the thread itself is a problem of scheduling, Need to consume more CPU

Multi-process Benefits:

Each process is independent of each other, does not affect the stability of the main program, the child process crashes does not matter, by increasing the CPU, you can easily expand performance, can minimize the impact of line Cheng/unlock, greatly improve performance, even if the thread running module algorithm inefficient is not OK; each child process has a 2GB address space and related resources , the overall performance limit can be reached with very large multithreading drawbacks:

Logic control is complex, need to interact with main program, need cross-process boundary, if there is large data volume transmission, it is not good, suitable for small data transfer, dense operation multi-process scheduling overhead is relatively large; preferably multi-process and multi-threaded combination, that is, according to the actual needs of each CPU to open a sub-process, This sub-process turns on multithreading to handle several types of data. Of course you can also use multi-threaded + multi-cpu+ polling method to solve the problem ...

Methods and means are diverse, the key is that they seem to be easy to meet the requirements, the price is also appropriate.

---------------------------------------------------------

Advantages of the process:

1) The characteristics of sequential procedures: closed and reproducible;

2) concurrent execution of programs and sharing of resources. After the design of multi-channel program, the concurrent execution and resource sharing of the program are realized, which improves the efficiency of the system and the resource utilization of the system. Disadvantages of the process:

Operating system scheduling switching multiple threads is much faster than switching the scheduling process. And the process of memory can not be shared, communication is also more troublesome.

It is convenient to exchange data between threads because of the shared process memory space, and when a process is created or revoked, the overhead of the system is significantly greater than the cost of creating or revoking a thread, because the system allocates and reclaims resources for it.

Advantages of threading:

1) It is a very "frugal" multi-tasking operation. Under the Linux system, starting a new process must be assigned to its own address space, creating numerous data tables to maintain its code snippets, stack segments, and data segments, which is an "expensive" multi-tasking way of working. While running on multiple threads in a process that use the same address space, sharing most of the data, starting a thread is much less than the space it takes to start a process, and the time it takes to switch between threads is much less than the time it takes to switch between processes. Of course, on the specific system, this data may be a big difference;

2) The convenient communication mechanism between the threads, because the data space is shared between the threads in the same process, so the data of one thread can be used directly by other threads, which is not only fast but also convenient;

3) Make multi-CPU system more effective. The operating system will ensure that when the number of threads is not greater than the number of CPUs, different threads run on different CPUs;

4) Improve the program structure. A long and complex process can be considered to be divided into multiple threads and become a separate or semi-independent part of the run, which facilitates understanding and modification. Disadvantages of threading: 1. When scheduling, to save the thread state, frequent scheduling, the need to occupy a large number of machines; 2. Programming error Prone (thread synchronization issues).

Http://www.cnblogs.com/Yogurshine/p/3640206.html

Pros and cons of "go" multi-process and multithreading

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: info-contact@alibabacloud.com 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.