I'm trying to experiment with how clone() is implemented for threads in Linux 3.10.0-327.3.1.el7.x86_64
I'm running this piece of code and having occasional segfaults. I know if I use CLONE_THREAD then there's no way to check if the thread finished, but why does printf cause problems? How does the Pthread library handle this issue? Without the printfs there's no segfault.
#define STACK_SIZE (1ULL<<22) //4MB
int tmp = 10;
int threadfunc(void *ptr)
{
printf("i'm here\n");
tmp++;
return 0;
}
int main()
{
char *child_stack = malloc(STACK_SIZE);
int child_pid = clone(&threadfunc, child_stack+(STACK_SIZE)-1, CLONE_THREAD|CLONE_SIGHAND|CLONE_VM|CLONE_FS|CLONE_FILES|SIGCHLD, NULL);
printf("my pid: %d\n", getpid());
printf("child pid: %d\n", child_pid);
printf("tmp is: %d\n", val);
return 0;
}