31. What Is A Child_process Module In Node.Js ?
Child process (child_process) is a module in Node.js which is used to spawn or copy an existing child process which are similar but not identical.
Different ways to create a new child process:
child_process.spawn(command[, args][, options])
child_process.fork(modulePath[, args][, options])
child_process.exec(command[, options][, callback])
child_process.execFile(file[, args][, options][, callback])
These all child process executes asynchronously without blocking Node.js event loop.
To execute them synchronously we have different synchronous methods in child process class:
child_process.spawnSync()
child_process.execSync()
child_process.execFileSync()
These synchronous methods creates or spawns new child processes which blocks the event loop until the spawned process either exits or is terminated.
32. Since node is a single threaded process, how to make use of all CPUs ?
Using the cluster in Node.js , we can use all available CPUs by forking the main processor.
And to make a process multi-threaded we use, worker_thread module, which uses the same memory of the processor.
33. What is cluster in Node.js ?
Cluster in nodejs is a group of work processors.
Number of work processors in group is decided by the number of available CPU in OS.
- If your system processor is a quad-core processor, then you can start 4 workers
Each work processor is started by using cluster.fork()
fork creates the exact clone of process, using the same available port to the master process.
var cluster = require('cluster');
var express = require('express');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
// Create a worker
cluster.fork();
}
} else {
// Workers share the TCP connection in this server
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
// All workers use this same port
app.listen(8080);
}
34. Difference between cluster and child_process modules ?
cluster.fork() - It starts from the beginning (entry) of the module.
child_process.fork() - It starts where its been defined.
child_process.fork() method is a bit lower-level and required arguments like
location (file path) of the module as an argument,
current working directory,
user that owns the process,
environment variables...
35. Differentiate between spawn() and fork() methods in Node.js ?
Difference between child_process.spawn() & child_process.fork()
child_process.spawn(command[, args][, options])
child_process.fork(modulePath[, args][, options])
Spawn: It takes commands as input argument parameter to run in new child proecss.
Fork: It takes the module path as input argument parameter to run in new child processs.
An IPC communication channel is established that allows sending messages between parent and child.
36. How does Node.js handle child threads ?
Node.js handle child threads using worker_threads, as it shares the same memory buffer of the main thread.
37. What is IPC in Node.js ?
IPC - Inter process communication
Node.js works on a single process, and on a single port.
But we can make it behave like multi processor with the help of child_process or clusters, which also works on the same port used by parent.
So to work on same port , parent and child process communicate between them by messaging between themselves.
This mechanism is called IPC (Inter process communication)
This IPC helps in the clustering of node.js process as per the available CPU processors.
38. How does Node.js support multi-processor platforms, and does it fully utilize all processor resources ?
We can make Node.js a multi-processor by forking the main processor , which will be a similar process to main processor, but does different works in parallel.
- By using cluster (cluster)
- By using Child process (child_process)
Yes, it utilizes the all processor resources.
39. What are Worker Threads in Node.js ?
Worker thread is the module (worker_thread) in Node.js.
This module is used to run some JavaScript tasks in parallel.
Useful for performing CPU-intensive JavaScript operations.
It shares the same memory buffer of the main thread.
It will not help much with I/O-intensive works.
const { Worker, isMainThread } = require('worker_threads');
if (isMainThread) {
// This re-loads the current file inside a Worker instance.
new Worker(__filename);
} else {
console.log('Inside Worker!');
console.log(isMainThread); // Prints 'false'.
}
40. What is the difference between cluster and worker thread?
Cluster:
One process is launched on each CPU and can communicate via IPC.
Each process has it's own memory with it's own Node (v8) instance. Creating tons of them may create memory issues.
Great for spawning many HTTP servers that share the same port as the master process will multiplexe the requests to the child processes.
Worker threads:
It has one process in total.
It creates multiple threads with each thread having one Node instance (one event loop, one JS engine). Most Node API's are available to each thread except a few. So essentially Node is embedding itself and creating a new thread.
It shares memory with other threads (e.g. SharedArrayBuffer)
Good for CPU intensive tasks like processing data or accessing the file system. Because NodeJS is single threaded, synchronous tasks can be made more efficient with workers