代码之家  ›  专栏  ›  技术社区  ›  Akhil V

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

  •  0
  • Akhil V  · 技术社区  · 7 年前

    还使用互斥和信号量。 预期输出是在每次生产或消费项目时显示缓冲区的状态。 第0个索引中的项为0,第1个索引为1,依此类推。。。。。这无关紧要。 该程序询问我们想要创建的生产者和消费者的数量。 线程1中出现分段错误。我不确定线程1是什么。 //生产者消费者。

    #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 回复  |  直到 5 年前
        1
  •  1
  •   Pras    7 年前
    pthread_create(&prod[i], NULL, producer, NULL);
    

    在这次通话中 pthread_create 将创建新线程并尝试在prod[i]中返回线程id

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

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

    否则pthread\u create将在无效内存中存储threadid,从而导致段错误