Home  >  Article  >  Web Front-end  >  Is Node.js really single-threaded? How to communicate between processes?

Is Node.js really single-threaded? How to communicate between processes?

青灯夜游
青灯夜游forward
2021-08-02 18:44:082444browse

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.

Is Node.js really single-threaded? How to communicate between processes?

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!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete