You need to know which direction stack is growing on your processor, and you need to know which end of the stack you should go to clone () for.
From man clone :
Stacks grow downwards 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.
You do not miss the highest address, you pass the lowest address, and you do not (I guess) on HP-PA.
Fix:
child_stack = (void **) malloc(16384) + 16384 / sizeof(*child_stack);
PS I am amazed at the number of obviously wrong non-answers here.
- No, closing an invalid file descriptor does not crash on any UNIX and Linux system exists.
- No,
void* vs. void** has nothing to do with this problem. - No, you do not need to take the address do_something, the compiler will do this automatically for you.
And finally, yes: calling close , _exit or any other libc procedure in the clone()d thread is potentially unsafe, although this does not cause a problem here.
source share