InterviewSolution
| 1. |
Explain difference between blocking and non blocking program? |
|
Answer» Blocking program
Example: Let us create text file named blk.txt blk.txtA technology that has emerged as the frontrunner for handling Big Data processing is Hadoop. Create a javascript code as follows and save as blk.js VAR fs = require("fs"); var data = fs.readFileSync('blk.txt'); console.log(data.toString()); console.log('Program Ended');Execute the code. The result is
Program Ended In this example, program BLOCKS until it reads the file and then only it proceeds to next statement or end the program. Non Blocking program
Example: Use the same input file defined for blocking code example. Create a javascript code as follows and save as nblk.js var fs = require("fs"); fs.readFile('blk.txt', function (err,data) { if (err) return console.error(err); console.log(data.toString()); }); console.log("Program Ended");Execute the code. The result is Program Ended A technology that has emerged as the frontrunner for handling Big Data processing is Hadoop. This example shows that program doesn’t WAIT for file reading and prints “Program Ended” first. At the same time, the program without blocking continues to read the file. |
|