linux - Thread management in a cloned process with CLONE_VM flag set -
i need thread management inside cloned process. point need create , destroy threads inside cloned process asynchronously (pthread_cancel_asynchronous set). fine if clone_vm not set, need because main , child process must share same address space. below example demonstrate question. being compiled clone_vm flag program hangs @ pthread_join while without clone_vm fine. there ways deal problem?
p.s. tried @ debian gnu/linux versions 7,8 , unstable (kernels 3.2, 3.16, 4.0).
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <sched.h> #include <string.h> #include <signal.h> #include <sys/types.h> #include <sys/wait.h> /* compile # gcc -o pthread_test -lpthread pthread_test.c */ #define clone 1 pthread_mutex_t mutex = pthread_mutex_initializer; void *thread_test(void *unused) { pthread_setcanceltype(pthread_cancel_asynchronous, null); pthread_setcancelstate(pthread_cancel_enable, null); pthread_mutex_unlock(&mutex); pause(); return null; } int test(void *unused) { void *status; int r; pthread_t thread; pthread_mutex_lock(&mutex); if ((r = pthread_create(&thread, null, thread_test, null)) != 0) { printf("pthread_create returned %d\n", r); exit(1); } pthread_mutex_lock(&mutex); printf("cancelling thread\n"); r = pthread_cancel(thread); printf("pthread cancel returned : %s(%d)\n", strerror(r), r); printf("joining thread\n"); r = pthread_join(thread, &status); printf("pthread_join returned : %s\n", strerror(r)); if (status == pthread_canceled) printf("child canceled\n"); else printf("child exit status %u\n", (unsigned)status); return 0; } int main(void) { #if clone void *stack = (void *)malloc(16384); if (clone(test, stack + 16383, clone_vm | clone_fs | clone_files, null) < 0) { printf("clone failed\n"); exit(1); } waitpid(0, null, __wclone); return 0; #else void *stack = (void *)malloc(16384); if (clone(test, stack + 16383, /*clone_vm |*/ clone_fs | clone_files, null) < 0) { printf("clone failed\n"); exit(1); } waitpid(0, null, __wclone); return 0; #endif }
Comments
Post a Comment