当前位置:网站首页>The concept of multiprocess and Multithread

The concept of multiprocess and Multithread

2022-06-21 22:36:00 Jida qinshaoyou

One 、 What is multithreading ?

Speaking of multithreading , So we have to say what is thread , And when it comes to threads , I have to say what is a process .

Concept

A process can be simply understood as a program unit that can run independently , It's a collection of threads , A process consists of one or more threads . The thread is the actual running unit in the process , It is the smallest unit of operation scheduling in the operating system . It can be understood that a thread is the smallest running unit in a process .

So multithreading is easy to understand : Multithreading means that multiple threads are executing in a process at the same time .

Why use multithreading ?

  • In a program , There are many operations that are very time-consuming , Such as database read and write operations ,IO Operation etc. , If you use a single thread , Then the program is a sequential operation , You must wait for these operations to complete before you can perform other operations . Using multithreading , You can continue to execute time-consuming tasks in the background while , Perform other operations at the same time .
  • Can improve the efficiency of the program .
  • On some waiting tasks , Such as user input , File reading, etc , Multithreading is very useful .

The disadvantage of multithreading

  • Using too many threads , It consumes a lot of system resources , Because threads need to open up memory . More threads require more memory .
  • Affect system performance , Because the operating system needs to switch back and forth between threads .
  • We need to consider the impact of thread operation on the program , If thread is suspended , The effect of suspension and other operations on the program .
  • Improper use of threads can cause many problems .

summary : Multithreading is asynchronous , But this does not mean that multithreading is really several threads at the same time , In fact, the system constantly switches back and forth between threads ( Because the system switching speed is very fast , So it gives us the illusion of running at the same time ).

Two 、 What is multi process ?

A process is an execution of a program on a computer . When you run a program , You start a process . All processes used to complete various functions of the operating system are system processes , All processes that you start are user processes .

Empathy , Multi process means that the computer executes multiple processes at the same time , It's usually running multiple software at the same time .

3、 ... and 、 Multithreading and multiprocessing , Choose who ?

The following is what I know from -pansz An answer reproduced on , He answered this question in a very popular way .

 Single process single thread : Eating alone on a table .
 Single process multithreading : Many people eat together on the same table .
 Multi process single thread : Many people, each eating at his own table .

The problem of multithreading is that when multiple people eat a dish at the same time, it is easy to compete , For example, two people have a dish at the same time , A man just stretched out his chopsticks , As a result, the vegetables had been taken away when they reached ... At this point, you have to wait for someone to take a bite , While serving food to another person , in other words Resource sharing conflicts will occur .

1. about Windows system ,【 Open the table 】 It costs a lot , therefore Windows Encourage everyone to eat at one table . therefore Windows The key point of multithreading learning is to face a lot of problems in resource contention and synchronization .

2. about Linux system ,【 Open the table 】 It's a small expense , therefore Linux Encourage everyone to open their own table as much as possible . The new problem is : Sit on two different tables , Inconvenient to speak . therefore ,Linux We should learn the method of inter process communication .

Opening a table means creating a process . Cost here mainly refers to time cost .
You can do an experiment : Create a process , Write some data to memory in the process , Then read the data , And then quit . The process repeats 1000 Time , It's equivalent to creating / Destruction process 1000 Time . The test result on my machine is :
UbuntuLinux: Time consuming 0.8 second Windows7: Time consuming 79.8 second The difference between the two costs is about onehundredfold .
It means , stay Windows in , The cost of process creation cannot be ignored . In other words ,Windows It is not recommended that you create a process in programming , If your program architecture requires a lot of process creation , Then it is better to switch to Linux System .

There are two typical examples of mass process creation , One is gnu autotools Tool chain , For compiling many open source code , They are Windows The compilation speed will be very slow , Therefore, it is best for software developers to avoid using Windows. The other is the server , Some server frameworks rely on a large number of creation processes to work , Even create a process for each user request , These servers are in Windows The efficiency will be very poor . this " Probably " It is also a world-wide view ,Linux There are far more servers than Windows The reason for the server .

Replenish again : If you are writing server-side applications , In fact, under the current network service model , The cost of opening a table is negligible , Because now it is generally popular to follow CPU The number of cores opens processes or threads , After the opening, the quantity has been maintained , Processes and threads use concurrent or asynchronous communication to handle multiple concurrent connections , Therefore, the overhead of starting processes and threads can be ignored .

Another new kind of expense is put on the agenda : Core switching overhead . Modern systems , commonly CPU There will be multiple cores , Multiple cores can run multiple different threads or processes at the same time . When each CPU When the core runs a process , Because the resources of each process are independent , therefore CPU There is no need to consider context when switching between cores . When each CPU When the core runs a thread , Because each thread needs to share resources , So these resources must come from CPU One core of is copied to another core , To continue the operation , This takes up extra overhead . let me put it another way , stay CPU In the case of multicore , Multithreading is not as good as multiprocessing in performance .
thus , In the current multi-core oriented server-side programming , You need to get used to multiprocessing rather than multithreading .

Four 、 parallel 、 Concurrent 、 Concepts such as high concurrency

parallel : Multiple CPU Instance or multiple machines execute a section of processing logic at the same time , At the same time .

Concurrent : adopt CPU Scheduling algorithm , Let the user appear to execute at the same time , actually CPU The operational level is not really simultaneous .

If public resources are operated during concurrency , There may be thread safety issues .

Thread safety : Multiple threads operate on common resources , There may be safety problems .

High concurrency : High concurrency refers to a problem encountered during system operation “ A large number of operation requests are encountered in a short time ” The situation of , Mainly occurs in web A large number of accesses or socket Received a large number of requests for port centralization ( for example :12306 The ticket grabbing situation of ; Tmall double 11 activities ). The occurrence of this situation will cause the system to perform a large number of operations during this period , For example, requests for resources , Database operation, etc . If high concurrency is not good , Not only does it reduce the user experience ( Request response time is too long ), At the same time, it may lead to system downtime , It can even lead to OOM abnormal , The system stops working, etc . If you want the system to be able to adapt to high concurrency state , We need to optimize the system from all aspects , Include , Hardware 、 The Internet 、 System architecture 、 Selection of development language 、 The use of data structures 、 Algorithm optimization 、 Database optimization …….

Multithreading and high concurrency

Multithreading is just at the same time / One of the ways to solve the high concurrency problem from the asynchronous point of view , It's a way of using idle computer resources at the same time .

The role of multithreading in high concurrency is to make full use of computer resources , Make the resources of the computer reach the maximum utilization rate at every moment , So as not to waste computer resources and make them idle .

原网站

版权声明
本文为[Jida qinshaoyou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212039544312.html