What is the implementation principle of node's child_process? Why can the data in the main process be used between child processes?

Doesn"t

say that resources between processes cannot be shared? even if you want to share, you need to use the manual of process communication, right?

individuals only go so far in sharing resources between processes.

so why can the processes created by Node.js child_process take the data of the parent process between them?

// 
const exec = require("child_process").exec;
const Name = "QQ";
exec("echo hello world", (err, stdout, stderr) => {
    if (err) throw err;
    console.log (Name);    // "QQ"
})

Why can the Name variable of the main process be obtained in the callback of exec? Is it possible that the callback function of exec is back to the parent process?
by the way, my parent process had a function that uniformly handles error. I can capture and process the throw of all the main processes.
but once throw error is in exec. My main process can"t be captured, and the main process is dead.

Apr.05,2021
Of course, the callback of

exec is in the main process.


you have to understand what fork has done. Instead of starting a new process, it copies the current process again, including its state, so the child process also has the constant Name . child_process of node.js I haven't used it, but I checked your usage. callback of exec is executed by the parent process after the child process ends, and because exec is asynchronous, you certainly can't catch errors with try-catch . Since the first parameter of callback marks you with an error, you no longer need try-catch .


fork will the status of the current process be copied once? I'll take a look at the document

Menu