Earlier quoted context omitted.
"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 w…
Ah, I see what you mean! Sorry for the misunderstanding, I had trouble parsing your post.
fork() can fail
171–180 of 320 posts
Re: fork() can fail
#172Re: fork() can fail
#173Is there any clean way to use an Option/Maybe monad in C (or C++)? It should be a simple way to solve problems where error codes are valid inputs of other functions. The simplest way I can think of is: struct maybe { bool isEmpty; void* value; } Although I wonder if using C++ templates, classes and operator overloading is possible to make a more practical implementation (using void* does seem like a bad idea).
In C++ std::optional does the trick, as of version 11, and boost::optional previously. In C ... hrm you could probably wrangle some macros around that struct if you were desperate.
Re: fork() can fail
#174Re: fork() can fail
#175Re: fork() can fail
#176Earlier quoted context omitted.
Taking the square root of a negative number removing all the files in your home directory could be "well defined and clearly documented behavior". Would you blame the API author at that point or would it still be strictly your fault? At what point do API authors share the blame for a needlessly harsh punishment delivered upon a predictably common error? I certainly prefer to work with systems produced by people tendi…
I could argue, however, that in this particular case, it's the user's fault for failing to understand the full and defined behavior of fork() in addition to failing to understand the full and defined behavior of other functions, ...say, kill(). It's just as wrong to feed kill() -1 as it would be to feed it -48585 or "babdkd" (unless that is explicitly your intention). A simple sanity check of if [ "${pid} > "0" ]; is…
This kind of mistake is godawful and should not be defended. (but it's correctly fixed through stronger typing, not through choosing -48585 as the code for killing everything).
Re: fork() can fail
#177Is there any clean way to use an Option/Maybe monad in C (or C++)? It should be a simple way to solve problems where error codes are valid inputs of other functions. The simplest way I can think of is: struct maybe { bool isEmpty; void* value; } Although I wonder if using C++ templates, classes and operator overloading is possible to make a more practical implementation (using void* does seem like a bad idea).
In C++ std::optional does the trick, as of version 11, and boost::optional previously. In C ... hrm you could probably wrangle some macros around that struct if you were desperate.
Re: fork() can fail
#178Re: fork() can fail
#179The first time I learnt of fork (from an OS book), the example had three branches to the if statement after fork - and the first tested for a negative pid. I suspect that the reason this link has 400 odd upvotes is because more people aren't learning OS the correct way in the beginning. Or maybe my OS book was nice. IDK.
Every system call can fail, even if it doesn't do something obvious like use disk resources. Ignoring this is how subtle bugs appear that seem unreproducible until you implement correct error handling.