Child Process
Start a new process with either a command, a file, a node program synchronously or asynchronously.
node file.js –> start new process
When we call node then provide a file that you’d like to run What you are effectively doing is starting a new process and this process will make available some resources and environment for a program to run and you know your program can use these resources to do its tasks.
What this module essentially allows you to do is to start these new processes while your program is running. You can start these new processes by calling specific programs that are already in your computer you can do that by using a command, you can do that by using a file, or by using some node program that you have in some directory somewhere.
While calling the functions to start it these new processes, you can provide arguments and options for these child processes who know where they’re gonna run what sort of resources is going to be available to them.
You can start these new processes synchronously or asynchronously.
We can start child processes with these functions are asynchronous, meaning that your parent program will not be waiting for these child processes terminate before the parent process terminates.
These are the synchronous versions for spawn,exec

What that essentially means is that you your program is going to wait for that process terminates before the parent continues with its execution.
These are the events that a child process can fire.It is also an event emitter.

4 Different way to create process/ threads
fork : will create child process, will run in separte V8 engine.
spawn : will create child process with in same V8 engine, it is used for huge amount of data, It returns a stream.
exec : it returns a buffer, by default max size is 200k, if the child process return anything more that 200k , it will throw error “memory buffer exceeded”. Though we can fix it by setting the bigger buffer amount in execute options, but it is not advice able. Exec is not made for process that return huge buffer to node, we should use spawn for that.
Example 1: Create Multi Threading In NodeJs using Fork
app.js
const { fork, spawn } = require ("child_process");
const myChildprocess = fork("child.js") // another file
//const myChildprocess = spawn("node child.js")
myChildprocess.send({hello: 'world'});
myChildprocess.on("message", (msg)=>{
console.log("Message from child : ", msg)
})
myChildprocess.on("exit", (msg)=>{
console.log("Child Process Terminated.", msg)
})
console.log("Checking the process id of child process : ", myChildprocess.pid)
child.js
process.on('message', (msg) => {
console.log("Message from parent : ", msg)
});
let counter = 0 ;
setInterval(() => {
process.send({counter: counter++}); //send it to parent
if(counter == 5){
process.exit();
}
}, 1000)
//Output :
Checking the process id of child process : 143600
Message from parent : { hello: 'world' }
Message from child : { counter: 0 }
Message from child : { counter: 1 }
Message from child : { counter: 2 }
Message from child : { counter: 3 }
Message from child : { counter: 4 }
Child Process Terminated. 0
Example 2 : We can kill the child process from my parent class :
app.js
const { fork, spawn } = require ("child_process");
const myChildprocess = fork("child.js") // another file
//const myChildprocess = spawn("node child.js")
myChildprocess.send({hello: 'world'});
myChildprocess.on("message", (msg)=>{
console.log("Message from child : ", msg)
})
myChildprocess.on("exit", (msg)=>{
console.log("Child Process Terminated.", msg)
})
console.log("Checking the process id of child process : ", myChildprocess.pid)
counter = 0;
setInterval(()=>{
counter++;
if(counter == 5 ){
myChildprocess.kill();
//process.kill(myChildprocess.pid) //we can kill by process id
}
console.log("Checking the status of child process : ", myChildprocess.killed);
},1000)
child.js
process.on('message', (msg) => {
console.log("Message from parent : ", msg)
});
let counter = 0 ;
setInterval(() => {
process.send({counter: counter++});
// if(counter == 5){
// process.exit();
// }
}, 1000)
//Output
Checking the process id of child process : 54276
Message from parent : { hello: 'world' }
Checking the status of child process : false
Message from child : { counter: 0 }
Checking the status of child process : false
Message from child : { counter: 1 }
Checking the status of child process : false
Message from child : { counter: 2 }
Checking the status of child process : false
Message from child : { counter: 3 }
Checking the status of child process : true
Child Process Terminated. null
Checking the status of child process : true
Checking the status of child process : true


