dimanche 31 janvier 2021

What does fork() == 0 returns?

I have 2 programs

The first program would call fork() and assign it to p. When p == 0, it implies child process.

// The first program
p = fork();
if (p == 0)
{
    // child process
}
else
{
    // parent process or exception
}

But what if we just call fork() == 0? I would think fork() return non-zero from it's docs. Hence the condition is never executed.

// The second program
if (fork() == 0)
{
    // Would this ever be reached?
    printf("A\n");
}

printf("B\n");

Please don't downvote this, or at least explain why... I tried this code before asking, "A" is never printed and there's not enough documentation about how fork() works

Aucun commentaire:

Enregistrer un commentaire