Can I send message to parent process?
master
var child =child_process.fork();
child.send({msg:msg})
child process
process.on('message', function(){
});
// how to send message to parent??
Write a C program in which the child process takes an input array and send it to the parent process using pipe() and fork() and then print it in the parent process.
The read end of one pipe serves as standard input for the child process, and the write end of the other pipe is the standard output for the child process.
In short use: process.send()
Longer example, I wrote awhile ago named forktest.js
:
var cp = require('child_process');
if (!process.send) {
var p = cp.fork(__dirname + '/forktest');
p.send({
count: 10
});
p.on('message', function(data) {
process.exit(0);
});
} else {
process.on('message', function(data) {
console.log(data);
data.count--;
if (data.count === 0) {
process.send({});
process.exit(0);
}
var p = cp.fork(__dirname + '/forktest');
p.send(data);
p.on('message', function(data) {
process.send(data);
process.exit(0);
});
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With