Threads and stack size


 
Thread Tools Search this Thread
Top Forums Programming Threads and stack size
# 1  
Old 08-12-2010
Threads and stack size

Can someone explain me, why when I create new threads with stack bigger than default (using pthread_attr_setstacksize()) i can create more? Each thread has it's own stack, so how bigger stack can cause possibility to create more threads?
# 2  
Old 08-12-2010
Could you post some example code and give info on the O/S ?
Thx
# 3  
Old 08-12-2010
Code:
#include <stdio.h>
#include <pthread.h>

#define MODIFY_STACK 

void *fnc(void *a)
{
        sleep(99999);
        return NULL;
}

int main()
{
        int i = 0;
        pthread_t abc;
#ifdef MODIFY_STACK
        pthread_attr_t    attr;
#endif
        
#ifdef MODIFY_STACK
        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 20000);
#endif
        
#ifdef MODIFY_STACK
        while (pthread_create(&abc, &attr, fnc, NULL) == 0) {
#else
        while (pthread_create(&abc, NULL, fnc, NULL) == 0) {
#endif
                i++;
        }
        
        printf("%d\n", i);
        fflush(stdout);
        return 0;
}

I use linux 32bit. When I compile this code without MODIFY_STACK definition I can create 380 threads. When I compile with it, I am able to open about 30000 threads. I want just know, how it is possible. On linux 64bit, MODIFY_STACK doesn't make any difference (stack is bigger by default)

Last edited by dawwin; 08-12-2010 at 04:57 PM..
# 4  
Old 08-12-2010
You're giving it an invalid stack size of 20000 and not checking if it returns success or error so there's no telling what it's actually setting the stack size to.

From man pthread_attr_setstacksize:

Code:
ERRORS
       pthread_attr_setstacksize() can fail with the following error:

       EINVAL The stack size is less than PTHREAD_STACK_MIN (16384) bytes.

       On  some  systems,  pthread_attr_setstacksize() can fail with the error
       EINVAL if stacksize is not a multiple of the system page size.

Try 20480, that's an even multiple of an x86 system's page size of 4096.

You should also check what the default thread size actually is on your system; whatever it is, I'm pretty sure 20000 is smaller. It used to be 64K, but may have been expanded a few years ago -- some buggy and piggy but unfortunately popular window managers were overrunning default 64K stacks with no obvious way to fix it, and there was talk of upping it to a meg to accommodate their poor code.

You're also never forcing your threads to join or exit, which can cause undefined behavior.

Also take a look at pthread_attr_setguardsize.

---------- Post updated at 03:03 PM ---------- Previous update was at 02:40 PM ----------

64-bit linux has a much larger limit regardless of the actual size of the thread stacks because it has 2^32 times more address space to waste. You can waste gigabytes upon gigabytes of virtual memory in a 64-bit system as long as you don't actually try to use it all. You could say the main advantage of a 64-bit system is a much bigger imagination Smilie

Last edited by Corona688; 08-12-2010 at 05:55 PM..
# 5  
Old 08-12-2010
This code was just an example. I just wanted to know, why bigger stack can allow to create more threads than default
# 6  
Old 08-12-2010
On my system the default stack size is 8388608.
So setting a stack of 20000 would allow many more threads to be created.
# 7  
Old 08-12-2010
Quote:
Originally Posted by dawwin
This code was just an example. I just wanted to know, why bigger stack can allow to create more threads than default
If you're comparing 64-bit linux to 32-bit linux, you might have missed the explanation I tacked on at the last minute. Otherwise, I think you're just mistaken about your system's default thread size, or having problems with your code that my crystal ball missed.

If you're checking compiled-in constants for the default thread size, keep in mind they're often actually minimums, you should get the default by querying the library at runtime. Try pthread_attr_init then pthread_attr_getstacksize

Last edited by Corona688; 08-12-2010 at 06:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Red Hat

Cannot set 'soft limits' for 'maximum stack size' for a standard user

Hi Guys, I'm trying to install Oracle Database on to Oracle Linux 7.6 but when the database install package checks the OS set-up, it keeps on failing on the soft limits for the stack. It's default value is 8192 but I'm trying to set it to 10240. This is what I added to... (2 Replies)
Discussion started by: ASGR
2 Replies

2. Shell Programming and Scripting

How to find stack size at runtime?

hi, I just wondering is there any way that I can record the stack size of shell script at runtime. (2 Replies)
Discussion started by: yc962frank
2 Replies

3. Linux

Linux Thread Stack Size pthread_attr_setstacksize

I have MultiThreaded Application in Linux. I am Setting the stack size using pthread_attr_setstacksize. The stack size i set is greter than PTHREAD_STACK_MIN i.e. 16384 bytes for my system. i am setting the stack size to 17408 bytes, and my application crashes due to insufficient stack size.... (1 Reply)
Discussion started by: Kedar Sabnis
1 Replies

4. UNIX for Advanced & Expert Users

How to identify maximum stack size?

Hi All, I have set max stack size as 4KB for my thread, but it always using very less. So I like to know what is the maximum stack size is used by my thread. I tried with gcc -fstack-usage command line option, but its not supported by mips. Kindly suggest me the way to find the max stack... (6 Replies)
Discussion started by: rajamohan
6 Replies

5. Programming

How to read max stack size -Xss that is set/default for a java program?

I need to know what is the maximum stack size i.e. -Xss my java program is running with. Is there a way to find that out from inside my java program code and outside of it. What i am looking for is to read whatever the current set max limit -Xss (stack sie) is for a particular JVM(not... (3 Replies)
Discussion started by: mohtashims
3 Replies

6. Programming

How to find out the stack size occupied for a process?

Hi, In Linux how to find out what will be the stack size allocated for a process? Actually i have to fork n number of processess, and will call exec. I will be execing an executable which is already multithreaded and each thread size is defined. My doubt is how to know if the size of the... (2 Replies)
Discussion started by: rvan
2 Replies

7. UNIX for Advanced & Expert Users

Stack size of Apps ?

hi all, I need to find out the Stack usage of an App which is basically an mpeg4 decoder. We are plannin to port the codec to an RTOS. so is there any way to find out the max stack size while the codec is running? saTs (1 Reply)
Discussion started by: satish_somu
1 Replies

8. Programming

How to increase the size of the stack

Hi!!, could someone tell me how to increase the stack size in HP-UX? Thanx (7 Replies)
Discussion started by: jyotipg
7 Replies
Login or Register to Ask a Question