1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Fibonacci Using Pthread

Discussion in 'C' started by samyx, Oct 15, 2010.

  1. samyx

    samyx New Member

    Joined:
    Oct 15, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hey everyone,

    I am just starting to learn about threads, and I am trying to write a thread to compute fibonacci and return an indicated fibonacci number. I have to use pthread_join to wait and collect the returned value, and then print it.

    Hereis what I have so far, but I am stuck, it keeps giving me the error
    " fibon.c: (.text+0x65): undefined reference to `fibonacci'
    collect2: ld returned 1 exit status"

    Please help me!
    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    /* compute sucessive fibonnaci numbers.*/
    
    void* fibonacci(void* arg);
    main()
    {
    
            pthread_t thread;
            int num1 = 0;
            int num2 = 1;;
            int ans;
            int which_fibo;
    
            int counter, numPrint;
            numPrint = 10;
    
            for(counter= 0; counter < numPrint; ++counter){
     ans = (num1 + num2);
                    num1 = num2;
                    num2 = ans;
            }
    
    
            /* start the computing thread up to the number you want to return*/
            pthread_create(&thread, NULL, &fibonacci, (void*)&which_fibo);
    
            /* wait for the fibonacci thread to complete */
             pthread_join(thread, (void*)&ans);
    
            /* print the number in the position requested */
            printf("the  number is %d.\n",which_fibo, ans);
    
     }
    
     
    Last edited by a moderator: Oct 15, 2010
  2. samyx

    samyx New Member

    Joined:
    Oct 15, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Some progress...Now my program is printing 2 positions ahead. For exemplo:

    0 1 1 2 3 5 8 13

    If I select to print position 3, the result would be 3.
    If I select to print position 4, the result would be 5.ow

    How can I fix this problem, and how can I return the value if the position selected is 1 or 2?

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

    /* compute sucessive fibonnaci numbers.*/

    void* fibonacci(void* arg){
    int num1 = 1;
    int num2 = 1;;
    int ans;
    int maxfibo=10;
    int n = *((int*) arg);
    int is_num = 1;

    int counter;

    for(counter= 0; counter < (maxfibo-2); ++counter){

    ans = (num1 + num2);
    num1 = num2;
    num2 = ans;
    if (is_num) {
    if (--n==0)
    return (void*) ans;
    }
    }
    }
    int main ()
    {
    pthread_t thread;
    int which_fibo = 5;
    int number;


    /* start the computing thread up to the number you want to return*/
    pthread_create(&thread, NULL, &fibonacci, (void*)&which_fibo);

    /* wait for the fibonacci thread to complete */
    pthread_join(thread, (void*)&number);



    /* print the number in the position requested */
    printf("The number is %d.\n", number);

    return 0;

    }
     

Share This Page