I am having a problem with the clone function in that it give me a segmentation fault after the 9th call to it. Program works fine until I get up to using 9+ threads.
here is my call to clone:
void **child_stack = (void **) malloc(SIGSTKSZ);
clone (func,
(child_stack + SIGSTKSZ),
CLONE_VM | CLONE_FILES | CLONE_PARENT_SETTID,
(void *) argsForFunc,
&pid);
I am using clone and not any higher level threads library like pthreads.
If it helps, this is the error I get when using GDB:
Program received signal SIGSEGV, Segmentation fault.
clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:66
66 movq %rcx,8(%rsi)
Current language: auto; currently asm
TJD's comment above made me see the problem right away: your pointer arithmetic to get the end of the stack is incorrect. If you allocate the stack as:
void **child_stack = (void **) malloc(SIGSTKSZ);
And then you calculate the top of the stack as:
child_stack + SIGSTKSZ
The actual address passed to clone will be
child_stack + sizeof(void*)*SIGSTKSZ
Maybe you meant for child_stack
to have type char*
? sizeof(char)
is 1 by definition, so that would give you the right result.