LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2011, 06:19 PM   #1
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
Increasing Thread Stack Size


All,

I seem to only be able to set my stack size on my linux server to 15000. If I increase it to 20000 I get a Segmentation Fault.

Any idea how I can get the linux OS to increase the stack size?

Code:
   threadRet |= pthread_attr_setstacksize( &m_ThreadAttributes, 15000 );

Last edited by gothrog; 01-23-2011 at 06:30 PM.
 
Old 01-23-2011, 07:41 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
You're not trying to increase the stack size after the thread is created, are you? That can't be done.
 
Old 01-24-2011, 03:26 PM   #3
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Original Poster
Rep: Reputation: 15
No. You only set the thread stacksize once.

I assume that after I find the linux file that controls the allocation of the allowed stacksize it won't have an issue anymore.

I can compile this with the stacksize increase on windows 7 using g++ on cygwin and the thread can allocate the memory without issues.
 
Old 01-24-2011, 07:22 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I was confused by your changing the stack size from 15000 to 20000. At first I thought you meant that the stack size for a thread was already 15000 and you were changing it. But no, apparently you're setting it to 20000 before creating the thread. So what does it mean to change the stack size "from" 15000? Does it mean that 15000 is the default stack size?

Whatever the answer to that question is, on the Linux system I'm running, Debian 5.0.4, where uname -a yields
Code:
Linux tiger 2.6.26-2-686 #1 SMP Sat Dec 26 09:01:51 UTC 2009 i686 GNU/Linux
I find that:
  1. The default stack size is 8388608.
  2. The minimum stack size is 16384, so even trying to set the stack size to 15000 should fail.
  3. Setting the stack size to 20000 works just fine.
As a demo, I take a program which creates two threads and compares the address of a particular variable in one thread's stack with the address of the same variable in another thread's stack. This is a quick and dirty measurement of the stack size. I run the program with the default stack size, with a stack size of 15000, and a stack size of 20000. The output of the program is this:
Code:
=== pthread_attr_setstacksize() is implemented on this system.
minimum stack size is    16384 00004000
default stack size is  8388608 00800000
using default stack size
difference         is  8392704
=== pthread_attr_setstacksize() is implemented on this system.
minimum stack size is    16384 00004000
default stack size is  8388608 00800000
going for stack size     15000 00003A98
Houston, we have a problem on line 113
the problem: Invalid argument
=== pthread_attr_setstacksize() is implemented on this system.
minimum stack size is    16384 00004000
default stack size is  8388608 00800000
going for stack size     20000 00004E20
new     stack size is    20000 00004E20
difference         is    20480
Maybe we can get a handle on your situation by running the program the same way I did. You can do that by running the following shell script. Would you be so kind as to do so and post the results in this thread?
Code:
rm -f 1; cat > 1.c <<EOD; gcc -pthread -Wall -Werror 1.c -o 1; ./1; ./1 15000; ./1 20000
#include <bits/local_lim.h>
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*--------------------------------------------------------------------------*/

/*
 * x_stage gets 0 initially.  The subsidiary thread sets it to 1 to indicate
 * that the variable pointer has been set.  The main thread sets it to 2 to
 * indicate that the thread may exit.
 */

typedef struct x_s
{
  int              x_thread_number;
  pthread_t        x_thread_id;
  pthread_mutex_t  x_mutex;
  pthread_cond_t   x_cond;
  int              x_stage;
  char            *x_variable_pointer;

} x_t,
 *x_p;

/*--------------------------------------------------------------------------*/

void
check(int line_number,int expression)
{
  if(expression)
  {
    fprintf(stderr,
            "Houston, we have a problem on line %d\n",
            line_number
           );

    errno=expression;

    perror("the problem");

    exit(1);
  }

} /* check() */

/*--------------------------------------------------------------------------*/

void *
doit(void *xx)
{
  char something;

  ((x_p)xx)->x_variable_pointer=&something;

  check(__LINE__,pthread_mutex_lock(&((x_p)xx)->x_mutex));

  ((x_p)xx)->x_stage=1;

  check(__LINE__,pthread_cond_signal(&((x_p)xx)->x_cond));

  check(__LINE__,pthread_mutex_unlock(&((x_p)xx)->x_mutex));

  /* We've set the pointer.  Wait for permission to finish. */

  check(__LINE__,pthread_mutex_lock(&((x_p)xx)->x_mutex));

  while(((x_p)xx)->x_stage<2)
  {
    check(__LINE__,pthread_cond_wait(&((x_p)xx)->x_cond,
                                     &((x_p)xx)->x_mutex
                                    )
         );
  }

  check(__LINE__,pthread_mutex_unlock(&((x_p)xx)->x_mutex));

  return xx;

} /* doit() */

/*--------------------------------------------------------------------------*/

