Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between multiprocess and multithreading in linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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

Process: a program that is running (loaded into memory). The execution process is called a process.

Thread: a thread is a lightweight process, an execution sequence (a set of ordered instructions) in a 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 thread libraries are called function threads.

Comparison of dimensional multi-process multi-thread summary data sharing, synchronization

Data sharing is complex and requires IPC

The data is separate and the synchronization is simple.

Because sharing process data, sharing data is simple, at the same time, synchronization has its own advantages, CPU takes up more memory, switching is complex, speed is slow, CPU utilization is low, memory is low, switching is simple, CPU utilization is high, multi-thread advantage is to create and destroy, switch to create and destroy, switch is complex, slow to create and destroy, switch is simple, speed is simple, multi-thread advantage programming is simple, debugging is simple, debugging is complex. Debugging complex multi-process advantage reliability processes will not affect each other a thread hanging will cause the whole process to hang up multi-process advantage distribution is suitable for multi-core, multi-machine distribution If one machine is not enough, expanding to multiple machines is relatively simple and suitable for multi-core, distributed and multi-process advantages.

Advantages of multithreading:

The program logic and control mode 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.

It can minimize the impact of thread locking / unlocking and greatly improve performance.

Disadvantages of multithreading:

Each thread shares the address space with the main program, and the size is limited.

Synchronization and locking between threads is troublesome.

The collapse of a thread may affect the stability of the whole program.

After reaching a certain number of threads, even increasing CPU will not improve performance.

Disadvantages of multiple processes:

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

Need to cross the process boundary. If there is a big data transmission, it is not suitable.

The cost of multi-process scheduling is high.

The application is as follows:

1) frequent creation of destroyed threads is required

This principle is the most common is the Web server, to a connection to establish a thread, broken on the destruction of the thread. If you use a process, the cost of creating destruction is hard to bear.

2) priority threads that require a lot of computation

The so-called massive computing means consuming a lot of CPU and switching frequently, in which case threads are the most appropriate.

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

3) strong correlation processing thread, weak correlation processing process

What is strong correlation and weak correlation? It is difficult to define in theory, so give an example to explain.

General Server needs to complete the following tasks: message sending and receiving, message processing. "message sending and receiving" and "message processing" are weakly related processing, and "message processing" is divided into "message decoding" and "business processing". These two services are relatively much stronger. Therefore, "message sending and receiving" and "message processing" can be designed by process, and "message decoding" and "business processing" can be designed by thread.

4) it may be extended to multi-machine distributed processes and multi-core distributed threads (see table above for specific reasons)

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

Threads, who use the same address space and share most of the data, spend much less space to start a thread than it takes to start a process. and the time it takes for threads to switch from one to another is much less than the time it takes to switch between processes.

Mode of communication:

Data can only be transferred between processes by means of communication, which is time-consuming and inconvenient. Most of the thread time data is shared, fast and convenient, but data synchronization needs to be locked.

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

The above is about the content of this article on "what is the difference between multi-process and multi-threading in linux". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report