生产者消费者C中的分割错误

时间:2017-08-28 06:33:49

标签: c linux segmentation-fault pthreads semaphore

我尝试使用C和线程中的bounder缓冲区模拟生产者消费者问题。 还使用互斥锁和信号量。 预期输出是每次生成或消耗项目时显示缓冲区的状态。 缓冲区大小固定为10.最初缓冲区项都是-1。当生产者在其中生成一个项目时,该项目将替换为-1。 第0个索引中的项目为0,第一个索引为1,依此类推.....无关紧要。 该程序询问我们想要创建的生产者和消费者的数量。 生产工作正常......但不是消费。 线程1中出现了分段错误。我不确定线程​​1是什么。 我尝试多次使用GDB进行调试....没有希望。     //生产者消费者。

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

#define TRUE 1

int buff_size=10,i;
int buffer[25];

pthread_mutex_t mutex;
sem_t full, empty;

int counter = 0;
int consume_count=0; //No of Consumers created
int produce_count=0; //No of Producers created

void initializeData()
{
    sem_init(&full, 0, 0);
    sem_init(&empty, 0, buff_size);
    pthread_mutex_init(&mutex, NULL);

}

int insert_item(int counter)
{
    if (counter < buff_size) {
        buffer[counter] = counter;
        counter++;
        //produce_count++;
        return 0;
    }

    else {
        printf("\n[BUFFER FULL!]");
        return -1;
    }
}

int remove_item()
{
    printf("\n[GOING TO REMOVE AN ITEM]\n");

    if (buffer[counter-1] != -1) {  
        buffer[counter-1] = -1;
        counter--;
        //consume_count++; // Commented out...

        return 0;
    }

    else {
        printf("\n[EMPTY]\n");
        return -1;
    }
}

void *producer(void *arg)
{
    int RET = 0;

    while( TRUE ) { 
        sem_wait(&empty);
        pthread_mutex_lock(&mutex);

    RET = insert_item(counter);

    if (RET){   
        printf("\nProducer Sleeping...zzZZ\n");
        sleep(2);
    }

    pthread_mutex_unlock(&mutex);
    sem_post(&full);

    if(!RET)
        printf("\n[ INSERTED ]\n" );

    printf("\n");

    for(i=0; i < buff_size ;i++)
        printf("[%d] ",buffer[i]);

    printf("\n");
    sleep(3);
    } // end of while...


}

void *consumer(void *arg)
{
    int RET = 0;

    while( TRUE ) { 
    sem_wait(&full);
    pthread_mutex_lock(&mutex);

    RET = remove_item(buffer);

    if (RET){   
        printf("\nConsumer Sleeping\n");
        sleep(3);
    }

    pthread_mutex_unlock(&mutex);
    sem_post(&empty);

    if(!RET) {
        printf("\nConsumed\n");
        printf("\n");
    }

    for(i=0 ; i < buff_size ; i++)
        printf("%4d",buffer[i]);

    printf("\n");
    sleep(2);
    } //end of while...

}

void main()
{
    int         produce, consume;
    pthread_t   *prod;//thread ID
    pthread_t   *cons;//thread ID

    printf("\nEnter the no of producers: ");
    scanf("%d",&produce);
    printf("\nEnter the no of consumers: ");
    scanf("%d",&consume);
    putchar('\n');

    for (i=0; i < buff_size; i++)
        buffer[i] = -1;

    for (i=0; i < buff_size; i++)
        printf("[%d] ", buffer[i]);

    printf("\n");

    initializeData();

    for (i = 0; i < produce; i++)
        {
            pthread_create(&prod[i], NULL, producer, NULL);
            produce_count++;
        }

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

            pthread_create(&cons[i], NULL, consumer, NULL);
            consume_count++;
            printf("AAAAA");
        }
    /*for (i = 0; i < produce; i++) 
        pthread_join(producer, NULL);

        for (i = 0; i < consume; i++)
            pthread_join(consumer, NULL);*/

printf("\n===============\n[ PRODUCED: %d ]", produce_count);
printf("\n[ CONSUMED: %d ]\n==============", consume_count);     
}

1 个答案:

答案 0 :(得分:1)

pthread_create(&prod[i], NULL, producer, NULL);

在此调用中pthread_create将创建新线程并尝试返回prod [i]中的线程ID 但是:

pthread_t   *prod;//thread ID
pthread_t   *cons;//thread ID

这些是uninitiaised指针,如果你想在其中收集threadids,你需要使用malloc为它们分配内存:

prod = malloc(sizeof(pthread_t) * produce);
cons = malloc(sizeof(pthread_t) * consume);

否则pthread_create会将threadid存储在无效内存中导致段错误