How to change pthread stack size more than 64kbytes ?

How to change pthread stack size more than 64kbytes ?

Post by Sangjoon Ah » Fri, 06 Sep 2002 15:42:43



**** Post for FREE via your newsreader at post.usenet.com ****

Hi.

I have seen in the freebsd implemetation of libc_r(pthread lib) pthreads
have a FIXED stack size and the default is 64kbytes.

This stack size is not variable and the only way to change it is to call
pthread_attr_setstacksize().

I set 1000Kbytes and use local memory more than 64kbytes in child thread.
but it crash.

What's wrong ?

The below source code is a sample.

=======================================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>
#include <limits.h>

#include <errno.h>

void * child(void *_arg)
{
        int i;
        char aa[68000];    // ==>  core dump
        //char aa[60000];  // ==> not core dump

        i = *((int *)_arg);
        free(_arg);

        printf("child %d spawned\n", i);

        return NULL;

Quote:}

int main(int argc, char *argv[])
{
        int     ret, *para;
        size_t  stacksize;

        pthread_t child_thread;
        pthread_attr_t  attr;

        pthread_attr_init(&attr);

        stacksize = 1000;
        if (!pthread_attr_setstacksize(&attr, stacksize*1024)) {
                printf("set stacksize = %dKB\n", stacksize);
        }

        if (!pthread_attr_getstacksize(&attr, &stacksize)) {
                printf("stack size = %d\n", stacksize);
        }
        printf("PTHREAD_THREAD_MAX in limits.h is %u\n",
PTHREAD_THREADS_MAX);

        para = malloc(sizeof(int));
        *para = 1;
        if (ret = pthread_create(&child_thread, NULL, (void *(*)(void
*))child, (void *)para)) {
                printf("Test failed : %s (%d)\n", strerror(ret), 1);
                exit(0);
        }

        pthread_join(child_thread, NULL);

        pthread_attr_destroy(&attr);
        return (0);

Quote:}

g-test4# gcc thread_aa.c -pthread

g-test4# ./a.out
set stacksize = 1000KB
stack size = 1024000
PTHREAD_THREAD_MAX in limits.h is 4294967295
Segmentation fault (core dumped)

=======================================================

Thanks in advance.

--
Sangjoon Ahn

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
                      http://www.usenet.com
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

 
 
 

How to change pthread stack size more than 64kbytes ?

Post by Patrick TJ McPh » Sat, 07 Sep 2002 15:13:28


% I have seen in the freebsd implemetation of libc_r(pthread lib) pthreads
% have a FIXED stack size and the default is 64kbytes.

All pthread implementations have a fixed size stack of some size. It's
necessary due to the common address space (thread 2's stack has to
go somewhere, which automatically limits the size of thread 1's stack).
Some platforms use a large stack to reserve the addresses, but commit
real memory only as it's required.

% I set 1000Kbytes and use local memory more than 64kbytes in child thread.
% but it crash.

You didn't really do this.

%         pthread_attr_init(&attr);
%
%         stacksize = 1000;
%         if (!pthread_attr_setstacksize(&attr, stacksize*1024)) {
%                 printf("set stacksize = %dKB\n", stacksize);
%         }
%
%         if (!pthread_attr_getstacksize(&attr, &stacksize)) {
%                 printf("stack size = %d\n", stacksize);
%         }
%         printf("PTHREAD_THREAD_MAX in limits.h is %u\n",
% PTHREAD_THREADS_MAX);

Here, you allocate a thread attribute and set the stack size for it.

%         if (ret = pthread_create(&child_thread, NULL, (void *(*)(void
% *))child, (void *)para)) {

And here you create a thread without using that thread attribute.
Try passing attr as the second argument to pthread_create().

--

Patrick TJ McPhee
East York  Canada