Following code causes "Segmentation fault" (kernel:linux2.6.9) /*my_thread.c*/ #include <pthread.h> #include <sys/shm.h> #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 <stdio.h> #include <errno.h> #include "my_thread.h" void thread1(void) { int i=11; printf("first variable of thread1 addr = %pn", &i); } int main(void) { pthread_t id; int ret; ret = my_pthread_create(&id,NULL,(void *)thread1, NULL); if(ret != 0 ) printf("errno = %dn",errno); pthread_join(id,NULL); return (0); } /*my_thread.h*/ #include <pthread.h> #include <sys/shm.h> 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
If the shmget call fails, the program will of course crash.
If you run it in kernel 2.6 ang use ROOT,shmget call success
Show me the strace output of a run where shmget is successful and the program crashes.