请教:cluster 多进程读相同文件 fd 问题
代码如下:
const cluster = require('cluster');
const http = require('http');
const fs = require('fs');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8080, function() {
fs.open('./test.txt', 'a', (err, fd) => {
console.log(err, fd);
})
});
console.log(`Worker ${process.pid} started`);
}
结果:
文件 fd 是相同的,请问是怎么做到的?