Appel système brute

J’essaie d’utiliser le système de clonage brut, mais je n’ai trouvé aucune documentation appropriée. J’ai essayé d’écrire un petit programme pour l’essayer, mais cela a entraîné une erreur de segmentation.

Je ne peux pas comprendre où je me trompe.

voici la petite 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; } 

Je ne peux pas dire que je recommande d’aller avec clone si vous pouvez utiliser pthreads. J’ai eu une mauvaise expérience avec des fonctions telles que malloc () en relation avec clone.

Avez-vous consulté la page de manuel pour la documentation?

Voici un exemple qui fonctionne pour moi. Je n’ai pas vraiment examiné votre code pour voir pourquoi il pouvait tomber en panne.

 #define _GNU_SOURCE #include  #include  #include  #include  #include  #include  #include  #include  // Allow us to round to 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 i = 0; for (i = 0; i < 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; }