I have a library which registers an atfork
handler (via pthread_atfork()
) which does not support multiple threads when fork()
is called. In my case, I don't need the forked environment to be usable because all I want is to call exec()
right after the fork()
. So, I want the fork()
but without any atfork
handlers. Is that possible? Do I miss any important edge cases?
For background info, the library is OpenBlas, the issue is described here and here.
You could use
vfork()
(NPTL implementation doesn't call fork handlers). Although POSIX has removedvfork
from the standard, it's likely available on your implementation.Or,
posix_spawn()
. This is similar tovfork
. Man page says:Or,
syscall
and directly useSYS_clone
.SYS_clone
is the system call number used to create threads and processes on Linux. Sosyscall(SYS_clone, SIGCHLD, 0);
should work, provided you would exec immediately.syscall(SYS_fork);
(as answered by Shachar) would likely work too. But note thatSYS_fork
not available on some platforms (e.g., aarch64, ia64).SYS_fork
is considered as obsolete in Linux and it's only there for backward compatibility and Linux kernel uses SYS_clone for creating all "types" of processes.(Note: These options are mostly limited to glibc/Linux).
Yes. The following should work on Linux (and, I think, all
glibc
based platforms):This bypasses the library and directly calls the system call for
fork
. You might run into trouble if your platform does not implementfork
as a single system call. For Linux, that simply means that you should useclone
instead.With that in mind, I'm not sure I'd recomment doing that. Since you're a library, you have no idea why someone registered an
atfork
. Assuming it's irrelevant is bad programming practice.So you lose portability in order to do something that may or may not break stuff, all in the name of, what? Saving a few function calls? Personally, I'd just use
fork
.