i trying use raw clone system, not find proper documentation. tried write small program try it, ends segmentation fault.
i cannot understand wrong.
here small application :
define stack_size 0x10000 define bufsize 200 #define _gnu_source void hello (){ fprintf(stderr,"hello word\n"); _exit(0); } int main() { int res; void *stack = mmap(0, stack_size, prot_read|prot_write, map_private|map_anonymous, -1, 0); pid_t ptid, tid; printf("stack %p\n", stack + stack_size); memset(stack, 0, stack_size); res= syscall(sys_clone,clone_sighand|clone_fs|clone_vm|clone_files,stack + stack_size, &tid,&ptid,null ); if (!res) hello(); printf("clone result %x\n", res); waitpid(-1, null, __wall); return 0; }
i can't recommend going clone if can use pthreads. i've had bad experience functions such malloc() in relation clone.
have looked @ man page documentation?
here example runs me. didn't examine code see why might crashing.
#define _gnu_source #include <stdio.h> #include <sched.h> #include <sys/types.h> #include <sys/wait.h> #include <linux/sched.h> #include <stdlib.h> #include <unistd.h> #include <assert.h> // allow round page size #define round_up_to_multiple(a,b) \ ( ( (a) % (b) == 0) ? (a) : ( (a) + ( (b) - ( (a) % (b) ) ) ) ) struct argsy { int threadnum; }; int fun(void * args) { struct argsy * arguments = (struct argsy *) args; fprintf(stderr, "hey!, i'm thread %d\n", arguments->threadnum); return 0; } #define n_threads 10 #define pagesize 4096 struct argsy arguments[n_threads]; int main() { assert(pagesize==getpagesize()); const int thread_stack_size = 256*pagesize; void * base = malloc((((n_threads*thread_stack_size+pagesize)/pagesize)*pagesize)); assert(base); void * stack = (void *)round_up_to_multiple((size_t)(base), pagesize); int = 0; (i = 0; < n_threads; i++) { void * args = &arguments[i]; arguments[i].threadnum = i; clone(&fun, stack+((i+1)*thread_stack_size), clone_files | clone_vm, args); } sleep(1); // wait not implemented return 0; }
Comments
Post a Comment