1

そこで、基本的な GNU 乱数ジェネレーターを使用する割り当て用の数値ジェネレーターを作成しています。理想的には、開発のこの段階で出力する必要があります

On loop 0, just produced...1804289383
On loop 0, I just consumed...1804289383.

などですが、今のところ、私の出力は次のとおりです。

On loop 0, just produced...1804289383
On loop 1, just produced...846930886
On loop 2, just produced...1681692777
On loop 3, just produced...1714636915
On loop 4, just produced...1957747793
On loop 5, just produced...424238335
On loop 6, just produced...719885386
On loop 7, just produced...1649760492
On loop 8, just produced...596516649
On loop 9, just produced...1189641421
On loop 0, I just consumed...1804289383
On loop 1, I just consumed...846930886
On loop 2, I just consumed...1681692777
On loop 3, I just consumed...1714636915
On loop 4, I just consumed...1957747793
On loop 5, I just consumed...424238335
On loop 6, I just consumed...719885386
On loop 7, I just consumed...1649760492
On loop 8, I just consumed...596516649
On loop 9, I just consumed...1189641421

たくさんの数字を作るまで、消費者にヒットしません。

ここで何が起こっているのですか?私はそれを、同様の機能を実行する別のコードと比較してきました。それは私が必要とすることを実行します.2つのコードは非常に似ています. 2つ目の目のセットが必要なだけです。何か簡単なものが欠けているのでしょうか?

私のコード:

#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define MAX_LOOP 10 //how many times the loop is allowed to go
#define MAX_VALUE 10 //how hig the random number can be
#define TRUE 1 //boolean value
#define FALSE 0 // ""
#define BUFFER_SIZE 10
#define DEBUG TRUE //debug variable. if on 1, it checks for bugs and stuff
pthread_cond_t cond; // conditional variable global
pthread_mutex_t the_mutex;

int bufferRandomFirst[BUFFER_SIZE]; //shared buffer array

int numOfItemsInQueue = 0;//number of items in the array



void *producerRandom(void *ptr)
{
/*
*
*   Random() Producer function:
*   Creates numbers for the consumer to process using gcc random()
*
*/
    int i = 0;
    int rand_num = 0;
    int rear = 0; //beginning of queue

    srandom((unsigned int)0) ; //make some random seeds

    for (i = 0; i< BUFFER_SIZE; i++){   

        rand_num =random(); //make a random number

        pthread_mutex_lock(&the_mutex); //get access to the buffer
        while (numOfItemsInQueue ==BUFFER_SIZE) {
            pthread_cond_wait(&cond, &the_mutex); //if the buffer is not empty, producer must stop
        }//end of while

        bufferRandomFirst[rear] = rand_num; //store the number in the buffer
        if (DEBUG){
            printf("On loop %d, just produced...%d\n", i, bufferRandomFirst[rear]);
        }
        rear = (rear + 1) % BUFFER_SIZE; //does the ciruclar queue loop
        numOfItemsInQueue +=1;

        pthread_cond_signal(&cond); //fires up the consumer process
        pthread_mutex_unlock(&the_mutex); // unlocks the mutex

    }//end of for loop                                                                      
    pthread_exit(0); //exit the thread
}//end producer


void *consumerRandom(void *ptr)
{
/*
*
*   Random() Consumer Function
*   consumes the numbers that the producer makes
*
*/
    int front = 0; //front of the queue
    int lastDigit = 0; //last digit of random number (thanks Nathan)
    int i = 0; //counting variable for for loops

    for (i = 0; i < BUFFER_SIZE; i++){

        pthread_mutex_lock(&the_mutex); //get exclusive access to buffer

        while(numOfItemsInQueue == 0){
            pthread_cond_wait(&cond, &the_mutex);
        }//end of while
        if (DEBUG){
            printf("On loop %d, I just consumed...%d\n", i, bufferRandomFirst[i]);
                }

        lastDigit = bufferRandomFirst[front] % 10; //steals the last digit from the random number
                    //for example, if the number was 100, 100 mod 10
                    //is 0, so you get 0.  if it was 101, you would get 1


        front = (front + 1) % BUFFER_SIZE; //wrap around the array
        numOfItemsInQueue -= 1;
        pthread_cond_signal(&cond);//wake up producer
        pthread_mutex_unlock(&the_mutex);

    }//end of for loop
    pthread_exit(0); //exit the loop
}//end of consumer




