Live data from Hacker News

fork() can fail

rachelbythebay.com

71–80 of 320 posts

Re: fork() can fail

#71

Just noticed that in Perl the behavior is slightly different: http://perldoc.perl.org/functions/fork.html unsuccessful fork() returns undef, effectively stopping you from kill-ing what you don't want to kill.

In C, there is no concept of null or undefined, only 0.

Re: fork() can fail

#72
post #20

If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest "it cannot happen to me", the gods shall surely punish thee for thy arrogance. [0] [0]: http://www.lysator.liu.se/c/ten-commandments.html

Annoyingly, that article refers to the One True Brace Style but doesn't bother to define it or link to it!

Re: fork() can fail

#74
post #72
post #20

If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest "it cannot happen to me", the gods shall surely punish thee for thy arrogance. [0] [0]: http://www.lysator.liu.se/c/ten-commandments.html

Annoyingly, that article refers to the One True Brace Style but doesn't bother to define it or link to it!

The "One True Brace Style" is actually a specific style with that name. It is based on the K&R style, with the additional stipulation that all `if`, `else`, `for`, and `while` statements use braces.

Re: fork() can fail

#75

Just as a reminder: "So, malloc on Linux only fails if there isn’t enough memory for its control structures. It does not fail if there isn’t enough memory to fulfill the request." - http://scvalex.net/posts/6/

Malloc can also fail if you're out of address space without necessarily being out of memory . NT does a much better job of separating these concepts than Unix-family operating systems do. Conceptually, setting aside a region of your process's address space and guaranteeing that the OS will be able to serve you a given number of pages are completely different operations. I wish more programs would use MAP_NORESERVE wh…

These days, I describe malloc() as "a function which allocates address space", to avoid confusion. Which means it makes sense that malloc() returns NULL if you are out of address space, even if you have lots of memory. (But so many people don't check malloc()'s return anyway...)

Re: fork() can fail

#76
post #14

Earlier quoted context omitted.

When you see chdir, or any notion of the current working directory being used for anything: run as fast as you can. (or refactor if it's not too late). Things I've seen because of software relying on it.. Sometimes it's just directories/files it creates popping up all over the place, sometimes it's 'just' crashing, but yes sometimes it starts to erase and all hell really breaks loose.

The real problem with chdir is when that code ends up refactored into a library and ends up used in a multithreaded program. Then you've got an ugly bug.

Which probably can be summarized as »global state is a really bad idea«.

Re: fork() can fail

#77
post #69
post #51

Earlier quoted context omitted.

This isn't really fork() failing per se -- but rather a failed program/script that did not understand the well defined and clearly documented behavior of fork().

Psst - it's "per se"...

;-P

Re: fork() can fail

#78

Earlier quoted context omitted.

You should always put a break statement at the end of a default branch. The default case is put at the end as a convention but no one (certainly not the C standard) prevents you from adding a new case after, which will be promptly executed even though probably it's what you want. I think this is mentioned a best practice in the K&R.

You mean, after exit(0) returns?

Not after exit(0) returns, no. Instead, you're going to want it after some programmer removes exit(0) six months from now.

Re: fork() can fail

#79
I don't use fork() that often, but my own paranoia is why I always test for
  if len(some_list) 
But it's just my way of covering my ass in case the laws of physics change during execution, or just in case weird bugs exist like those found in this article.

Re: fork() can fail

#80
post #76

Earlier quoted context omitted.

The real problem with chdir is when that code ends up refactored into a library and ends up used in a multithreaded program. Then you've got an ugly bug.

Which probably can be summarized as »global state is a really bad idea«.

I wonder if it would be worthwhile to forego the notion of a CWD for processes entirely.
Post reply on HN