process
In the operating system, the interpretation of process: process is a dynamic execution process of a program with certain independent functions in a data set. It is an independent unit of the operating system for resource allocation and scheduling. It is the carrier of application program operation.
-
Is a running process of the application (dynamic concept)
-
It is the basic unit of the system to allocate and schedule resources (process is generally composed of three parts: program, data set and process control block).
-
Each process has its own independent spatial address. Data stack (data between processes is not shared, but can be communicated by other means) Processes generally have five states: initial state, execution state, waiting state, ready state and termination state
thread
Thread is a single sequence control flow in program execution. It is the smallest unit of program execution flow and the basic unit of processor scheduling and dispatching.
-
The smallest unit of task scheduling and execution
-
A single line of code execution in a process
Process and thread
Threads are the smallest units of program execution, a process is operating system allocates resources the smallest unit of a process is composed of one or more threads, thread is a process of the code in different route between processes are independent of each other, but under the same process each Shared program memory space of the thread context switching between threads faster than process context switching
The node in the process
Node is single-threaded, meaning that a process has only one thread running on it
Is it really single threaded?
Node is single-threaded, but its underlying thread is multi-threaded. In the event loop, the Libuv library pulls tasks from the event queue and redistributes them to different threads for processing. The hardware is not as backward as it used to be, and using only a single thread to operate would be a waste of resources. Therefore, Node provides child_process module and Cluster module in order to realize multi-process processing and give full play to the advantages of multi-core CPU.
-
The child_process module is used to start multiple child processes that run different commands or execute Node.js module files or executable files
-
The Cluster module is used to start multiple child processes in node.js applications, and each child process runs a copy of the Node.js application
Child_process module
-
Child_process.spawn () : good for returning large amounts of data, such as image processing, binary data processing.
-
Child_process.exec () : applies to small amounts of data. The default value of maxBuffer is 200 * 1024, exceeding which will crash the program. Spawn is used for large amounts of data.
-
ExecFile () : Similar to child_process.exec(), except that it cannot be executed by shell and does not support actions such as I/O redirection and file lookup
-
Child_process.fork () : generates new processes independently of each other. Each process has its own V8 instance and memory. System resources are limited.
The cluster module
-
Cluster.fork ([env]) starts a child process that creates an instance of the Node.js application
-
The isMaster and isWorker attributes are used to determine whether you are running in the main or child process
-
The Workers property is used to get worker objects running in all child processes
By extension, how do threads communicate with each other?
Communication between processes
Four:
-
Messaging (pipes, FIFO, message queues)
-
Semaphore (mutex, condition variable, read/write lock)
-
Shared memory (anonymous, named)
-
Remote procedure call
Interprocess communication is not only encountered in Node, but in virtually every other language, and is certainly a must-ask question in an interview.
Much of this can be discussed in depth