int main(int argc){
    pthread_t thread_one, thread_two; //create two threads for Random() Function


    printf("Random GNU numbers inbound!\n");
    //Random GCC pthread handing/
    pthread_mutex_init(&the_mutex,0); //set up the mutex    
    pthread_cond_init(&cond, 0); //set up the condition
    //makes a thread
    pthread_create(&thread_one, 0, producerRandom, 0);
    //makes a thread
    pthread_create(&thread_two, 0, consumerRandom, 0);  
    pthread_join(thread_one, 0); //wait on this thread  
    pthread_join(thread_two, 0); //wait on this one too. =)
    pthread_cond_destroy(&cond); //condition destroy
    pthread_mutex_destroy(&the_mutex); //destroy the mutex resources
    printf("And that is all for those numbers\n\n");

}//end of main
4

2 に答える 2

1

あなたのソースを最初に閲覧したところ、プロデューサーとコンシューマーの間に期待どおりの動作を引き起こすインターロックはありません。たとえば、プロデューサーでは、アイテムが消費されるまで待ってから別のアイテムを生成するように指示するものは何もありません。 the_mutexプロデューサー ループの最後で解放されますが、ループの先頭ですぐに再取得されます。プロデューサー スレッドを引き続き実行できると OS スケジューラが判断した場合、別のアイテムが生成されます。

于 2013-11-05T04:36:23.100 に答える
0

最新の変更を参照してください。シグナリング前に the_mutex のロックを解除しました。生成/消費シーケンスはあまり決定論的ではないようです。printf ステートメントをクリティカル セクションの外に移動しました。numOfItemsInQueue 値をチェックするときに、while ループの代わりに if ステートメントを使用しました。

// Compile with "gcc -pthread" command

#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define MAX_LOOP 300 //how many times the loop is allowed to go
#define MAX_VALUE 10 //how big the random number can be
#define TRUE 1 //boolean value
#define FALSE 0 // ""
#define BUFFER_SIZE 20
#define DEBUG TRUE //debug variable. if on 1, it checks for bugs and stuff
pthread_cond_t cond; // conditional variable global
pthread_mutex_t the_mutex;

int bufferRandomFirst[BUFFER_SIZE]; //shared buffer array

int numOfItemsInQueue = 0;//number of items in the array



void *producerRandom(void *ptr)
{
/*
*
*   Random() Producer function:
*   Creates numbers for the consumer to process using gcc random()
*
*/
    int i = 0;
    int rand_num = 0;
    int rear = 0; //beginning of queue

    srandom((unsigned int)0) ; //make some random seeds

    for (i = 0; i< MAX_LOOP; i++){   

        int rand_num =random(); //make a random number

        pthread_mutex_lock(&the_mutex); //get access to the buffer
        if (numOfItemsInQueue == BUFFER_SIZE) { // No need to loop: Upon pthread_cond_wait, we know for a fact that numOfItemsInQueue < BUFFER_SIZE (we have a single producer here)
            pthread_cond_wait(&cond, &the_mutex); //if the buffer is full, producer must stop
        }


        bufferRandomFirst[rear] = rand_num; //store the number in the buffer
        rear = (rear + 1) % BUFFER_SIZE; //does the circular queue loop
        numOfItemsInQueue +=1;

        pthread_mutex_unlock(&the_mutex); // unlocks the mutex
        pthread_cond_signal(&cond); //fires up the consumer process
        if (DEBUG){
            printf("On loop %d, just produced...%d\n", i, rand_num);
        }


    }//end of for loop                                                                      
    pthread_exit(0); //exit the thread
}//end producer


