pthread_attr_setstacksize 和pthread_attr_getstack 的大小不一样

_阿杜_ 2013-03-27 03:53:35

创建 栈大小 65530 至 72660 的线程,并打印每个线程的设置的栈大小 和 获取出来的栈大小, 发现没有有理论的规律

普通段,线程分配的真实的线程栈大小为16的倍数

但部分段 65530 - 69632 又不满足该规律
----
换在一个其它的linux 中执行发现结果又不一样
65530 - 69632 段满足 16 倍数的分配原则

在两台机器上查看pagesize【使用getpagesize()】都是4096

请大侠指点下,栈的实际分配大小和什么有关系



#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <limits.h>

unsigned int getStackSize(pthread_t task)
{
unsigned int err,size;
pthread_attr_t attr;
void *stack_base = NULL;
memset(&attr,0,sizeof(attr));
if(0 == task)
{
return -1;
}
err = pthread_attr_init(&attr);
if(err !=0)
{
return -1;
}
pthread_getattr_np((pthread_t)task, &attr);
if( 0 == pthread_attr_getstack(&attr,(void*)&stack_base,&size) )
{
err = 0;
}
else
{
err = -1;
}
pthread_attr_destroy(&attr);
return size;
}
void thread_function1()
{

}

void testsize(int stacksize)
{
int err,size;
pthread_t pt1=0;
pthread_attr_t attr1;
memset(&attr1, 0, sizeof(attr1));
err=pthread_attr_init(&attr1);
//printf("pthread_attr_init 1 err=%d\n",err);
int pagesize=getpagesize();
//printf("pagesize=%d\n",pagesize);
//printf("getpagesize()=%d\n",getpagesize());
//printf("stacksize-stacksize pagesize+pagesize=%ld\n",(stacksize-(stacksize%pagesize)+pagesize));
err=pthread_attr_setstacksize(&attr1,stacksize);
//printf("pthread_attr_setstacksize stacksize=%ld err=%d\n",stacksize,err);
err=pthread_create(&pt1,&attr1,thread_function1, NULL);
//printf("pthread_create 1 err=%d\n",err);
size=getStackSize(pt1);
if(stacksize==size)
{
printf("input stacksize== really size==%d\n",stacksize);
}else
{
printf("input stacksize==%d,!=reallly size==%d\n",stacksize,size);
}

pthread_join(pt1,NULL);
}
int main(int argc, char *argv[])
{
printf("PTHREAD_STACK_MIN=%d\n",PTHREAD_STACK_MIN);
int size=65530;
while(size<72660)
{
testsize(size);
size++;
}
return 0;
}

...全文
320 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
_阿杜_ 2013-03-28
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
PTHREAD_ATTR_GETSTACKSIZE Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME pthread_attr_getstacksize, pthread_attr_setstacksize - get and set the stacksize attribute SYNOPSIS #include <pthread.h> int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize); int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); DESCRIPTION The pthread_attr_getstacksize() and pthread_attr_setstacksize() functions, respectively, shall get and set the thread creation stacksize attribute in the attr object. The stacksize attribute shall define the minimum stack size (in bytes) allocated for the created threads stack. RETURN VALUE Upon successful completion, pthread_attr_getstacksize() and pthread_attr_setstacksize() shall return a value of 0; otherwise, an error number shall be returned to indicate the error. The pthread_attr_getstacksize() function stores the stacksize attribute value in stacksize if successful. ERRORS The pthread_attr_setstacksize() function shall fail if: EINVAL The value of stacksize is less than {PTHREAD_STACK_MIN} or exceeds a system-imposed limit. These functions shall not return an error code of [EINTR]. The following sections are informative. ------------- PTHREAD_ATTR_GETSTACKSIZE Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME pthread_attr_getstacksize, pthread_attr_setstacksize - get and set the stacksize attribute SYNOPSIS #include <pthread.h> int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize); int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); DESCRIPTION The pthread_attr_getstacksize() and pthread_attr_setstacksize() functions, respectively, shall get and set the thread creation stacksize attribute in the attr object. The stacksize attribute shall define the minimum stack size (in bytes) allocated for the created threads stack. RETURN VALUE Upon successful completion, pthread_attr_getstacksize() and pthread_attr_setstacksize() shall return a value of 0; otherwise, an error number shall be returned to indicate the error. The pthread_attr_getstacksize() function stores the stacksize attribute value in stacksize if successful. ERRORS The pthread_attr_setstacksize() function shall fail if: EINVAL The value of stacksize is less than {PTHREAD_STACK_MIN} or exceeds a system-imposed limit. These functions shall not return an error code of [EINTR].
哥哥,你又给我来段英文描述,我在描述中也没找到实际栈大小分配的时候是否有其它的潜在规则呀
赵4老师 2013-03-28
  • 打赏
  • 举报
回复
参考Linux相关函数的源代码!
_阿杜_ 2013-03-28
  • 打赏
  • 举报
回复
求指点呀!!!
赵4老师 2013-03-27
  • 打赏
  • 举报
回复
PTHREAD_ATTR_GETSTACKSIZE Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME pthread_attr_getstacksize, pthread_attr_setstacksize - get and set the stacksize attribute SYNOPSIS #include <pthread.h> int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize); int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); DESCRIPTION The pthread_attr_getstacksize() and pthread_attr_setstacksize() functions, respectively, shall get and set the thread creation stacksize attribute in the attr object. The stacksize attribute shall define the minimum stack size (in bytes) allocated for the created threads stack. RETURN VALUE Upon successful completion, pthread_attr_getstacksize() and pthread_attr_setstacksize() shall return a value of 0; otherwise, an error number shall be returned to indicate the error. The pthread_attr_getstacksize() function stores the stacksize attribute value in stacksize if successful. ERRORS The pthread_attr_setstacksize() function shall fail if: EINVAL The value of stacksize is less than {PTHREAD_STACK_MIN} or exceeds a system-imposed limit. These functions shall not return an error code of [EINTR]. The following sections are informative. ------------- PTHREAD_ATTR_GETSTACKSIZE Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME pthread_attr_getstacksize, pthread_attr_setstacksize - get and set the stacksize attribute SYNOPSIS #include <pthread.h> int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize); int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); DESCRIPTION The pthread_attr_getstacksize() and pthread_attr_setstacksize() functions, respectively, shall get and set the thread creation stacksize attribute in the attr object. The stacksize attribute shall define the minimum stack size (in bytes) allocated for the created threads stack. RETURN VALUE Upon successful completion, pthread_attr_getstacksize() and pthread_attr_setstacksize() shall return a value of 0; otherwise, an error number shall be returned to indicate the error. The pthread_attr_getstacksize() function stores the stacksize attribute value in stacksize if successful. ERRORS The pthread_attr_setstacksize() function shall fail if: EINVAL The value of stacksize is less than {PTHREAD_STACK_MIN} or exceeds a system-imposed limit. These functions shall not return an error code of [EINTR].

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