c++中克隆系统调用语句出错
error in clone system call sentence in c++
我正试图在内部使用克隆的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 %dn", variable);
clone(do_something, child_stack,CLONE_VM|CLONE_FILES, NULL);
sleep(1);
printf("The variable is now %dn", variable);
if (read(fd, &tempch, 1) < 1) {
perror("File Read Error");
exit(1);
}
printf("We could read from the filen");
return 0;
}
我得到了错误:
dell@ubuntu:~$g++n.cpp-o nn.cpp:在函数"int main()"中:n.cpp:40:62:错误:从"int()()"到"int()/usr/include/x86_64-linux-gnu/bits/sched.h:83:12:错误:初始化"int clone(int()(void),void*,int,void*…)"的参数1[-f许可]dell@ubuntu:~$
编译器告诉您,clone
的第一个参数应该是int(*)(void*)
(指向接受一个void*
参数并返回int
的函数的指针),您正试图将其传递给int(*)()
(指向接受无参数且返回int
的函数的指示器)。
前者不能隐式转换为后者,因此会出现错误。
要修复它,可以将do_something
定义为:
int do_something(void*)
{
// your code
}
您真的不应该使用clone(2)系统调用。它(有点)像futex(2)一样保留给pthreads的实现。C++11标准实际上要求pthread链接到已编译的应用程序中。
如果您想使用clone
(这可能是一个错误),请将自己重新设置为C,并小心避免需要pthread
库,即使是间接的;通过您的申请。
如果您坚持使用clone
,那么它的child_stack
参数应该适当对齐(至少与4K字节的页面对齐),而malloc
不能保证这一点。您可以使用mmap
或posix_memalign
。
但实际上,不要使用clone
(特别是不要使用C++)。使用pthreads。
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- C++ 使用 OpenMP 时,如果 for 循环在 if else 语句中出错
- R6010 执行后出错.if 语句在 for 循环中.[作业][中止已解决,发现新问题]
- 关闭文件流时出错,其存在取决于与 C++ 中的 .close() 语句相同的布尔值
- 预期在 if 和 else 语句上的语句上出错
- c++中克隆系统调用语句出错
- 在IF语句中添加时出错
- 编写 if 语句时出错"'=' : left operand must be l-value"
- 我的代码中的switch语句出错.(c++)
- 使用 if 语句时出错