【发布时间】: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