Before we start talking about processes and threads, one of the first concepts we need to understand is parallel processing, because when you understand parallel processing, your understanding of processes and threads will be much easier.
What is parallel processing
Literally, parallel processing means processing multiple tasks at the same time. For example, we need to calculate the values of the following two expressions:
A = 1 + 2 B = 2 + 3
From the perspective of code compilation, this process can be divided into three parts:
- Step 1: calculate a = 1 + 2
- Step 2: calculate B = 2 + 3
- Step 3: display the final calculation results
If we useSingle threadTo process, the browser will follow the above steps in three steps.
What is the final result of multithreading?
Multithreading only needs to be handled in two steps:
- Step 1: use two threads to perform the first two tasks at the same time
- Step 2: output results
What is the process?
First of all, a clear concept is that threads can not exist alone, and threads depend on processes.
Then what isprocessWhat about it?
A process is a running instance of a program.
The official explanation is that when starting a program, the operating system will create a piece of memory for the program to store code, running data and a main thread executing tasks. We call such a running environment a process.
Relationship between process and thread
If any thread in the process fails to execute, the whole process will crash
A = 1 * 0 B = 2 / 0
When the above program is calculating B, because the denominator is 0, it will cause execution error, because the thread execution error will cause the whole process to crash, once the process crashes, the execution of other threads will have no results.
Sharing process data between threads
Threads can operate on common data in the process.
When a process is shut down, the operating system reclaims the memory used by the process
When a process exits, the operating system will reclaim all the resources occupied by the process; even if any of the threads leaks memory due to improper operation, the occupied memory will be recycled correctly.
Content between processes is isolated from each other
Process isolation is to protect each process in the operating system from interfering with each other. Each process can only access its own data, so as to avoid the situation that process a writes data to process B.
At the same time, because the data between processes is isolated from each other, when a process crashes or hangs, it will not affect other processes.
However, there is no absolute isolation between processes. If data communication is needed between processes, then IPC is needed.
Multi process architecture of browser
The latest Chrome browser includes: 1 browser main process, 1 GPU process, 1 network process, multiple rendering processes and multiple plug-in processes.
Let’s talk about the main functions of these processes:
- Browser process.It is mainly responsible for page display, user interaction, sub process management and storage.
- GPU process.Chrome didn’t have a GPU process when it was first released. The original intention of GPU is to achieve 3D CSS effect, but then the UI of web page and chrome all choose to use GPU to draw, which makes GPU known as the common demand of browser. Finally, chrome introduced GPU processes on top of its multiprocess architecture.
- Network process.It is mainly responsible for the loading of web resources of the page. It was previously run as a module in the browser process, but it was only recently independent and became a separate process.
- Plug in process.It is mainly responsible for the running of plug-ins. Because plug-ins are easy to crash, they need to be isolated through plug-in processes to ensure that process crashes will not affect browsers and pages.
Problems caused by browser’s multi process architecture
Although the multi process model improves the stability, fluency and security of the browser, the same cloth inevitably brings some problems:
- More complex architecture.The problems of high coupling and poor scalability among browser modules will make the current architecture difficult to meet the needs.
Future oriented service architecture
In order to solve the problems brought by the current browser architecture, in 2016, the official chrome team designed a new chrome architecture using the idea of “service-oriented architecture”.
That is to say, the overall architecture of chrome will develop towards the “service-oriented architecture” adopted by the modern operating system. The original various modules will be reconstituted into independent services. Each service can run in an independent process. To access the service, you must use a defined interface to communicate through IPCBuild a more cohesive, loosely coupled, maintainable, and scalable systemTo better achieve the goal of simple, stable, high-speed and safe chrome.