首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

pthread_attr_setstacksize 跟pthread_attr_getstack 的大小不一样

2013-04-21 
pthread_attr_setstacksize 和pthread_attr_getstack的大小不一样创建 栈大小 65530 至 72660 的线程,并打

pthread_attr_setstacksize 和pthread_attr_getstack 的大小不一样

创建 栈大小 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;
}

thread 线程 栈大小
[解决办法]
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]. 


热点排行