Producer Consumer with threads

时间:2016-10-09 15:57:03

标签: c arrays multithreading pointers queue

I am trying to work with producer and consumer problem, I have taken up C programming after a long time. My ultimate aim is to have 3 producers and 1 consumer. At the moment I have 1 producer and 1 consumer. I have set a limit at the top for which the loops should run. The code works well till the limit of 46 however, it gives me a segmentation fault with any value above 46. This is the value of #define max 46. I am unsure as to why this is happening. Any help will be appreciated.

Here is my code. I have built on the code from Tanenbaum's book of modern OS.

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
//#define MAX 10000000000
#define max 50 //Works well till 46
#define NP 1
#define NC 1


pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
char queue[1][80], data[80];
int front, rear,reply;

void *producer(void *ptr)
{
    int i,index;
    index = (int)ptr;
    printf("Producing!\n");
    for(i=1; i<=max; i++)
    {
        printf("The value of i in prod %d with index %d\n: ", i, index);
        pthread_mutex_lock(&the_mutex);
        //wait till the queue is null. After that insert
        while(notNULL(&front,&rear) != -1)
            pthread_cond_wait(&condp, &the_mutex);
        reply =insq(queue, &rear, "test string");
        if (i == max)
            printf("DONE PRO!!!!\n");
        if( reply == -1 ){
            printf("\n Queue is Full, ERROR!!!\n");
            //break;
            //exit(0);
        }

        pthread_cond_signal(&condc);
        pthread_mutex_unlock(&the_mutex);
    }
    pthread_exit(0);
}

void *consumer(void *ptr)
{
    int i, index;
    index = (int)ptr;
    printf("Consuming\n");
    for(i=1; i<=max; i++)
    {
        printf("The value of i in con is %d with index %d\n: ", i, index);
        pthread_mutex_lock(&the_mutex);
        while(notNULL(&front, &rear) == -1)
            pthread_cond_wait(&condc, &the_mutex);
        reply = delq(queue, &front, &rear, data);
        if (i == max)
            printf("DONE CON!!!!\n");
        if( reply == -1 )
            printf("\n Queue is Empty \n");
        pthread_cond_signal(&condp);
        pthread_mutex_unlock(&the_mutex);
    }
    pthread_exit(0);
}

//int notNULL(queue[max][80],int *rear, int *front)
int notNULL(int *front, int *rear)
{
    if(*front == *rear)
        return(-1);
    else
        return 0;
}

int insq(char queue[1][80], int *rear, char data[80])
{
    printf("rear is in insq %s\n",queue[*rear]);
    printf("value of rear is %d\n",*rear);
    //printf("Value of max-1 is %d\n",(max-1) );
    if(*rear == max -1)
        return(-1);
    else
    {
        *rear = *rear + 1;
        printf("Data is %s\n",data );
        strcpy(queue[*rear], data);
        return(1);
    }
}

 int delq(char queue[1][80], int *front, int *rear, char data[80])
 {
    if(*front == *rear)
        return(-1);
    else
    {   
        (*front)++;
        strcpy(data, queue[*front]);
        return(1);
    }
} 


int main(int argc, char *argv[])
{
    front = rear = -1;
    int i;

    pthread_t pro,con;
    pthread_mutex_init(&the_mutex,0);
    pthread_cond_init(&condc, 0);
    pthread_cond_init(&condp, 0);
    for (i = 0; i < NC; i++)
    {
        pthread_create(&con, 0, consumer, (void*)i);
    }
    for (i = 0; i < NP; i++)
    {
        pthread_create(&pro, 0, producer, (void*)i);    
    }
    pthread_join(pro,0);
    pthread_join(con,0);
    pthread_cond_destroy(&condc);
    pthread_cond_destroy(&condp);
    pthread_mutex_destroy(&the_mutex);
}

0 个答案:

没有答案