生产者/消费者与pthreads

时间:2013-04-22 03:35:39

标签: c pthreads producer-consumer

程序将接受关于sim运行多长时间,睡眠以及要创建多少生产者/消费者的输入。当我运行程序时,它要求输入但停在“启动线程”并且没有任何反应。我认为问题在于缓冲功能,但我不确定。任何建议都将非常感激。注意** //代码行注释掉了,只关注它在没有信号量的情况下运行

#include "buffer.h"
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

void *producer(void *param);
void *consumer(void *param);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t Buffer_Not_Full = PTHREAD_COND_INITIALIZER;
pthread_cond_t Buffer_Not_Empty = PTHREAD_COND_INITIALIZER;

sem_t sem,empty,full;
sem_t sem_mutex;
pthread_t self_id;
int MAXSLEEP;
bool var = true;
int main( int argc, char *argv[] )
{
    int simTime,pThread,cThread;
    char option[4];
    int i=0;
    /* create the mutex lock */
    pthread_mutex_init( &mutex, NULL );
    printf("Enter Simulation time \t");
    scanf("%d", &simTime);
    printf("Enter Thread sleep time \t");
    scanf("%d", &MAXSLEEP);
    printf("Enter Number of Producers \t");
    scanf("%d", &pThread);
    printf("Enter Number of Consumers \t");
    scanf("%d", &cThread);
    printf("Enter yes or no to display results \t");
    scanf("%s", &option);

    /* create the semaphore and initialize it to 5 */
    //sem_init( &sem, 0, 5 );
    /* create the semaphore */
    //sem_init( &sem_mutex, 0, 1 );
    //sem_init( &full, 0, 0 );
    //sem_init( &empty, 0, 1 );
    pthread_t pro_thread;
    pthread_t con_thread;



    //Get command line arguments argv[0],argv[1], argv[2], argv[3], argv[4]
    //Initialize buffer
    printf("Starting Threads...\n");
    for(i=0;i<pThread;i++)
    {
        pthread_create(&pro_thread,NULL,producer,NULL);
    }
    for(i=0;i<cThread;i++)
    {
        pthread_create(&con_thread,NULL,consumer,NULL);
    }

    sleep(simTime);

    for(i=0;i<pThread;i++)
    {
        pthread_join(pro_thread,NULL);
    }
    for(i=0;i<cThread;i++)
    {
        pthread_join(con_thread,NULL);
    }

    //if(option == "yes")
    //{
    //DisplayStatistics();
    //}

    exit(0);
}


void *producer(void *param)
{

    buffer_item item;
    unsigned int seed = 10;
    while(var)
    {
        sleep(MAXSLEEP);// sleep for one second
        item = rand_r(&seed) % 100;// generate random number

        /* acquire the semaphore */
        //sem_wait( &sem_mutex );
        //sem_wait( &empty );

        if (buffer_insert_item(item))
            printf("error");
        else
            self_id = pthread_self();
        printf("Thread %u\n", self_id);
        printf("producer produced %d\n",item);
        /* release the semaphore */
        //sem_post( &sem_mutex );
        //sem_post( &full );
    }
    pthread_exit(NULL);
}

void *consumer(void *param)
{

    buffer_item item;
    while(var)
    {
        sleep(MAXSLEEP);// sleep for one second
        /* acquire the semaphore */
        //sem_wait( &full );
        //sem_wait( &sem_mutex );
        if (buffer_remove_item(&item))
            printf("error");
        else
            self_id = pthread_self();
        printf("Thread %u\n", self_id);
        printf("consumer consumed %d\n",item);
        /* release the semaphore */
        //sem_post( &sem_mutex );
        //sem_post( &empty );
    }
    pthread_exit(NULL);
}

int buffer_insert_item( buffer_item item )
{

    int nextProduced = item;

    while(var)
    {
        /* aquire the mutex lock */
        pthread_mutex_lock( &mutex );
        while(counter == BUFFER_SIZE)
            ;// Do nothing
        buffer[in] = nextProduced;
        in = (in+1) % BUFFER_SIZE;
        counter++;
        /* release the mutex lock */
        pthread_mutex_unlock( &mutex );
        pthread_cond_signal(&Buffer_Not_Empty);
    }
}

int buffer_remove_item( buffer_item *item  )
{

    int nextConsumed = *item;
    while(var)
    {
        /* aquire the mutex lock */
        pthread_mutex_lock( &mutex );
        while(counter == 0)
            ;// Do nothing
        nextConsumed = buffer[out];
        out = (out+1) % BUFFER_SIZE;
        counter--;
        /* release the mutex lock */ 
        pthread_mutex_unlock( &mutex );
        pthread_cond_signal(&Buffer_Not_Full);
    }
}

0 个答案:

没有答案