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

  • Home
  • SEARCH
  • 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 3986158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:58:40+00:00 2026-05-20T05:58:40+00:00

Any ideas why it works fine for values like 0, 1, 2, 3, 4…

  • 0

Any ideas why it works fine for values like 0, 1, 2, 3, 4… and seg faults for values like >15?
#include
#include
#include

void *fib(void *fibToFind);

main(){
pthread_t mainthread;

long fibToFind = 15;
long finalFib;

pthread_create(&mainthread,NULL,fib,(void*) fibToFind);

pthread_join(mainthread,(void*)&finalFib);

printf("The number is: %d\n",finalFib);
}


void *fib(void *fibToFind){
long retval;

long newFibToFind = ((long)fibToFind);

long returnMinusOne;
long returnMinustwo;

pthread_t minusone;
pthread_t minustwo;

if(newFibToFind == 0 || newFibToFind == 1)
return newFibToFind;

else{
long newFibToFind1 = ((long)fibToFind) - 1;
long newFibToFind2 = ((long)fibToFind) - 2;

pthread_create(&minusone,NULL,fib,(void*) newFibToFind1);
pthread_create(&minustwo,NULL,fib,(void*) newFibToFind2);

pthread_join(minusone,(void*)&returnMinusOne);
pthread_join(minustwo,(void*)&returnMinustwo);

return returnMinusOne + returnMinustwo;

}

}
  • 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-20T05:58:41+00:00Added an answer on May 20, 2026 at 5:58 am

    Runs out of memory (out of space for stacks), or valid thread handles?

    You’re asking for an awful lot of threads, which require lots of stack/context.
    Windows (and Linux) have a stupid “big [contiguous] stack” idea.

    From the documentation on pthreads_create:
    “On Linux/x86-32, the default stack size for a new thread is 2 megabytes.”
    If you manufacture 10,000 threads, you need 20 Gb of RAM.
    I built a version of OP’s program, and it bombed with some 3500 (p)threads
    on Windows XP64.

    See this SO thread for more details on why big stacks are a really bad idea:
    Why are stack overflows still a problem?

    If you give up on big stacks, and implement a parallel language with heap allocation
    for activation records
    (our PARLANSE is
    one of these) the problem goes away.

    Here’s the first (sequential) program we wrote in PARLANSE:

    (define fibonacci_argument 45)
    
    (define fibonacci
       (lambda(function natural natural )function 
       `Given n, computes nth fibonacci number'
          (ifthenelse (<= ? 1)
               ?
             (+ (fibonacci (-- ?))
                  (fibonacci (- ? 2))
               )+
       )ifthenelse  
       )lambda
     )define
    

    Here’s an execution run on an i7:

     C:\DMS\Domains\PARLANSE\Tools\PerformanceTest>run fibonaccisequential
     Starting Sequential Fibonacci(45)...Runtime: 33.752067 seconds
     Result: 1134903170
    

    Here’s the second, which is parallel:

    (define coarse_grain_threshold 30) ; technology constant: tune to amortize fork overhead across lots of work
    
    (define parallel_fibonacci
       (lambda (function natural natural )function 
       `Given n, computes nth fibonacci number'
          (ifthenelse (<= ? coarse_grain_threshold)
               (fibonacci ?)
               (let (;; [n natural ] [m natural ]  )
                       (value (|| (= m (parallel_fibonacci (-- ?)) )=
                                  (= n (parallel_fibonacci (- ? 2)) )=
                              )||
                              (+ m n)
                       )value
               )let
           )ifthenelse  
       )lambda
    )define
    

    Making the parallelism explicit makes the programs a lot easier to write, too.

    The parallel version we test by calling (parallel_fibonacci 45). Here
    is the execution run on the same i7 (which arguably has 8 processors,
    but it is really 4 processors hyperthreaded so it really isn’t quite 8
    equivalent CPUs):

    C:\DMS\Domains\PARLANSE\Tools\PerformanceTest>run fibonacciparallelcoarse
    Parallel Coarse-grain Fibonacci(45) with cutoff 30...Runtime: 5.511126 seconds
    Result: 1134903170
    

    A speedup near 6+, not bad for not-quite-8 processors. One of the other
    answers to this question ran the pthreads version; it took “a few seconds”
    (to blow up) computing Fib(18), and this is 5.5 seconds for Fib(45).
    This tells you pthreads
    is a fundamentally bad way to do lots of fine grain parallelism, because
    it has really, really high forking overhead. (PARLANSE is designed to
    minimize that forking overhead).

    Here’s what happens if you set the technology constant to zero (forks on every call
    to fib):

    C:\DMS\Domains\PARLANSE\Tools\PerformanceTest>run fibonacciparallel
    Starting Parallel Fibonacci(45)...Runtime: 15.578779 seconds
    Result: 1134903170
    

    You can see that amortizing fork overhead is a good idea, even if you have fast forks.

    Fib(45) produces a lot of grains. Heap allocation
    of activation records solves the OP’s first-order problem (thousands of pthreads each
    with 1Mb of stack burns gigabytes of RAM).

    But there’s a second order problem: 2^45 PARLANSE “grains” will burn all your memory too
    just keeping track of the grains even if your grain control block is tiny.
    So it helps to have a scheduler that throttles forks once you have “a lot”
    (for some definition of “a lot” significantly less that 2^45) grains to prevent the
    explosion of parallelism from swamping the machine with “grain” tracking data structures.
    It has to unthrottle forks when the number of grains falls below a threshold
    too, to make sure there is always lots of logical, parallel work for the physical
    CPUs to do.

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

Sidebar

Related Questions

I can't figure out why the following wont work, any ideas?? public interface IFieldSimpleItem
Any ideas on how to implement tab completion for a .NET (C#) Console Application?
Any ideas what the average user's download speed is? I'm working on a site
Any ideas how to determine the number of active threads currently running in an
Any ideas how to display a PDF file in a WPF Windows Application? I
Any ideas on how to disable, but not uninstall Resharper 4.x or above?
Any ideas why this won't validate here: http://validator.w3.org/#validate_by_input It seems the form input tags
Any ideas why the built in asp.net webserver insists on serving /default.aspx whenever you
Any ideas on how to get the functionality of an attributed string on an
Do you have any ideas how to call DoEvents from a C# DLL

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.