3 ms·
Fault is not a rivalrous good. It's the user's fault, and it's the API creator's fault. Is there a reason fork can't be changed to just crash the program on fa
by rwallace 12y ago
Fault is not a rivalrous good. It's the user's fault, and it's the API creator's fault.
Is there a reason fork can't be changed to just crash the program on failure? Are situations where a program usefully does something other than crash on fork failure, more or less common than situations where a program fails in the way described in the article?
- paulfurtado 12y agoI can't think of any good cases for crashing a program when fork fails. If you're doing work in a pool of processes and the parent process tries to fork another process, but fails, there are many ways to handle this: wait a few seconds before trying again, wait for another process to finish, kill a few processes, etc. If you were forking in a high-level language such as Python, failing to fork would raise an exception which would possibly crash the program if left unhandled. C does not have exceptions so return codes are used to indicate success or failure. This is true for nearly every function, not just fork. If you're not checking for errors in a C program, it's going to break in unexpected ways, and will possibly be vulnerable to exploitation. fork has 3 possible return values: - 0 for the child process - a positive number for the parent process - a negative number if it failed. If you look at the man page's "Return Value" section, it is extremely clear, see: http://linux.die.net/man/2/fork http://linux.die.net/man/2/fork "On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately."
- Sanddancer 12y agoThat should be up to the programmer to decide. If fork() fails, it's almost always a transient situation that can be recovered from by spinning until fork() succeeds. Or one could have jobs doing useful things that can finish up, state saved, etc, for when the process is re-run. Just dropping everything onto the floor is usually the worst option.
- dllthomas 12y ago"Is there a reason fork can't be changed to just crash the program on failure?" YES. Situation: fork bomb, can't create new processes. How do you notice? Typically because you can't spawn new processes from your shell because fork is failing. With bash, there is a kill builtin - so you have a chance of cleaning things up (depending) if you have a shell open. If the failed fork kills the shell, then oops, you don't have a shell open.