void *consumerRandom(void *ptr)
{
/*
*
*   Random() Consumer Function
*   consumes the numbers that the producer makes
*
*/
    int front = 0; //front of the queue
    int i = 0; //counting variable for for loops

    for (i = 0; i < MAX_LOOP; i++){

        pthread_mutex_lock(&the_mutex); //get exclusive access to buffer

        if(numOfItemsInQueue == 0){
            pthread_cond_wait(&cond, &the_mutex); // If buffer is empty, consumer must stop
        }

        int rand_num = bufferRandomFirst[front];


        front = (front + 1) % BUFFER_SIZE; //wrap around the array
        numOfItemsInQueue -= 1;
        pthread_mutex_unlock(&the_mutex);
        pthread_cond_signal(&cond);//wake up producer

        if (DEBUG){
            printf("On loop %d, I just consumed...%d\n", i, rand_num);
        }


    }//end of for loop
    pthread_exit(0); //exit the loop
}//end of consumer


int main(){
    pthread_t thread_one, thread_two; //create two threads for Random() Function


    printf("Random GNU numbers inbound!\n");
    //Random GCC pthread handing/
    pthread_mutex_init(&the_mutex,0); //set up the mutex    
    pthread_cond_init(&cond, 0); //set up the condition
    //makes a thread
    pthread_create(&thread_one, 0, producerRandom, 0);
    //makes a thread
    pthread_create(&thread_two, 0, consumerRandom, 0);  
    pthread_join(thread_one, 0); //wait on this thread  
    pthread_join(thread_two, 0); //wait on this one too. =)
    pthread_cond_destroy(&cond); //condition destroy
    pthread_mutex_destroy(&the_mutex); //destroy the mutex resources
    printf("And that is all for those numbers\n\n");

}//end of main 

バッファサイズ5、MAX_LOOP=30で出力。

Random GNU numbers inbound!
On loop 0, just produced...1804289383
On loop 1, just produced...846930886
On loop 2, just produced...1681692777
On loop 3, just produced...1714636915
On loop 4, just produced...1957747793
On loop 0, I just consumed...1804289383
On loop 1, I just consumed...846930886
On loop 5, just produced...424238335
On loop 6, just produced...719885386
On loop 2, I just consumed...1681692777
On loop 3, I just consumed...1714636915
On loop 4, I just consumed...1957747793
On loop 5, I just consumed...424238335
On loop 6, I just consumed...719885386
On loop 7, just produced...1649760492
On loop 8, just produced...596516649
On loop 9, just produced...1189641421
On loop 10, just produced...1025202362
On loop 11, just produced...1350490027
On loop 7, I just consumed...1649760492
On loop 8, I just consumed...596516649
On loop 9, I just consumed...1189641421
On loop 12, just produced...783368690
On loop 13, just produced...1102520059
On loop 14, just produced...2044897763
On loop 10, I just consumed...1025202362
On loop 11, I just consumed...1350490027
On loop 12, I just consumed...783368690
On loop 13, I just consumed...1102520059
On loop 15, just produced...1967513926
On loop 16, just produced...1365180540
On loop 17, just produced...1540383426
On loop 18, just produced...304089172
On loop 14, I just consumed...2044897763
On loop 15, I just consumed...1967513926
On loop 16, I just consumed...1365180540
On loop 17, I just consumed...1540383426
On loop 18, I just consumed...304089172
On loop 19, just produced...1303455736
On loop 20, just produced...35005211
On loop 19, I just consumed...1303455736
On loop 20, I just consumed...35005211
On loop 21, just produced...521595368
On loop 22, just produced...294702567
On loop 21, I just consumed...521595368
On loop 22, I just consumed...294702567
On loop 23, just produced...1726956429
On loop 24, just produced...336465782
On loop 23, I just consumed...1726956429
On loop 24, I just consumed...336465782
On loop 25, just produced...861021530
On loop 26, just produced...278722862
On loop 25, I just consumed...861021530
On loop 26, I just consumed...278722862
On loop 27, just produced...233665123
On loop 28, just produced...2145174067
On loop 27, I just consumed...233665123
On loop 28, I just consumed...2145174067
On loop 29, just produced...468703135
On loop 29, I just consumed...468703135
And that is all for those numbers
于 2013-11-05T04:51:44.953 に答える