是否有等效于 macOS 上的 clone() 系统调用?

Is there an equivalent to the clone() syscall on macOS?

本文关键字:clone 系统调用 上的 macOS 是否      更新时间:2023-10-16

作为Linux中的那个,我可以在其中传递要在子项中执行的函数,要使用的内存等作为参数。我附上一个示例,其中我正在尝试启动一个子进程,该子进程将使用stack_memory()中分配的内存执行chld_func函数。

#include <iostream>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
// ...
int main(int argc, char** argv)
{
printf("Hello, World! (parent)n");
clone(chld_func, stack_memory(), SIGCHLD, 0);
wait(nullptr);
return EXIT_SUCCESS;
}

也许我可以尝试使用fork()做类似的事情,但我不知道从哪里开始。

提前感谢!

正如这里和这里所说clone特定于Linux。

您可以执行的macOS系统调用包括forkvfork,因此您可以使用其中之一。

另请参阅此答案,了解有关clonefork以及读取手册页的一些推理:

  • http://man7.org/linux/man-pages/man2/clone.2.html
  • http://man7.org/linux/man-pages/man2/vfork.2.html
  • http://man7.org/linux/man-pages/man2/fork.2.html