Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

382
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda