What is Child Process and Cluster? What are the difference?

 Child Process in Node.js is used to run a child process under Node.js. There are two methods to do that: exec and spawn. The example for exec is:

var exec = require('child_process').exec;
exec('node -v', function(error, stdout, stderr) {
  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);
  if (error !== null) {
    console.log('exec error: ' + error);
}
});

We are passing our command as our first argument of the exec and expect three response in the callback. stdout is the output we could expect from the execution.

Cluster is used to split a single process into multiple processes or workers, in Node.js terminology. This can be achieved through a cluster module. The cluster module allows you to create child processes (workers), which share all the server ports with the main Node process (master).

A cluster is a pool of similar workers running under a parent Node process. Workers are spawned using the fork() method of the child_processes module. Cluster is multiple processes to scale on multi cores.

Comments

Popular posts from this blog

What test framework did you use to test your nodejs applications

What is V8 Engine? What is the relationship between Node.js and V8?