Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c+;中克隆系统调用语句出错+; 我试图在C++中运行C代码,里面使用克隆,我有一个错误,我不能解决它,任何人以前都使用C++中的克隆,并且可以帮助。_C++_Linux_Clone - Fatal编程技术网

c+;中克隆系统调用语句出错+; 我试图在C++中运行C代码,里面使用克隆,我有一个错误,我不能解决它,任何人以前都使用C++中的克隆,并且可以帮助。

c+;中克隆系统调用语句出错+; 我试图在C++中运行C代码,里面使用克隆,我有一个错误,我不能解决它,任何人以前都使用C++中的克隆,并且可以帮助。,c++,linux,clone,C++,Linux,Clone,我的代码: 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 =

我的代码:

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;
}
int变量,fd;
使用名称空间std;
int do_something(){

variable=42;cout编译器告诉您,
clone
的第一个参数应该是
int(*)(void*)
(一个指向函数的指针,它取一个
void*
参数并返回
int
),您正试图传递它
int(*)(
(指向不带参数并返回
int
的函数的指针)

前者不能隐式转换为后者,因此存在错误

要修复它,您可以将
do\u something
定义为:

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

您真的不应该使用系统调用。它(某种程度上)类似于pthreads的实现。而C++11标准实际上要求将pthreads链接到已编译的应用程序中

如果要使用
clone
(这可能是一个错误),请使用C,并小心避免应用程序需要的
pthread
库,即使是间接的

如果坚持使用
clone
,则其
child_stack
参数应适当对齐(至少对齐到4KB的页面),并且
malloc
不能保证这一点。您可以使用
mmap
posix\u memalign


但实际上,不要使用克隆(特别是不使用C++)。使用pthreads。

@jrock Thnax很多,但它在运行时给了我一个分段错误,您能看到问题a吗?我编辑了文本,谢谢u@sana这个问题值得一个单独的问题-请发布另一个问题,不要编辑这样的问题,因为这会使答案过时。我将回滚您最新的编辑。我相信您对child的使用_堆栈**导致了SEGFAULT。虽然不知道clone()的实现和期望,但我无能为力。我只是查找了clone()的原型。看起来你应该在两个地方用*替换**。16384字节的堆栈是否足够大?我尝试了**by*,结果是一样的,关于堆栈,我认为这已经足够了,因为它与子实现相关,而子实现只是克隆方法中的局部变量,非常简单且不明确什么都可以!但我不能百分之百确定!