The choice of multi-process and multi-thread

A process is the basic unit for the operating system to allocate resources (CPU time, memory), and a thread is the basic unit for scheduling execution. A thread must belong to a process, and a process can contain multiple threads.

Nginx and redis are common multi-process models, while Tomcat and memcached are multi-threaded models.

  1. Multi-process data sharing is complex and requires communication mechanisms such as pipes, signals, message queues, shared memory, sockets, etc., but data synchronization is simple, while multi-threads share process data segments, data sharing is simple, but synchronization is complex.
  2. Multi-process occupies a lot of memory, complicated to create, destroy and switch, and the CPU utilization rate is low, while multi-thread takes up less memory, and the creation, destruction, switching is simple, and the speed is fast.
  3. Multi-process programming and debugging is simple, and the processes will not affect each other. It is suitable for multi-core and multi-machine distribution. It is simple to expand to multiple machines, while multi-thread programming and debugging are complicated. One thread hangs up and may affect the entire process. It is suitable for multi-core distributed .

If you need to create and destroy frequently, need a lot of calculations, switch frequently, and use threads first when multi-core distribution is required, it is suitable for selecting processes when you need to be more stable and secure, and support multi-machine distribution.

Contrast dimension

multi-Progress

Multithreading

Summarize

Data sharing and synchronization

Data sharing is complex and requires IPC; data is separated and synchronization is simple

Because of the shared process data, data sharing is simple, but it is also because of this reason that synchronization is complex

Each has its own advantages

memory, CPU

Occupies a lot of memory, switching is complicated, and CPU utilization is low

Low memory usage, easy switching, and high CPU utilization

thread dominance

Create, destroy, switch

Creating and destroying, switching is complicated, and the speed is slow

Creating and destroying, switching is simple and fast

thread dominance

programming, debugging

Simple programming and easy debugging

Complex programming, complex debugging

process dominance

reliability

Processes do not affect each other

If a thread hangs, it will cause the whole process to hang

process dominance

distributed

It is suitable for multi-core and multi-machine distribution; if one machine is not enough, it is relatively simple to expand to multiple machines

Suitable for multi-core distributed

process dominance

https://blog.csdn.net/bv1315008634/article/details/53844430

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161359&siteId=291194637