【问题标题】:Is there an equivalent to the clone() syscall on macOS?macOS 上是否有与 clone() 系统调用等效的功能?
【发布时间】:2020-01-04 08:53:14
【问题描述】:

作为the one in 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()。如果你想实现线程,你应该使用pthread
  • 另外,您应该指定是否要在 C 或 C++ 中执行此操作。使用两者进行标记是不明确的。
  • 哦,对不起,它是用 C++ 编写的

标签: c++ macos system-calls


【解决方案1】:

如上所述 herehere clone 特定于 Linux。

macOS system calls you can do 包括 forkvfork,因此您可以使用其中之一。

另请参阅 this answer 了解有关 clonefork 的一些推理并阅读手册页:

【讨论】:

    猜你喜欢
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2011-01-17
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多