varCluster = require (' cluster '));varHTTP = require (' http ');varFS = require ("FS");//var process = require ("process");varnum = require ("OS"). CPUs (). length;if(cluster.ismaster) {if(Fs.existssync ("Aa.txt") {Fs.unlinksync ("Aa.txt"); } //Fork workers. for(vari = 0; i < num; i++) { //Console.log ("fork" + i); varWorker =cluster.fork (); Worker.on ("Message",function(msg) {Finish++; if(finish = 8) {Console.log ("All done."); } This. Kill (); }); } varfinish = 0; Cluster.on (' Exit ',function(worker, Code, signal) {Console.log (' Worker ' + worker.process.pid + ' died '); });} Else { //Workers can share any TCP connection //In the case of its a HTTP server /*console.log ("Start HTTP Server @" + cluster.worker.process.pid); Http.createserver (function (req, res) {Console.log ("request @" + cluster.worker.process.pid); Res.writehead (200); SetTimeout (function () {res.end ("Hello world\n @" + cluster.worker.process.pid); }, 20000); var i = 9999999999999; while (i--> 0) {}}). Listen (8000);*/ vari = 99999; varWorker =Cluster.worker; varFD = Fs.opensync ("Aa.txt", "A +"); //Console.log (FD); while(i--> 0) { //Console.log (i + "@" + worker.process.pid);Fs.writesync (FD, i + "@" + worker.process.pid + "\ r \ n")); } fs.closesync (FD); Process.send ("OK"); //Worker.kill ();}
Node JS Test cluster