Reputation: 135
Clone causes a segmentation fault
code :
#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;
}
Upvotes: 0
Views: 1079
Reputation: 55395
Read the man page for clone
:
Stacks grow downward on all processors that run Linux (except the HP PA processors), so child_stack usually points to the topmost address of the memory space set up for the child stack.
So I'd try something like this:
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);
Upvotes: 3