The essential difference between process and thread

A few days ago, a classmate asked me why electron is multi-process instead of multi-threaded? Summarize today and answer this question.

First of all, let's understand the concept of a process: a process is the smallest unit for a computer to allocate resources .

Simply put, a process is a container. For example, a beautiful small villa, the villa has a bedroom, kitchen, study, bathroom, etc., of course, there is a family of three living in it. When mom took her daughter out to play, dad was home alone. At this time, Dad can go wherever he likes at home alone and do whatever he likes. At this time, Dad is like a thread (there is only one active thread in this process), the small villa is like a process , and the kitchen and study at home are like Resources occupied by this process. When three people live together (equivalent to three threads), sometimes there may be some small conflicts. For example, when the daughter occupies the TV to watch cartoons, the father cannot watch the sports channel. This is the inter-thread competition for resources. Of course, most of the time, there is a cooperative relationship between threads (if we create a thread to fight, then why create it?). For example, if mother cooks for father and daughter in the kitchen, father works in the study to earn money to support the family, and daughter is doing homework and performing their duties, then the family will be happy and harmonious. Correspondingly, the process is executing in a healthy manner.

How to watch the process on the computer?

Thread: is the smallest unit of program execution .
Open the task manager,
insert image description here
the program is dead, the process is "live", or is being executed

how to create a process

There are four main functions for nodejs to create subprocesses:

spawnexecexecFilefork

Interested in reading my article: https://blog.csdn.net/woyebuzhidao321/article/details/129567942

How to view threads on the computer?

A process can contain several threads. They are not invisible and intangible , as illustrated below.

insert image description here
You can see that a process contains several threads.

In slightly more technical terms, a thread is a lightweight process and the smallest unit of program execution . Using multi-threads instead of multi-processes to design concurrent programs is because the cost of thread switching and scheduling is far less than that of processes.

how to create a thread

In Node.js, you can Worker create your own thread objects through objects. Worker is a built-in module of Node.js that provides an easy way to create independent JavaScript threads. Here is a simple example:

const {
    
     Worker } = require('worker_threads');

// 创建新的线程
const worker = new Worker('./worker.js', {
    
     workerData: {
    
     message: 'hello' } });

// 监听线程消息
worker.on('message', (message) => {
    
    
  console.log(`Received message from worker: ${
      
      message}`);
});

// 监听线程错误
worker.on('error', (error) => {
    
    
  console.error(`Error in worker: ${
      
      error}`);
});

// 监听线程退出事件
worker.on('exit', (code) => {
    
    
  console.log(`Worker exited with code ${
      
      code}`);
});

In the above example, Workera new thread is created through the object, and workerDataa message is passed to the thread using the option. The thread file worker.js can workerDataget the passed message through the attribute, and send the message to the main thread when needed. In the main thread, you can monitor the message, error and exit events of the thread, and make corresponding processing as needed. In the thread file worker.js, parentPortthe object can be used to communicate with the main thread. For example:

const {
    
     parentPort, workerData } = require('worker_threads');

console.log(`Received message in worker: ${
      
      workerData.message}`);

// 向主线程发送消息
parentPort.postMessage('world');

In the above code, use workerDatathe attribute to get the passed message and output it on the console. Then use postMessagethe method to send a message to the main thread. It should be noted that the Worker object of Node.js uses a thread-based implementation instead of a process-based implementation. Therefore, the created threads run in the same process, and different threads share the same memory. In multi-threaded programming, you need to pay attention to thread safety and shared resources to avoid potential concurrency problems.

Why electron is multi-process instead of multi-thread

The main reasons for adopting multi-processing instead of multi-threading are security and stability. In the case of multi-threading, the crash of one thread may cause the entire application to crash , while in the case of multi-process, the crash of one process will only affect this process and will not affect other processes, thus improving the performance of the application. stability.
In addition, Electron's use of multi-process can also achieve better resource management and allocation. Each process has its own memory space and CPU time, which can make better use of system resources and improve application performance.
Finally, multiprocessing also enables better security. Each process has its own sandbox environment, which can prevent malicious code from attacking and destroying the entire application.
So while multithreading may be more efficient in some cases, multiprocessing is a better choice in terms of security and stability.

If there is any problem in the article, welcome brother Meng to correct me

Guess you like

Origin blog.csdn.net/woyebuzhidao321/article/details/131676125