What is the difference between multi-process and multi-threading in linux

青灯夜游
Release: 2022-01-11 17:32:03
Original
5940 people have browsed it

Difference: 1. Data sharing in multiple processes is complex and synchronization is simple, while data sharing in multi-threads is simple and synchronization is complex; 2. Multiple processes occupy a lot of memory, complex switching, slow speed, and low CPU utilization. Multi-threading takes up less memory, is easy to switch, and has high CPU utilization; 3. Multi-process programming is simple and debugging is simple, while multi-threading is complicated to program and debug.

What is the difference between multi-process and multi-threading in linux

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

The difference between multi-process and multi-threading in Linux

Process: running (loaded into memory Procedure on). -->The execution process is called a process.

Thread: A thread is a lightweight process, an execution sequence (a set of ordered instructions) in the process, and a process has at least one thread.

The execution sequence represented by the main function is called the main thread. Threads created through the thread library are called function threads.

Contrast dimension Multiple processes Multiple threads Summary
Data sharing and synchronization

Data sharing is complex and requires IPC;

Data is separated, synchronization is simple

Because process data is shared, sharing data is simple, and synchronization is also complicated Each has its own advantages
Memory, CPU Occupies a lot of memory, complex switching, slow speed, low CPU utilization Occupies less memory, simple switching, high CPU utilization More Thread advantages
Creation, destruction, switching Creation and destruction, switching is complicated and slow Creation and destruction, switching is simple and fast Multi-threading advantages
Programming and debugging Simple programming, simple debugging Complex programming, complex debugging Multiple processes Advantages
Reliability Processes will not affect each other If one thread hangs up, the entire process will hang up Multiple Process advantages
Distributed Applicable tomulti-core, multi-machinedistributed; if one machine is not enough, it is relatively simple to expand to multiple machines Applicable toMulti-coreDistributed Multi-process advantages

Advantages of multi-threading:

  • No need to cross process boundaries;

  • The program logic and control method are simple;

  • All threads can share memory and variables directly;

  • The thread mode consumes less total resources than the process

Advantages of multiple processes:

  • Each process is independent of each other and does not affect the stability of the main program. It does not matter if the child process crashes;

  • Performance can be easily expanded by adding CPU;

  • can minimize the impact of thread locking/unlocking and greatly improve performance;

Disadvantages of multi-threading:

  • Each thread shares the address space with the main program and is limited in size;

  • Synchronization and locking between threads are troublesome;

  • The collapse of one thread may affect the stability of the entire program;

  • After reaching a certain number of threads, performance cannot be improved even if the CPU is increased;

Disadvantages of multiple processes:

  • The logic control is complex and needs to interact with the main program;

  • It needs to cross process boundaries and is not suitable if there is large data transmission;

  • Multi-process scheduling overhead is relatively large

The application situation is as follows:

1) Need to frequently create and destroy Use threads

The most common principle of this is the Web server. A thread is created when a connection is made, and the thread is destroyed when it is disconnected. If you use a process, the cost of creation and destruction is very difficult to bear.

2) Prioritize threads that need to perform a large amount of calculations

The so-called large amounts of calculations consume a lot of CPU and switch frequently. In this case, threads is the most suitable.

This principle is most commonly used in image processing and algorithm processing.

3) Threads are used for strong correlation processing, and processes are used for weak correlation processing

What are strong correlation and weak correlation? Theoretically it is very Difficult to define, give an example to explain.

A general server needs to complete the following tasks: message sending and receiving, and message processing. "Message sending and receiving" and "message processing" are weakly related processes, and "message processing" is divided into "message decoding" and "business processing". These two businesses are relatively much stronger. Therefore, "message sending and receiving" and "message processing" can be designed in separate processes, and "message decoding" and "business processing" can be designed in separate threads.

4) It may be extended to multi-machine distribution using processes and multi-core distribution using threads(please see the table above for specific reasons)

Consumption of resources:

From the kernel's point of view, the purpose of a process is to serve as the basic unit for allocating system resources (CPU time, memory, etc.). A thread is an execution stream of a process and the basic unit of CPU scheduling and dispatch. It is a basic unit that is smaller than a process and can run independently.

Threads use the same address space and share most of the data. The space taken to start a thread is much less than the space taken to start a process, and the time required to switch between threads is also Far less than the time spent switching between processes.

Communication method:

Data can only be transferred between processes through communication, which is time-consuming and inconvenient. Most of the thread time data is shared, which is fast and convenient, but data synchronization requires locks.

Advantages of threads:

Improve application response; use the CPU system more effectively;The operating system will ensure that when the number of threads is insufficient When the number of CPUs is greater than the number of CPUs, different threads run on different CPUs; to improve the program structure, a long and complex process can be divided into multiple threads and become several independent or semi-independent parts. Such a program Will be easy to understand and modify.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is the difference between multi-process and multi-threading in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!