Linux中线程栈测试

#include<stdio.h>
#include<stdlib.h>
//#include<types.h>
//#include<stat.h>
#include<pthread.h>
#include<string.h>
#include<limits.h>

void* thread_func(void* arg)
{
    printf("The thread is here\n");
    char p[1024*1024*7];
    int i = 1024*1024*7;

    /*确定分配内存成功了*/
    while(i--)
        p[i]= 3;
    printf("Get 7M Memory!!!\n");
    char p2[1024*1024*3];
    memset(p2, 0, sizeof(char)*(1024*1024*3));
    printf("Get More Memory!!!\n");
    pthread_exit(NULL);
}

int main(void)
{
    pthread_t thread_id;
    pthread_attr_t thread_attr;
    size_t stack_size;
    int status;

    status = pthread_attr_init(&thread_attr);
    if(status != 0)
        return status;
    status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
    if(status != 0)
        return status;
    /*未设置线程栈大小之前的大小*/
    status = pthread_attr_getstacksize(&thread_attr, &stack_size);
    printf("Default stack size is %u; minimum is %u\n", stack_size, PTHREAD_STACK_MIN);
    /*未改变线程栈大小之前创建线程*/
//  status = pthread_create(&thread_id, &thread_attr, thread_func, NULL);
//  if(status != 0)
//      return status;
//  sleep(5);
    /*设置线程栈的大小*/
    status = pthread_attr_setstacksize(&thread_attr, PTHREAD_STACK_MIN*1024);
    if(status != 0)
        return status;
    /*输出设置线程栈大小数值*/
    status = pthread_attr_getstacksize(&thread_attr, &stack_size);
    if(status != 0)
        return status;
    printf("After set: stack size is %u; minimum is %u\n", stack_size, PTHREAD_STACK_MIN);

    status = pthread_create(&thread_id, &thread_attr, thread_func, NULL);
    if(status != 0)
        return status;
    sleep(5);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值