Live data from Hacker News

fork() can fail

rachelbythebay.com

171–180 of 320 posts

Re: fork() can fail

#171

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.

No worries :)

Re: fork() can fail

#172

Earlier quoted context omitted.

Parent said as soon as it is able to fork a new process, which still isn't quite right (it's the child that quits, as you say) but not the error you seem to be responding to.

He didn't say this when I replied.

Ah, do you remember what it said?

Re: fork() can fail

#173
post #144

Is 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.

Nitpick: std::optional does not exist in C++11, nor in the recently-approved C++14. It is, nevertheless, in the process of getting into the standard, and is already present in GCC 4.9's libstdc++ as std::experimental::optional.

Re: fork() can fail

#174

Earlier quoted context omitted.

He didn't say this when I replied.

Ah, do you remember what it said?

I got it swapped around initially:

"while(1) {fork()}; is "better", as the version you mention will quit as soon as it is not able to fork a new process."

or something similar

Re: fork() can fail

#176
post #131
post #124

Earlier 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…

There'd be far fewer bugs if everyone knew exactly how everything else works.

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

#177
post #144

Is 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.

Unfortunately `std::optional` did not get accepted for C++14, because it's implementation uses some hacks that are undefined behavior by the standard (but work on the major compilers).

Re: fork() can fail

#178
The 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.

Re: fork() can fail

#179

The 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.

This. One of the most useful classes I took in undergrad was implementing a scheduler and an I-node disk. Nothing shows you all the ways fork() can fail like having to implement it.

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.

Re: fork() can fail

#180

Earlier quoted context omitted.

Ah, do you remember what it said?

I got it swapped around initially: "while(1) {fork()}; is "better", as the version you mention will quit as soon as it is not able to fork a new process." or something similar

Gotcha.
Post reply on HN