Best Online Tutorials

SQL Tutorial

Free SQL Tutorial

SQL Interview Questions

SQL Practice Questions

SQL Tips & Tricks

SQL Errors

SQL Books

Angular Tutorial

Free Angular Tutorial

Angular Interview Questions

Angular Practice Questions

Angular Tips & Tricks

Angular Errors

Angular Books

Node.js Tutorial

Free Node.js Tutorial

Node.js Interview Questions

Node.js Practice Questions

Node.js Tips & Tricks

Node.js Errors

Node.js Books

React.js Tutorial

Free React.js Tutorial

React.js Interview Questions

React.js Practice Questions

React.js Tips & Tricks

React.js Errors

React.js Books

SSIS Tutorial

Free SSIS Tutorial

SSIS Interview Questions

SSIS Practice Questions

SSIS Tips & Tricks

SSIS Errors

SSIS Books

SEO Tutorial

Free SEO Tutorial

SEO Interview Questions

SEO Practice Questions

SEO Tips & Tricks

SEO Errors

SEO Books

Javascript Tutorial

Free Javascript Tutorial

Javascript Interview Questions

Javascript Practice Questions

Javascript Tips & Tricks

Javascript Errors

Javascript Books

MongoDB Tutorial

Free MongoDB Tutorial

MongoDB Interview Questions

MongoDB Practice Questions

MongoDB Tips & Tricks

MongoDB Errors

MongoDB Books

Next.js Tutorial

Free Next.js Tutorial

Next.js Interview Questions

Next.js Practice Questions

Next.js Tips & Tricks

Next.js Errors

Next.js Books

Node.js Interview Questions and Answers | Part 4 

Pooja Goel  |   Dec 30, 2019  |  Views: 259
pictures

Description:

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



Comments:
Content Contributor
Pooja Goel

Pooja Goel

Sr. Software developer at Damco Solutions Pvt. Ltd.  |   Noida, Uttar Pradesh, India

I am a sr. software developer working on technologies like SQL, SSIS, SSRS, DB2,Tableau, ASP.NET having 7 years of experience.


Read related articles

Node.js Interview Questions and Answers | Part 5

Pooja Goel  |   Dec 30, 2019  |  Views: 207

Node.js Interview Questions and Answers | Part 4

Pooja Goel  |   Dec 30, 2019  |  Views: 260

Node.js Interview Questions and Answers | Part 3

Pooja Goel  |   Dec 30, 2019  |  Views: 150

Node.js Interview Questions and Answers | Part 2

Pooja Goel  |   Dec 27, 2019  |  Views: 106

Node.js Interview Questions and Answers | Part 1

Pooja Goel  |   Dec 27, 2019  |  Views: 300

Want to sell your software ?-

If you want to sell your readymade software to the genuine clients or businessman, list your software with details and demo links.

  • Sell ERP
  • Sell Website Clones
  • Sell App Clones
  • Sell Readymade Portals
  • Sell Readymade Websites
  • Sell Mobile Apps
  • Sell Window Apps

Clients will find it using our advanced search filter and will contact you directly.

No any charge for the product lsiting.

Do you own a Company ?+

Property.sale

Buy or Sell Commercial Property+

Are you a freelancer ?+

Looking for a job ?+

Are you a Service Provider ?+

Are you a blogger ?+

Do you own an IT Institute ?+


Property.sale