URN Logo
UNIX Resources » Linux » China Linux Forum » C/C++编程版 » 36 » 为什么一个函数写到共享库里就出错?(内附可执行代码)
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
为什么一个函数写到共享库里就出错?(内附可执行代码)
 
 
Subject: 为什么一个函数写到共享库里就出错?(内附可执行代码)
Author: ymjzxw    Posted: 2005-03-25 15:15    Length: 6,098 byte(s)
[Original] [Print] [Top]
函数功能就是封装pthread_create(),以改变系统默认的栈的大小和地址,原程序如下:
/*test_merge.c*/
#include <pthread.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>

#define PAGE_SIZE 2UL*1024UL*1024UL
#define ADDR (0x00000000UL)

void thread1(void)
{
int i=11,ret=12;
printf("first variable of thread1 addr = %p ", &i);
printf("first variable of thread1 addr = %p ", &ret);
}
int main(void)
{
pthread_attr_t attr;
pthread_attr_init(&attr); //initialize thread attribute object
pthread_t id;
int i=0,ret;
unsigned long size;

void *stackaddr = NULL;
size_t stacksize = 0;
void *addr = NULL;

addr = malloc(PAGE_SIZE);
printf("addr = %p ",addr);
stacksize = PAGE_SIZE;
ret = pthread_attr_setstack (&attr, addr , stacksize);
if(ret == 0 && errno == 0) printf("thread pthread_attr_setstack success! ");
else
{
printf("ret = %d ",ret);
printf("errno = %d ",errno);
return 1;

}
ret = pthread_create(&id,&attr,(void *)thread1, NULL);
if(ret == 0 && errno == 0)
{
printf("thread %d LPG_pthread_create success! ",i);
}
else
{
printf("thread %d LPG_pthread_create fail! ",i);
printf("ret = %d ",ret);
printf("errno = %d ",errno);
}
pthread_join(id,NULL);
return (0);
}
运行没有问题,线程栈大小和地址都改变了,但是写成共享库就出错,代码如下:
/*my_thread.c*/
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <sys/shm.h>
#include <setjmp.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <string.h>

#define PAGE_SIZE 2UL*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); //initialize thread attribute object
int i=0,ret;
void *stackaddr = NULL;
size_t stacksize = 0;
int shmid = 0;
void *addr = NULL;

addr = malloc(PAGE_SIZE);
printf("addr = %p ",addr);
stacksize = PAGE_SIZE;
ret = pthread_attr_setstack (&attr, addr , stacksize);
if(ret == 0 && errno == 0) printf("thread pthread_attr_setstack success! ");
else
{
printf("ret = %d ",ret);
printf("errno = %d ",errno);
return 1;

}
ret=pthread_create(id,&attr,(void *)start_routine, NULL);
if(ret == 0 && errno == 0)
{
printf("thread pthread_create success! ");
}
else
{
printf("thread pthread_create fail! ");
printf("ret = %d ",ret);
return 1;
}
return (0);
}

/*my_thread.h*/
#include <pthread.h>
extern int my_pthread_create(pthread_t * id, pthread_attr_t * attr1, void * (*start_routine)(void *), void * arg);

/*test.c*/
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include "my_thread.h"

void thread1(void)
{
int i=11,ret=12;
printf("first variable of thread1 addr = %p ", &i);
printf("first variable of thread1 addr = %p ", &ret);
}


int main(void)
{
pthread_t id;
int i=0,ret;
unsigned long size;

ret = my_pthread_create(&id,NULL,(void *)thread1, NULL);
if(ret == 0 && errno == 0)
{
printf("thread %d LPG_pthread_create success! ",i);
}
else
{
printf("thread %d LPG_pthread_create fail! ",i);
printf("ret = %d ",ret);
printf("errno = %d ",errno);
}
pthread_join(id,NULL);
return (0);
}

/*Makefile*/
TEST:
gcc -fpic -c -g my_thread.c
gcc -shared -g -o libmy_thread.so my_thread.o
cp ./libmy_thread.so /lib/
gcc -g test.c -o test -lmy_thread -lpthread
clear:
rm -f *.o libmy_thread.so test

make后运行输出:
addr = 0xb7294008
thread pthread_attr_setstack success!
段错误

但是如果在库里面去掉
ret = pthread_attr_setstack (&attr, addr , stacksize);
这句话,程序就能运行成功.
gdb跟踪进去好象是在allocate_stack()里面调用memset()时出错的,大侠们给我看看吧,谢谢!

[Original] [Print] [Top]
Subject: Re: 为什么一个函数写到共享库里就出错?(内附可执行代码)
Author: ymjzxw    Posted: 2005-03-28 10:57    Length: 16 byte(s)
[Original] [Print] [Top]
斑竹,help me:-(
[Original] [Print] [Top]
« Previous thread
关于2.6内核 epoll()的效率与使用框架,敬请各位大峡!
C/C++编程版
36
Next thread »
有哪位大侠清楚网络服务器软件设计中都需要改那些/proc/sys/net下面的参数啊?谢谢
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 03:53:05, cost 0.03908109664917 ms.