Subj : why can not setstack in share library? To : comp.programming.threads From : zhouxw Date : Wed Mar 30 2005 01:22 am Following program runs OK on Linux kernel 2.6.9 under root /*test_merge.c*/ /* #gcc -o test_merge test_merge.c -lpthread */ #include #include #include #define PAGE_SIZE 4UL*1024UL*1024UL #define ADDR (0x00000000UL) void thread1(void) { int i=11; printf("first variable of thread1 addr = %p\n", &i); } int main(void) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_t id; int ret; void *addr = NULL; int shmid = 0; key_t key = 600; shmid = shmget(key, PAGE_SIZE, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W|SHM_RND); if(shmid != -1) { addr = shmat(shmid, (void *)ADDR , 0); if (((long)addr) == -1) return 1; } ret = pthread_attr_setstack (&attr, addr , PAGE_SIZE); if(ret != 0 ) return 1; ret = pthread_create(&id,&attr,(void *)thread1, NULL); if(ret != 0 ) return 1; pthread_join(id,NULL); return (0); } But why if I wrapped pthread_create(3),It causeed "Segmentation fault" :( I display the code here: /*my_thread.c*/ #include #include #define PAGE_SIZE 4UL*1024UL*1024UL #define ADDR (0x00000000UL) int my_pthread_create(pthread_t * id, pthread_attr_t * attribute, void * (*start_routine)(void *), void * arg) { pthread_attr_t attr; pthread_attr_init(&attr); int ret; int shmid = 0; void *addr = NULL; key_t key = 566; shmid = shmget(key, PAGE_SIZE, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W|SHM_RND); if(shmid != -1) { addr = shmat(shmid, (void *)ADDR , 0); if (((long)addr) == -1) return 1; } ret = pthread_attr_setstack (&attr, addr , PAGE_SIZE); if(ret != 0 ) return 1; ret=pthread_create(id,&attr,(void *)start_routine, NULL); if(ret != 0 ) return 1; return (0); } /*test.c*/ #include #include #include "my_thread.h" void thread1(void) { int i=11; printf("first variable of thread1 addr = %p\n", &i); } int main(void) { pthread_t id; int ret; ret = my_pthread_create(&id,NULL,(void *)thread1, NULL); if(ret != 0 ) printf("errno = %d\n",errno); pthread_join(id,NULL); return (0); } /*my_thread.h*/ #include #include extern int my_pthread_create(pthread_t * id, pthread_attr_t * attr1, void * (*start_routine)(void *), void * arg); /*Makefile*/ TEST: gcc -fpic -c -g my_thread.c my_thread.h gcc -shared -Wl,-soname,libmy_thread.so -o libmy_thread.so my_thread.o cp ./libmy_thread.so /lib/ cp ./libmy_thread.so /usr/lib gcc test.c -o test -lmy_thread -lpthread clear: rm -f *.o libmy_thread.so test .