child.js

    1. console.log(`当前是子进程: ${process.pid}`)
    2. process.on('message', msg => {
    3. console.log(`来自master进程的消息: ${msg}`)
    4. })
    5. // send 发送消息
    6. process.send(`这是子进程`)

    master.js

    1. const {fork} = require('child_process')
    2. const child = fork('./child.js')
    3. child.on('message', msg => {
    4. console.log(`来自子进程发送的消息 ${msg}`)
    5. })
    6. child.send(`来自master`)