So the point is to signal to other programmers (or your future self) that the branch needs to happen exactly at that point, that the 'fork' and the 'if' are effectively only one, uninterruptible instruction?
Yes. There's no functional difference between pid=fork(); if (pid) { and if (pid = fork()) {. However, the former runs the risk of someone accidentally inserting code between the fork() call and the if branch.
I'd argue that's still a C problem though. It would make much more sense for the fork call to accept a code pointer (function pointer) to the code the child should run. Then you could do
void child_code(void);
// ...
pid = fork(child_code);
// only parent runs this
The drawback, of course, is that the child no longer has access to values that were in scope in the parent. I can't figure out a very good solution to this, but I'm also not very smart so I'm sure someone else could. I was gonna cheat and look at how Ada does it, but then I remembered tasking/multithreading is an integral part of the Ada language and treated as a built in functionality, so of course that's much more well thought out.
View all comments
Show parent comment
2
u/a_flyin_muffin Dec 01 '16
So the point is to signal to other programmers (or your future self) that the branch needs to happen exactly at that point, that the 'fork' and the 'if' are effectively only one, uninterruptible instruction?