samedi 28 février 2015

How to spawn new process everytime I call a recursive function in Node.js

I am new to node.I am trying to create a recursive function say Merge Sort.I want to create a new process every time I call the function.


Basically I want to divide the task into two parts every time I call the function and pass each part to a new process.


Here is my code: merge.js



process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function (chunk) {
var arr = chunk.trim().split(' ');

mergeSort(arr,0,arr.length - 1);
// I want to create new threads like this
// CreateNewLeftThread
// CreateNewRightThread
// Join(lefThread.rightThread)


function mergeSort(arr,left,right) {

if (left < right) {

var mid = Math.floor(left + (right - left)/2);
mergeSort(arr,left,mid);
mergeSort(arr,mid+1,right);
merge(arr,left,right,mid);

}

}

function Merge(Parameters) {

/*Code Here*/

}

process.exit(0);

});

Aucun commentaire:

Enregistrer un commentaire