-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var exec = require('child_process').exec; | ||
|
||
var child = exec('uptime | 23cut -d "," -f 1', function(err, stdout, stderr) { | ||
if (err) { | ||
console.log(stderr); | ||
} else { | ||
console.log('Output is: ' + stdout); | ||
} | ||
}); | ||
|
||
console.log("PID is: " + child.pid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var fork = require('child_process').fork; | ||
|
||
var child = fork(__dirname + '/honourstudent.js'); | ||
|
||
child.on('message', function(m) { | ||
console.log('The answer is: ', m.answer); | ||
child.send({cmd: 'done'}); | ||
}); | ||
|
||
child.send({cmd: 'double', number:20}); | ||
|
||
|
||
console.log('PID is ' + child.pid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var spawn = require('child_process').spawn, | ||
ps = spawn('ps', ['ax']), | ||
grep = spawn('grep', ['node']); | ||
|
||
ps.stdout.pipe(grep.stdin); | ||
grep.stdout.pipe(process.stdout); | ||
|
||
ps.stderr.on('data', function(data) { | ||
console.log('ps stderr: ' + data); | ||
}); | ||
|
||
grep.stderr.on('data', function (data) { | ||
console.log('grep stderr: ' + data); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var cluster = require('cluster'); | ||
var http = require('http'); | ||
var numWorkers = 2; | ||
|
||
if (cluster.isMaster) { | ||
// Fork Workers | ||
for (var i=0; i<numWorkers; i++) { | ||
console.log('master: about to fork a worker'); | ||
cluster.fork(); | ||
} | ||
|
||
cluster.on('fork', function(worker) { | ||
console.log('master: fork event (worker ' + worker.id + ')'); | ||
}); | ||
|
||
cluster.on('online', function(worker) { | ||
console.log('master: online event (worker ' + worker.id + ')'); | ||
}); | ||
|
||
cluster.on('listening', function(worker, address) { | ||
console.log('master: listening event (worker ' + worker.id + ', pid ' + worker.process.pid + ', ' + address.address + ':' + address.port + ')'); | ||
}); | ||
|
||
cluster.on('exit', function(worker, code, signal) { | ||
console.log('master: exit event (worker ' + worker.id + ')'); | ||
}); | ||
} else { | ||
|
||
console.log('worker: worker#' + cluster.worker.id + ' ready!'); | ||
|
||
var count = 0; | ||
|
||
// Workers can share any TCP connection | ||
// In this case its a HTTP server | ||
http.createServer(function(req, res) { | ||
res.writeHead(200); | ||
// count.dir(req); | ||
count++; | ||
console.log('Worker # ' + cluster.worker.id + 'is incrementing count to ' + count); | ||
res.end('Hello world from worker #' + cluster.worker.id + ' (pid ' + cluster.worker.process.id + ')'); | ||
if (count === 3) { | ||
cluster.worker.destroy(); | ||
} | ||
}).listen(process.env.PORT, process.env.IP); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var fun = require('./mathfun'); | ||
|
||
process.on('message', function(m) { | ||
console.log("Child pid is: " + process.pid); | ||
if (m.cmd === 'double') { | ||
console.log('hs: I was asked to double ' + m.number); | ||
fun.evenDoubler(m.number, function(err, res) { | ||
process.send({answer: res}); | ||
}); | ||
} else if (m.cmd === 'done') { | ||
process.exit(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var maxTime = 200; | ||
|
||
var evenDoubler = function(v, callback){ | ||
var randomTime = Math.floor(Math.random()*(maxTime+1)); | ||
if( v % 2 == 0) { | ||
setTimeout(function(){ | ||
callback(null, v * 2, randomTime); | ||
}, randomTime); | ||
} else { | ||
var err = new Error("odd number"); | ||
setTimeout(function(){ | ||
callback(err); | ||
}, randomTime); | ||
} | ||
}; | ||
|
||
var evenDoublerSync = function(v) { | ||
if (v%2) throw (new Error("ODD INPUT")); | ||
else return v*2; | ||
} | ||
|
||
module.exports.evenDoubler = evenDoubler; | ||
module.exports.evenDoublerSync = evenDoublerSync; | ||
module.exports.foo = "bar"; | ||
|
||
//evenDoubler(2, handleResults); | ||
//evenDoubler(1, handleResults); |