Node.js runs in a single-thread mode, but it uses an event-driven paradigm to handle concurrency. It also facilitates creation of child processes to leverage parallel processing on multi-core CPU based systems.
Child processes always have three streams child.stdin, child.stdout, and child.stderr which may be shared with the stdio streams of the parent process.
Node provides child_process module which has the following three major ways to create a child process.
child_process.exec method runs a command in a shell and buffers the output. It has the following signature −
child_process.exec(command[, options], callback)
Here is the description of the parameters used −
The exec() method returns a buffer with a max size and waits for the process to end and tries to return all the buffered data at once.
Let us create two js files named support.js and master.js −
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
const fs = require('fs');
const child_process = require('child_process');
for(var i=0; i<3; i++) {
var workerProcess = child_process.exec('node support.js '+i,function
(error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
console.log('Signal received: '+error.signal);
}
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
});
workerProcess.on('exit', function (code) {
console.log('Child process exited with exit code '+code);
});
}
Now run the master.js to see the result −
node master.js
Verify the Output. Server has started.
Child process exited with exit code 0 stdout: Child Process 1 executed. stderr: Child process exited with exit code 0 stdout: Child Process 0 executed. stderr: Child process exited with exit code 0 stdout: Child Process 2 executed.
child_process.spawn method launches a new process with a given command. It has the following signature −
child_process.spawn(command[, args][, options])
Here is the description of the parameters used −
The spawn() method returns streams (stdout &stderr) and it should be used when the process returns a volume amount of data. spawn() starts receiving the response as soon as the process starts executing.
Create two js files named support.js and master.js −
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
const fs = require('fs');
const child_process = require('child_process');
for(var i = 0; i<3; i++) {
var workerProcess = child_process.spawn('node', ['support.js', i]);
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
workerProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
workerProcess.on('close', function (code) {
console.log('child process exited with code ' + code);
});
}
Now run the master.js to see the result −
node master.js
Verify the Output. Server has started
stdout: Child Process 0 executed. child process exited with code 0 stdout: Child Process 1 executed. stdout: Child Process 2 executed. child process exited with code 0 child process exited with code 0
child_process.fork method is a special case of spawn() to create Node processes. It has the following signature −
child_process.fork(modulePath[, args][, options])
Here is the description of the parameters used −
The fork method returns an object with a built-in communication channel in addition to having all the methods in a normal ChildProcess instance.
Create two js files named support.js and master.js −
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
const fs = require('fs');
const child_process = require('child_process');
for(var i=0; i<3; i++) {
var worker_process = child_process.fork("support.js", [i]);
worker_process.on('close', function (code) {
console.log('child process exited with code ' + code);
});
}
Now run the master.js to see the result −
node master.js
Verify the Output. Server has started.
Child Process 0 executed. Child Process 1 executed. Child Process 2 executed. child process exited with code 0 child process exited with code 0 child process exited with code 0