


Is Node.js really single-threaded? How to communicate between processes?
This article will introduce processes and threads to you, take you to understand the threads in Node.js, see if node is really single-threaded, child_process and cluster modules, and briefly talk about more How threads communicate with each other.
Process
In the operating system, the explanation of process: A process is a program with certain independent functions in a data A dynamic execution process on the set is an independent unit for resource allocation and scheduling by the operating system, and is the carrier for application running.
is a running process of the application (dynamic concept)
is the basic unit for system allocation and scheduling of resources (a process is generally composed of a program , data collection and process control block)
Each process has its own independent space address and data stack (data is not shared between processes and can be communicated through other methods) A process generally has five states: initial state, execution state, waiting state, ready state, and termination state.
Thread
Thread is when the program is executing A single sequential control process is the smallest unit of program execution flow and the basic unit of processor scheduling and dispatch.
The smallest unit of task scheduling and execution
A single execution route of code in a process
The difference between process and thread
Thread is the smallest unit of program execution, and process is the smallest unit of resources allocated by the operating system. A process consists of one or more threads, and a thread is the code in a process. Processes of different execution routes are independent of each other, but the memory space of the program is shared between threads in the same process. Thread context switching is faster than process context switching.
#Processes in node
node is single-threaded, that is, a process only opens one thread node. [Recommended study: "nodejs Tutorial"]
Is it really single-threaded?
Although Node is single-threaded, its underlying layer is multi-threaded. In the event loop, the libuv library takes out tasks from the event queue and assigns them to different threads for processing. Now the hardware conditions are not as backward as before. If only a single thread is used for operation, resources will be wasted. Therefore, in order to achieve multi-process processing and give full play to the advantages of multi-core CPUs, Node provides the child_process module and cluster module.
The child_process module is used to start multiple child processes, and run different commands or execute node.js module files and executable files in the child processes
Cluster module, cluster module, is used to start multiple child processes in a Node.js application and run a copy of the Node.js application in each child process
child_process module
child_process.spawn(): Suitable for returning large amounts of data, such as image processing and binary data processing.
child_process.exec(): Suitable for small amounts of data. The default value of maxBuffer is 200 * 1024. Exceeding this default value will cause the program to crash. If the amount of data is too large, spawn can be used.
-
child_process.execFile(): similar to child_process.exec(), the difference is that it cannot be executed through the shell and does not support behaviors like I/O redirection and file search
child_process.fork(): Spawn a new process. The processes are independent of each other. Each process has its own V8 instance and memory. System resources are limited. It is not recommended to spawn too many processes. The number of child processes coming out is usually set according to the number of system * CPU cores.
cluster module
cluster.fork([env]) Start the child process and create a Node in the child process Example of .js application
isMaster attribute and isWorker attribute are used to determine whether it is running in the main process or in a child process
Workers attribute is used to obtain worker objects running in all sub-processes
Expanding, how do multiple threads communicate?
Inter-process communication
Four types:
-
Message passing (pipeline, FIFO, message queue)
Semaphore (mutex, condition variable, read-write lock)
Shared memory (anonymous, named)
Remote Procedure Call
Inter-process communication is not only encountered in node, but also in other languages. Of course, it is also a must-ask question in the interview. .
Many contents can be discussed in depth!
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of Is Node.js really single-threaded? How to communicate between processes?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

How does Node.js do GC (garbage collection)? The following article will take you through it.

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".