int
main(int    argc,
     char **argv
    )
{
#if defined(_POSIX_THREAD_ATTR_STACKSIZE)

  long           desired_size;
  size_t         stack_size;
  pthread_attr_t attributes;
  x_t            fred;
  x_t            barney;

  if(argc>1)
  {
    desired_size=strtol(argv[1],NULL,0);
  }

  printf("=== pthread_attr_setstacksize() is implemented on this system.\n");
  printf("minimum stack size is %8d %08X\n",PTHREAD_STACK_MIN,PTHREAD_STACK_MIN);
  check(__LINE__,pthread_attr_init(&attributes));
  check(__LINE__,pthread_attr_getstacksize(&attributes,&stack_size));
  printf("default stack size is %8d %08X\n",stack_size,stack_size);

  if(argc>1)
  {
    printf("going for stack size  %8ld %08lX\n",desired_size,desired_size);
    check(__LINE__,pthread_attr_setstacksize(&attributes,desired_size));
    check(__LINE__,pthread_attr_getstacksize(&attributes,&stack_size));
    printf("new     stack size is %8d %08X\n",stack_size,stack_size);
  }
  else
  {
    printf("using default stack size\n");
  }

  fred  .x_thread_number=1;
  barney.x_thread_number=2;

  check(__LINE__,pthread_mutex_init(&fred  .x_mutex,NULL));
  check(__LINE__,pthread_mutex_init(&barney.x_mutex,NULL));
  check(__LINE__,pthread_cond_init (&fred  .x_cond, NULL));
  check(__LINE__,pthread_cond_init (&barney.x_cond, NULL));

  fred  .x_stage=0;
  barney.x_stage=0;

  check(__LINE__,pthread_create(&fred  .x_thread_id,&attributes,doit,&fred  ));
  check(__LINE__,pthread_create(&barney.x_thread_id,&attributes,doit,&barney));

  /* Wait for both threads to have written the pointer. */

  check(__LINE__,pthread_mutex_lock(&fred.x_mutex));

  while(fred.x_stage<1)
  {
    check(__LINE__,pthread_cond_wait(&fred.x_cond,&fred.x_mutex));
  }

  check(__LINE__,pthread_mutex_unlock(&fred.x_mutex));

  check(__LINE__,pthread_mutex_lock(&barney.x_mutex));

  while(barney.x_stage<1)
  {
    check(__LINE__,pthread_cond_wait(&barney.x_cond,&barney.x_mutex));
  }

  check(__LINE__,pthread_mutex_unlock(&barney.x_mutex));

  /* Let the threads finish. */

  check(__LINE__,pthread_mutex_lock(&fred.x_mutex));

  fred.x_stage=2;

  check(__LINE__,pthread_cond_signal(&fred.x_cond));

  check(__LINE__,pthread_mutex_unlock(&fred.x_mutex));

  check(__LINE__,pthread_join(fred.x_thread_id,NULL));

  check(__LINE__,pthread_mutex_lock(&barney.x_mutex));

  barney.x_stage=2;

  check(__LINE__,pthread_cond_signal(&barney.x_cond));

  check(__LINE__,pthread_mutex_unlock(&barney.x_mutex));

  check(__LINE__,pthread_join(barney.x_thread_id,NULL));

  printf("difference         is %8d\n",
         fred.x_variable_pointer-barney.x_variable_pointer
        );

#else

  printf("=== pthread_attr_setstacksize() is not implemented on this system.\n");

#endif

  return 0;

} /* main() */
EOD
 
1 members found this post helpful.
Old 01-25-2011, 11:06 AM   #5
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Original Poster
Rep: Reputation: 15
Your code works great.... I'm looking back at my code to figure out what the differences are. Thanks for your help.

I figured out what my problem is.

Code:
if ( 0 != threadRet)
//should be:
if ( -1 != threadRet)
Even though the properties come back wrong my code still allows it to use the m_attributes to attempt to create the thread. It must still have worked using the default because the thread is still created.

Thanks for your help again.
Unfortunately this old problem isn't my current one. I was hoping that this was my current issue. I haven't posted that issue at all yet and it is also a segmentation fault that I can't see because of my gdb issues.
 
Old 01-25-2011, 02:05 PM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by gothrog View Post
I figured out what my problem is.

Code:
if ( 0 != threadRet)
//should be:
if ( -1 != threadRet)
You're aware, are you not, that no pthreads function returns -1 as its value under any circumstances?
 
1 members found this post helpful.
Old 01-25-2011, 05:27 PM   #7
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Original Poster
Rep: Reputation: 15
Wow I'm really glad that you replied to that because I used a bad source as a reference or I didn't understand the differences of what ibm is talking about at a first glance.

I did a search for "C++ pthread_attr_setstacksize" on google and got this as the first site hit.
It says the return value of it is -1 in a fail case.

http://publib.boulder.ibm.com/infoce...00/ptasets.htm

Thanks
 
Old 01-25-2011, 06:44 PM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by gothrog View Post
It says the return value of it is -1 in a fail case.

http://publib.boulder.ibm.com/infoce...00/ptasets.htm
Well, heck fire. It shore do! What you've stumbled across there is a man page describing behavior of IBM's product z/OS, which isn't UNIX or Linux. Read about it here.

In fairness to IBM, they do seem to have some sort of UNIX compatibility mode, because on that same man page you reference, it also says this:
Quote:
Special Behavior for Single UNIX Specification, Version 3:

If unsuccessful, pthread_attr_setstacksize() returns an error number to indicate the error.
You're not running z/OS, are you? If you are, my apologies.

And thanks for sharing the link. I didn't even know about z/OS until now, although I hail from the days of OS/360 (older than OS/390, not quite as old as archtoad6), when your main choices for programming (at least in our shop at the University of California at Irvine) were assembly language or FORTRAN.
 
1 members found this post helpful.
Old 01-26-2011, 09:18 AM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
(OT) Cute.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why the thread stack size can not be changed when calling in a dynamic library? xhx321 Programming 10 10-09-2012 01:38 AM
Command to list stack size of each thread in an application mannw72 Linux - Newbie 1 03-14-2007 06:31 PM
Cedega 5.1/Guild Wars - Thread stack size is too small. Reth Linux - General 0 05-05-2006 09:35 PM
Increasing Kernel stack size to 8k without recompiling the Kernel deathman Linux - Software 2 04-08-2006 04:39 AM
Thread stack size failure jdhedden Programming 0 03-27-2006 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration