Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Perhaps I'm misunderstanding what you're saying, but that's wrong too. With fork() you need to always test 3 cases:

    * -1: error
    * 0: success, in child
    * > 0: success, in parent
Testing for <= 0 would cause you to think there's an error when you're really just the child process.


You only need 3 cases if you need to distinguish between the parent and the child. I can imagine designs where the parent and child are both going to exec the same program and so all you need to check on the fork is for success or failure.


Or that you're the child process when really there's an error, which might go undetected longer.


Thankfully that case will never happen because you can't be a child if there was an error. :)

Also the check as presumably written would never miss an error, it would just potentially assume valid return values were also errors.


"Thankfully that case will never happen because you can't be a child if there was an error. :)"

Uh, no. If there is an error, there will be no child process but the parent process will think it is the child.

From the fork man page, emphasis mine: "On success, the PID of the child process is returned in the parent, and 0 is returned in the child."

"Also the check as presumably written would never miss an error, it would just potentially assume valid return values were also errors."

That was already discussed as a possibility; I was addressing the other. In the case you describe the software would never work at all, even when fork successfully forks, because the child will always think there was an error and presumably fall over rather than getting things done. That's probably the better case, in terms of development progress, because it would be spotted and fixed right away. But hopefully fixed correctly, and not converted to the broken-but-working-when-fork-succeeds other variant that also uses "<= 0".


Ah, I see what you mean! Sorry for the misunderstanding, I had trouble parsing your post.


No worries :)




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: