2016-10-24 8 views
-3

私は、Cプログラミング言語を使って、オペレーティングシステムの研究とプロセス/スレッドの作成を新たに開始しましたが(私たちが使用すると期待されていることですが)、私は書き込みをしようとして:ここではC、フィボナッチプログラムでのマルチスレッド化

は、Ubuntuシステム上で書かれた私のコードです:

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

    int total = 0; 

    typedef struct 
    { 
    int start; 
    int end; 
    int threadNo; 
    }THREAD_PARAMETERS; 

    void *work(void *parameters); 
    int threadCount; 

int main(int argc, char* argv[]) 
{ 

     printf("please give the number of terms you want to diplay.."); 
     scanf("%d", &threadCount); 

    pthread_t tid[threadCount]; 
    pthread_attr_t attr[threadCount]; 

    THREAD_PARAMETERS* lpParameter; 

     int n; 

    lpParameter = malloc(sizeof(THREAD_PARAMETERS)* threadCount); 

    int i=0; 

    for(i=0; i<threadCount; i++) 
    { 
    lpParameter[i].start = 0; 
    lpParameter[i].end = 1; 
    lpParameter[i].threadNo = i + 1; 

    pthread_attr_init(&attr[i]); 
    pthread_create(&tid[i],&attr[i],work,&lpParameter[i]); 
    } 

    for(i=0; i<threadCount; i++) 
    { 
    pthread_join(tid[i],NULL); 
    } 
    return 1; 
    } 



    void fibonacci(int a) 
    { 
    int prev_term = 0, current_term = 1, next_term = 0; 

    if(a==0){ 
    printf("%d\n",prev_term); 

    } 
    else if(a==1){ 

    next_term=current_term+prev_term; 
    printf("%d\n",current_term); 
    prev_term=current_term; 
    current_term=next_term; 

    void *work(void * parameters) 
    { 
    THREAD_PARAMETERS* param = (THREAD_PARAMETERS*)parameters; 
    fibonacci(threadCount); 
    pthread_exit(0); 
    } 

問題は、プログラムがTHREADCOUNT変数を数えるが、どのようなプログラムを印刷するだけでTHREADCOUNT回ゼロです。 そして、主な質問は、どのスレッドが、ユーザによって入力された用語の数(同時にスレッドの数であるか)に応じて、フィボナッチシリーズの「1つの用語」だけを書くことができるかどうかです。この種のプログラムを実装するための他のより論理的な方法はありますか?

+0

「threadCount times zeros」とはどういう意味ですか? –

+2

フィボナッチシリーズには閉じた形式のソリューションがあるので、スレッド、はるかに少ないスレッドが必要ないことに注意してください。 –

+0

threadCountが3の場合、たとえば3回0を出力します。 – adropintheocean

答えて

0

lpParameter[i]を各スレッドのworkの引数として使用していますが、fibonacciを呼び出すとその内容は無視されます。

関連する問題