Ady Wicaksono Daily Activities

C code to test thread limitation on Linux

with 5 comments

Still remember with posting about thread limitation on Linux?

Here’s a C code to test it..

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_THREADS 10000
int i;

void run(void) {
   char c;
   if (i < 10)
      printf("Address of c = %u KB\n", (unsigned int) &c / 1024);
   sleep(60 * 60);

int main(int argc, char *argv[]) {
   int rc = 0;
   pthread_t thread[MAX_THREADS];
   printf("Creating threads ...\n");
   for (i = 0; i < MAX_THREADS && rc == 0; i++) {
     rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
     if (rc == 0) {
       if ((i + 1) % 100 == 0)
         printf("%i threads so far ...\n", i + 1);
        printf("Failed with return code %i creating thread %i.\n",rc, i + 1);

Save it as threadlimit.c and compile it with

gcc -lpthread -o resulttest threadlimit.c

You have an exe file => “resulttest” so execute it now


Here is an example as result of this C code execution on my Fedora 🙂
Linux 2.6.17-1.2142_FC4smp #1 SMP Tue Jul 11 22:57:02 EDT 2006 i686 i686 i386 GNU/Linux

# ulimit -s

# ./resulttest
Creating threads ...
100 threads so far ...
200 threads so far ...
300 threads so far ...
Failed with return code 12 creating thread 304.

Boom!!!, I only create 303 threads. No try decrease thread stack size to 100

# ulimit -s 100

# ./resulttest
Creating threads ...
100 threads so far ...
200 threads so far ...
300 threads so far ...
400 threads so far ...
500 threads so far ...
600 threads so far ...
700 threads so far ...
800 threads so far ...
900 threads so far ...
1000 threads so far ...
1100 threads so far ...
1200 threads so far ...
1300 threads so far ...
1400 threads so far ...
1500 threads so far ...
10000 threads so far...
... [CTRL-C]

Wow, I could create a lot of threads :D….

Written by adywicaksono

July 15, 2007 at 3:35 pm

Posted in Linux, Programming

5 Responses

Subscribe to comments with RSS.

  1. test message yo!


    February 24, 2008 at 12:53 am

  2. Good Day

    Just wanted to share my new experience.

    If your system denies to run due to an error corresponding to lost HAL.DLL, invalid Boot.ini or any other important system boot files you can repair this by using the XP installation CD. Just boot from your XP Setup CD and enter the Recovery Console. Then run “attrib -H -R -S” on the C:\Boot.ini file and remove it. Launch “Bootcfg /Rebuild” and then Fixboot



    August 31, 2008 at 7:09 am

  3. Hello, I just became part of this forum and I love the contribution of this community.

    I would like to make a contribution of my own by posting a site I created informing users about computer repair. Online Computer Services

    Also, this is worth taking a look at – Online Computer Support

    There is lots of information regarding computer repair techniques and what one should do when prompted with certain instances.


    June 9, 2009 at 2:25 pm

  4. I’m new here on the forum, found it by searching google. I look forward to chatting about various topics with all of you.


    June 15, 2009 at 4:14 am

  5. hi there! I am new to the board and just wanted to introduce myself 🙂


    February 22, 2010 at 7:37 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: