Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:10:49+00:00 2026-05-23T16:10:49+00:00

Given the code below, I get a segmentation fault if I run it with

  • 0

Given the code below, I get a segmentation fault if I run it with n>16.

I think it has something to do with the stack, but I can’t figure it out. Could anyone give me a hand? The code is not mine, and really not important. I would just like someone to give me a hand with what is happening. This SO question is very similar, but there’s not enough information (the person who posts the answer briefly talks about the problem, but then goes on to talk about a different language). Besides, notice that with two gigs and no recursion, I can (if I’m doing it right) successfully create more than 16000 threads (though the OS only creates about 500 and runs about 300). Anyway, where am I getting the seg fault here and why? Thanks.

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

static void* fibonacci_thread( void* arg ) {
int n = (int)arg, fib;

pthread_t th1, th2;

void* pvalue; /*Holds the value*/

switch (n) {
case 0:  return (void*)0;
case 1:  /* Fallthru, Fib(1)=Fib(2)=1 */
case 2:  return (void*)1;
default: break;
}

pthread_create(&th1, NULL, fibonacci_thread, (void*)(n-1));

pthread_create( &th2, NULL, fibonacci_thread, (void*)(n-2));

pthread_join(th1, &pvalue);

fib = (int)pvalue;

pthread_join(th2, &pvalue);

fib += (int)pvalue;

return (void*)fib;
}

int main(int argc, char *argv[])
{
int n=15;
printf ("%d\n",(int)fibonacci_thread((void*)n));
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:10:50+00:00Added an answer on May 23, 2026 at 4:10 pm

    Heck with it, might as well make this an answer.

    First, check the return values of pthread_create and pthread_join. (Always, always, always check for errors. Just assert they are returning zero if you are feeling lazy, but never ignore them.)

    Second, I could have sworn Linux glibc allocates something like 2 megabytes of stack per thread by default (configurable via pthread_attr_setstacksize). Sure, that is only virtual memory, but on a 32-bit system that still limits you to ~2000 threads total.

    Finally, I believe the correct estimate for the number of threads this will spawn is basically fib(n) itself (how nicely recursive). Or roughly phi^n, where phi is (1+sqrt(5))/2. So the number of threads here is closer to 2000 than to 65000, which is consistent with my estimate for where a 32-bit system will run out of VM.

    [edit]

    To determine the default stack size for new threads on your system, run this program:

    int main(int argc, char *argv[])
    {
        size_t stacksize;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_getstacksize(&attr, &stacksize);
        phthread_attr_destroy(&attr);
        printf("Default stack size = %zd\n", stacksize);
        return 0;
    }
    

    [edit 2]

    To repeat: This is nowhere near 2^16 threads.

    Let f(n) be the number of threads spawned when computing fib(n).

    When n=16, one thread spawns two new threads: One to compute fib(15) and another to compute fib(14). So f(16) = f(15) + f(14) + 1.

    And in general f(n) = f(n-1) + f(n-2) + 1.

    As it turns out, the solution to this recurrence is that f(n) is just the sum of the first n Fibonacci numbers:

          1 + 1 + 2 + 3 + 5 + 8   // f(6)
    +         1 + 1 + 2 + 3 + 5   // + f(5)
    + 1                           // + 1
    
    = 1 + 1 + 2 + 3 + 5 + 8 + 13  // = f(7)
    

    This is (very) roughly phi^(n+1), not 2^n. Total for f(16) is still measured in the low thousands, not tens of thousands.

    [edit 3]

    Ah, I see, the crux of your question is this (hoisted from the comments):

    Thanks Nemo for a detailed answer. I did a little test and
    pthread_created ~10,000 threads with just a while(1) loop inside so
    they don’t terminate… and it did! True that the OS was smart enouth
    to only create about 1000 and run an even smaller number, but it
    didn’t run out of stack. Why do I not get a segfault when I generate
    lots more than THREAD_MAX, but I do when I do it recursively?

    Here is my guess.

    You only have a few cores. At any time, the kernel has to decide which threads are going to run. If you have (say) 2 cores and 500 threads, then any particular thread is only going to run 1/250 of the time. So your main loop spawning new threads is not going to run very often. I am not even sure whether the kernel’s scheduler is “fair” with respect to threads within a single process, so it is at least conceivable that with 1000 threads the main thread never gets to run at all.

    At the very least, each thread doing while (1); is going to run for 1/HZ on its core before giving up its time slice. This is probably 1ms, but it could be as high as 10ms depending on how your kernel was configured. So even if the scheduler is fair, your main thread will only get to run around once a second when you have thousands of threads.

    Since only the main thread is creating new threads, the rate of thread creation slows to a crawl and possibly even stops.

    Try this. Instead of while (1); for the child threads in your experiment, try while (1) pause();. (pause is from unistd.h.) This will keep the child threads blocked and should allow the main thread to keep grinding away creating new threads, leading to your crash.

    And again, please check what pthread_create returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the code below, how can you get unbind('click', h) to work? It doesn't
Given the code below: class Sample { public static void Run() { int i
Code given below is taken from the stackoverflow.com !!! Can anyone tell me how
I want to display the previous month name. My code is given below but
Check below given code. I could not get that how it had called testPrivate()
Where is the proper place to perform validation given the following scenario/code below: In
I tried the following code in LINQPad and got the results given below: List<string>
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
Given the code below, how do I compare a List of objects's values with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.