Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

389
Views
why I'm getting different outputs in different devices of the same code of the child_process's fork method in nodejs?

Main I have two files, one is the parent file and another one is the child file. I tested in two different systems one mac os monastery and another one is dell - ubuntu 20., In both systems, I'm getting the different sequences of output(console log) as in below-attached pictures

app.js:

var cp = require('child_process');



var child = cp.fork(__dirname + '/child.js');
  
child.on('message', function(m) {
  console.log('Parent process received:', m);
});
  
child.send({ hello: 'from parent process' });



child.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

child.js

    process.on('message', function(m) {
      console.log('Child process received:', m);
    });
    
    process.send({ hello: 'from child process' });

Mac os montery: output:

Child process received: { hello: 'from parent process' }
Parent process received: { hello: 'from child process' }

Ubunt 20, output:

Parent process received: { hello: 'from child process' } 
Child process received: { hello: 'from parent process' }

Output links:

Output: mac os monterey

Output: Ubuntu 20

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you fork a process, there are no guarantees about the order that the parent and child will next run in. Even if you see one consistent order on macOS and another on Ubuntu, this isn't guaranteed and you shouldn't rely on it. If you need things to happen in a certain order, then you need to structure your code differently to guarantee it (for example, you could have the child not send the message to the parent until it's in its own message received handler).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!