When I write a small script with fork, the syscall returns twice processes (once per process):
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int pid = fork();
if (pid == 0) {
// child
} else if (pid > 0) {
// parent
}
}
If I instrument that with systemtap, I only find one return value:
// fork() in libc calls clone on Linux
probe syscall.clone.return {
printf("Return from clone\n")
}
(SystemTap installes probes on _do_fork
instead of clone, but that shouldn't change anything.)
This confuses me. A couple of related questions:
- Why does the syscall only return once?
- If I understand the
_do_fork
code correctly, the process is cloned in the middle of the function. (copy_process
andwake_up_new_task
). Shouldn't the subsequent code run in both processes? - Does the kernel code after a syscall run in the same thread / process as the user code before the syscall?