InterviewSolution
| 1. |
Explain spawn() and fork() in Node.js? |
|
Answer» Node.js follows a single-threaded EVENT loop model architecture. One process in one CPU is not enough to handle the application WORKLOAD so we create CHILD processes.“child_process” module supports child processes in Node.js. These child processes can communicate with each other using a built-in messaging system. Child processes could be created in four different ways Node: spawn(), fork(), exec(), and execFile(). spawn() method brings up the child processes asynchronously. It is a command designed to run system commands that will be run on its own process. In spawn() no new V8 instance is created and only one copy of your node module is active. When your child process returns a large amount of data to the Node spawn() method could be used. Syntax: child_process.spawn(command[, args][, options]) Spawn method returns streams (stdout & stderr) and it’s main ADVANTAGES are
In fork() a fresh instance of the V8 engine is created. fork() method could be used as below: Syntax: child_process.fork(modulePath[, args][, options]) In fork() a communication channel is ESTABLISHED between parent and child processes and returns an object. We use the EventEmitter module interface to exchange messages between them. |
|