【发布时间】:2012-09-08 08:55:46
【问题描述】:
我正在尝试在内部使用克隆的 c++ 中运行 c 代码,我遇到了一个我无法解决的错误,任何人以前在 c++ 中使用过克隆,并且可以提供帮助。
我的代码:
int variable, fd;
using namespace std ;
int do_something() {
variable = 42;cout << "sana" << endl ;
close(fd);
_exit(0);
}
int main() {
void **child_stack;
char tempch;
variable = 9;
fd = open("test.file", O_RDONLY);
child_stack = (void **) malloc(16384);
printf("The variable was %d\n", variable);
clone(do_something, child_stack,CLONE_VM|CLONE_FILES, NULL);
sleep(1);
printf("The variable is now %d\n", variable);
if (read(fd, &tempch, 1) < 1) {
perror("File Read Error");
exit(1);
}
printf("We could read from the file\n");
return 0;
}
我得到了错误:
dell@ubuntu:~$ g++ n.cpp -o n n.cpp:在函数“int main()”中: n.cpp:40:62:错误:从“int ()()”到“int ()(void*)”的无效转换 [-fpermissive] /usr/include/x86_64-linux-gnu/bits/sched.h:83:12: 错误:初始化参数 1 的 'int clone(int ()(void), void*, int, void*, ...)' [-fpermissive] dell@ubuntu:~$
【问题讨论】:
-
我相信您对 child_stack** 的使用导致了 SEGFAULT。虽然不知道 clone() 的实现和期望,但我无能为力。
-
我刚刚查找了 clone() 的原型。看来您应该在使用这种方式的两个地方将 ** 替换为 *。
-
还有16384字节的栈够大吗?
-
我试过** by *,结果是一样的,关于堆栈我认为足够了,因为它与子实现有关,它只是克隆方法中的局部变量,非常eimple并没有声明任何东西!但不确定 100%!