0

克隆导致分段错误

代码 :

#define STACKSIZE 16384
int variable ;
using namespace std ;
int do_something(void *) {
    variable = 42;
return 0 ;
}

int main() {
void *child_stack;
variable = 9;
child_stack = (void *) malloc(STACKSIZE);
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);
free(child_stack);
return 0;
}
4

1 回答 1

3

阅读手册页clone

堆栈在所有运行 Linux 的处理器(HP PA 处理器除外)上向下增长,因此 child_stack 通常指向为子堆栈设置的内存空间的最高地址。

所以我会尝试这样的事情:

char* child_stack = (char*) malloc(STACKSIZE);
child_stack += STACKSIZE - 1;  // set it to the topmost address
                               // of allocated space

clone(do_something, (void*) child_stack, CLONE_VM|CLONE_FILES, NULL);
于 2012-09-15T13:22:39.603 回答