0

Is there a way to get which CPUs are executing the child processes. I have 8 CPUs. So the parent code starts 8 child processes. Here is the node code:

// parent.js
var child_process = require('child_process');

var numchild  = require('os').cpus().length;
var done      = 0;

for (var i = 0; i < numchild; i++){
  var child = child_process.fork('./child');
  child.send((i + 1) * 1000);
  child.on('message', function(message) {
    console.log('[parent] received message from child:', message);
    done++;
    if (done === numchild) {
      console.log('[parent] received all results');
    }
  });
}


// child.js
process.on('message', function(message) {
  console.log('[child] received message from server:', message);

  setTimeout(function() {
    process.send({
      child   : process.pid,
      result  : message + 1
    });
    process.disconnect();
  }, 10000); 

});
TMichel
  • 4,336
  • 9
  • 44
  • 67
user603749
  • 1,638
  • 2
  • 24
  • 36

1 Answers1

0

Directly from the child object forked though child_process no.

However you could use its PID - child.pid - and execute for example UNIX ps -p <PID> -o psr command from node to parse its response. PSR is the number of the processor being used in a particular process.

To do that you can adapt the proposed solution in node.js shell command execution to your needs:

function cmd_exec(cmd, args, cb_stdout, cb_end) {
  var spawn = require('child_process').spawn,
    child = spawn(cmd, args),
    me = this;
  me.exit = 0;  // Send a cb to set 1 when cmd exits
  child.stdout.on('data', function (data) { cb_stdout(me, data) });
  child.stdout.on('end', function () { cb_end(me) });
}
foo = new cmd_exec('netstat', ['-rn'], 
  function (me, data) {me.stdout += data.toString();},
  function (me) {me.exit = 1;}
);
function log_console() {
  console.log(foo.stdout);
}
setTimeout(
  // wait 0.25 seconds and print the output
  log_console,
250);
Community
  • 1
  • 1
TMichel
  • 4,336
  • 9
  • 44
  • 67
  • This would be a good solution. Unfortunately psr does not work on the Mac. And could not find it even on man for Linux. https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ps.1.html – user603749 Nov 22 '15 at 00:46
  • Your question is then a OSX one. I took the liberty of editing your question to add the tag. In that case I cannot think of a straight-forward solution, you could try using `top` or `htop`. – TMichel Nov 22 '15 at 09:04