【问题标题】:error in clone system call sentence in c++C ++中克隆系统调用语句中的错误
【发布时间】: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%!

标签: c++ linux clone


【解决方案1】:

编译器告诉您clone 的第一个参数应该是int(*)(void*)(一个指向带有void* 参数并返回int 的函数的指针)并且您正在尝试传递它int(*)() (一个指向函数的指针,带有 no 参数并返回 int)。

前者不能隐式转换为后者,故报错。

要修复它,您可以将do_something 定义为:

int do_something(void*)
{
    // your code
}

【讨论】:

  • @jrock Thnax 很多,但它在运行时给了我分段错误,你能看到我编辑文本的问题吗?谢谢你
  • @sana 该问题值得单独提出一个问题 - 请发布另一个问题,不要编辑这样的问题,因为它会使答案过时。我会回滚你最近的编辑。
【解决方案2】:

你真的不应该使用clone(2) 系统调用。对于 pthread 的实现,它是(某种)保留的 - 类似于 futex(2)-。而 C++11 标准实际上要求将 pthread 链接到已编译的应用程序中。

如果你想使用clone(这很可能是一个错误),请将自己限制为C,并小心避免需要pthread库,即使是间接的;由您的应用程序。

如果你坚持使用clone,它的child_stack 参数应该适当对齐(至少到4Kbytes 的页面),malloc 不保证这一点。您可以使用mmapposix_memalign

但实际上,不要使用clone(特别是不要使用 C++)。使用 pthread。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多