by kalmar on 4/14/16, 4:46 AM with 81 comments
by zuzun on 4/14/16, 8:23 AM
if let Some(child) = fork() {
do_only_child_stuff();
} else {
do_only_parent_stuff();
}
or if let Some(ForkResult::Child) = fork() {
do_only_child_stuff();
} else {
do_only_parent_stuff();
}
Now, if you're about to tell me that the examples above are totally stupid and no
developer would do such a thing, then you know how I feel about the sloppy
C versions. Doing a system call and not checking for error is totally
stupid as well.By the way, you can also write your own wrapper functions in C, that transform the return value into something like
struct fork_status {
enum { ERROR, PARENT, CHILD } state;
int ret;
};
Then Clang and GCC will warn you about missing switch cases.That said, the libc bindings in Rust are pretty low-level and a project that offers higher-level wrappers can be very helpful, so I hope my comment doesn't create the impression that I'm ripping on the project itself.
by geocar on 4/14/16, 7:18 AM
by subway on 4/14/16, 6:18 AM
by justincormack on 4/14/16, 12:44 PM
by bigger_cheese on 4/14/16, 6:35 AM
pid_t childPid;
switch (childPid = fork()) {
case -1: ... /error handling /;
case 0: ... /Child Specific/
default: sleep (5); }
edit - seems to mangle formatting but something like that seems fairly clean.
by sergiolp on 4/14/16, 7:15 AM
Adding new abstraction layers rarely helps when doing systems programming. You (as in "the developer") want to be as near to the machine as possible. C does this pretty well.
Perhaps I'm just getting old :-(
by superobserver on 4/14/16, 1:57 PM
by SixSigma on 4/14/16, 6:53 AM
The function of kill is to kill a given pid, so there are two failure modes : "the pid didn't exist" or "the pid didn't die"
by bogomipz on 4/15/16, 2:27 AM
by ZephyrP on 4/14/16, 5:06 PM
by etrain on 4/14/16, 7:00 AM
by larozin on 4/14/16, 7:35 AM
by peterwwillis on 4/14/16, 3:36 PM
Someone never had to bit-pack their programs to save memory, disk space, or bandwidth. In fact, it's a huge waste of memory; if you only need 3 bits, a 'char' would have sufficed. Saves 24 bits!
Of course, we could use nibbles to make data structures where the fork return value only takes up 3 bits instead of a whole byte, but that could be considered micro-optimizing. (the compiler may do this for us anyway, though)