I don't think that algorithm works, but I'm probably misunderstanding:
char *shared, *private;
bool check_fork() {
return ++*shared != ++*private;
}
shared = mmap(MAP_SHARED...);
private = mmap(MAP_PRIVATE...);
*shared = 0;
*private = 0;
check_fork();
if (fork() == 0) {
printf("%d\n", check_fork());
} else {
wait();
}
The only way it could work is if you were guaranteed to also call check_fork synchronously in the parent. But if you had that degree of control over forks, then you just know when your forks are and you don't need these tricks.
The use case here is in an external library (e.g., OpenSSL) where it might be called post-fork by a semi-naive user of the library, or where the library might explicitly want to be usable post-fork (e.g., Apache prefork serving) and need to adjust some state. If you expect the caller to reliably inform the library of forks, then you don't need any trickery.
Another use case that I'm interested in is signal handlers, which could be delivered immediately post-fork. If your handler does something like write to a file descriptor (which is very common), those file descriptors mean something different in the parent and the child, so if the post-fork code in the child was in the middle of switching out file descriptors before exec, you might accidentally write to the wrong thing. So you want to be sure that you're still in the same process before writing to a particular file descriptor. UNIX doesn't offer a way to reset signal handlers on fork, unfortunately.