我正在尝试使用pthread和向量形式的缓冲区来解决Producer-Consumer问题。我希望能够输入生产者和消费者将拥有的线程数量。输入两个值后,我就会遇到细分错误。我正在使用gcc和-lpthread编译代码,但没有收到编译错误。如何解决此错误?

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

#define MAX 1000//00            /* Numbers to produce */
#define SIZE 20                 /* Size of Buffer     */

typedef struct {
    int id;
} parm;

pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int buffer[SIZE];
int c = 0;

/*
    @Function: printState
    @In: integer i
    @Out: none

    @Description: Used to show the state of the buffer on a given state
*/
void printState(int i){
    int j;

    puts("Showing the state of the buffer: ");
    printf("[ ");
    for (j = 0; j < SIZE; j++){
        printf("%d ",buffer[j]);
    }
    printf("]\n");

}

/*
    @Function: producer
    @In: void *ptr
    @Out: none

    @Description: Call a producer on the process
*/

void* producer(void *ptr){
    int i;

    for (i = 1; i <= MAX; i++){
        printf("calling producer\n");// on position %d.\n",c+1);
        pthread_mutex_lock(&the_mutex); /* protect the buffer */

        if(c == SIZE){  /* If the buffer is full, wait */
            puts("The buffer is full. Waiting.");
            pthread_cond_wait(&condp, &the_mutex);
        }

        buffer[c] = 1;
        c++;

        printf("There are %d occupied positions on the buffer.\n", c);
        pthread_cond_signal(&condc); /* Wake up the consumer */
        pthread_mutex_unlock(&the_mutex); /* Release the buffer */

        //if(i == MAX/2){
        //  printState(i);
        //}

    }
    pthread_exit(0);
}

/*
    @Function: consumer
    @In: void *ptr
    @Out: none

    @Description: Call a consumer on the process
*/
void* consumer(void *ptr) {
    int i, j;

    for (i = 1; i <= MAX; i++){
        printf("calling consumer\n");// on position %d\n", c+1);
        pthread_mutex_lock(&the_mutex); /* protect the buffer */
        if (c == 0){ /* If there is nothing in the buffer, wait */
            puts("Buffer is empty. Waiting.");
            pthread_cond_wait(&condc, &the_mutex);
        }
        buffer[c] = 0;
        c--;
        printf("There are %d occupied positions on the buffer.\n", c);

        pthread_cond_signal(&condp); /* wake up consumer */
        pthread_mutex_unlock(&the_mutex); /* release the buffer */

        //if(i == MAX){
        //  printState(i);
        //}

    }
    pthread_exit(0);
}

/*
    @Function: main
    @In: integer argc and character **argv
    @Out: none

    @Description: Main function of the algorithm
*/
int main(int argc, char **argv){
    pthread_t *pro_threads, *con_threads;
    pthread_attr_t pro_pthread_custom_attr, con_pthread_custom_attr;
    int i, M, N;
    parm *p_pro, *p_con;

    puts("Please, enter the number of producer threads:");
    scanf("%d",&N);

    puts("Please, enter the number of consumer threads:");
    scanf("%d",&M);

    for(i=0;i<SIZE;i++){
        buffer[i] = 0;
    }

    // Allocate space for the threads

    pro_threads=(pthread_t *)malloc(N*sizeof(*pro_threads));
    pthread_attr_init(&pro_pthread_custom_attr);
    con_threads=(pthread_t *)malloc(M*sizeof(*con_threads));
    pthread_attr_init(&con_pthread_custom_attr);

    // Initialize the mutex and condition variables

    pthread_mutex_init(&the_mutex, NULL); /* Initialize the mutex */
    pthread_cond_init(&condc, NULL); /* Initialize the consumer condition variable */
    pthread_cond_init(&condp, NULL); /* Initialize the producer condition variable */

    // Create the threads

    for (i=0; i<N; i++){
        p_pro[i].id=i;
        pthread_create(&pro_threads[i], &pro_pthread_custom_attr, producer, (void *)(p_pro+i));
    }

    for (i=0; i<M; i++){
        p_con[i].id=i;
        pthread_create(&con_threads[i], &con_pthread_custom_attr, consumer, (void *)(p_con+i));
    }

    // Wait for the threads to finish.
    // Otherwise main might run to the end
    // and kill the entire process when it exits.

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

        pthread_join(pro_threads[i], NULL);
    }

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

        pthread_join(con_threads[i], NULL);
    }

    // Cleanup -- would happen automatically at the end of program

    pthread_mutex_destroy(&the_mutex); /* Free up the_mutex */
    pthread_cond_destroy(&condc); /* Free up the consumer condition variable */
    pthread_cond_destroy(&condp); /* Free up the producer condition variable */
    free(p_pro);
    free(p_con);
    return 0;
}

最佳答案

这是大学课程吗?

如果仅要求编译器(gcc)启用警告,则报告该问题。谁在“教”您,您c都应该告诉您。


  meh.c:在函数“ printState”中:meh.c:25:21:警告:未使用
  参数'i'[-Wunused-parameter] void printState(int i){
                       ^ meh.c:在函数“生产者”中:meh.c:47:22:警告:未使用的参数“ ptr” [-Wunused-parameter] void *
  生产者(void ptr){
                        ^ meh.c:在功能“消费者”中:meh.c:85:12:警告:未使用的变量“ j” [-Wunused-variable]
       int i,j;
              ^ meh.c:84:22:警告:未使用的参数'ptr'[-Wunused-parameter] void Consumer(void * ptr){
                        ^ meh.c:在函数“ main”中:meh.c:118:14:警告:未使用的参数argc [-Wunused-parameter] int main(int
  argc,char ** argv){
                ^ meh.c:118:27:警告:未使用的参数'argv'[-Wunused-parameter] int main(int argc,char ** argv){
                             ^ meh.c:150:14:警告:“ p_pro”可能未在此函数中使用[-Wmaybe-uninitialized]
           p_pro [i] .id = i;
                ^ meh.c:155:14:警告:“ p_con”可能未在此函数中使用[-Wmaybe-uninitialized]
           p_con [i] .id = i;


但是,即使使用标准方法(例如在各处放置printfs来缩小崩溃站点的范围),也可以轻松诊断出该问题。

因此,我对于找出问题出在什么地方感到困惑。

该代码有一些琐碎的错误,即使修复了segfault,也无法正常工作。在处理一般问题时,我将其省略。

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

#define MAX 1000//00            /* Numbers to produce */
#define SIZE 20                 /* Size of Buffer     */

typedef struct {
    int id;
} parm;

pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int buffer[SIZE];
int c = 0;


它已经是0。全局变量的糟糕的非描述性名称。
   //-----------------------------------------------------------------------------

/ *
    @Function:printState
        @In:整数i
        @Out:无

    @Description: Used to show the state of the buffer on a given state
*/
void printState(int i){


未使用的命名错误的参数。

    int j;


习惯用法是使用“ i”作为循环索引。

    puts("Showing the state of the buffer: ");
    printf("[ ");
    for (j = 0; j < SIZE; j++){
        printf("%d ",buffer[j]);
    }
    printf("]\n");

}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/*
    @Function: producer
    @In: void *ptr
    @Out: none

    @Description: Call a producer on the process
*/

void* producer(void *ptr){


'*'的位置不一致。将其放入类型名称很糟糕。

    int i;

    for (i = 1; i <= MAX; i++){


在循环中不使用“ i”,因此实际值无关紧要。习惯用法是从0到
        printf("calling producer\n");// on position %d.\n",c+1);
        pthread_mutex_lock(&the_mutex); /* protect the buffer */

        if(c == SIZE){  /* If the buffer is full, wait */
            puts("The buffer is full. Waiting.");
            pthread_cond_wait(&condp, &the_mutex);
        }

        buffer[c] = 1;
        c++;

        printf("There are %d occupied positions on the buffer.\n", c);
        pthread_cond_signal(&condc); /* Wake up the consumer */
        pthread_mutex_unlock(&the_mutex); /* Release the buffer */

        //if(i == MAX/2){
        //  printState(i);
        //}

    }
    pthread_exit(0);
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
/*
    @Function: consumer
    @In: void *ptr
    @Out: none

    @Description: Call a consumer on the process
*/
void* consumer(void *ptr) {
    int i, j;

    for (i = 1; i <= MAX; i++){
        printf("calling consumer\n");// on position %d\n", c+1);
        pthread_mutex_lock(&the_mutex); /* protect the buffer */
        if (c == 0){ /* If there is nothing in the buffer, wait */
            puts("Buffer is empty. Waiting.");
            pthread_cond_wait(&condc, &the_mutex);
        }
        buffer[c] = 0;
        c--;
        printf("There are %d occupied positions on the buffer.\n", c);

        pthread_cond_signal(&condp); /* wake up consumer */
        pthread_mutex_unlock(&the_mutex); /* release the buffer */

        //if(i == MAX){
        //  printState(i);
        //}

    }
    pthread_exit(0);
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/*
    @Function: main
    @In: integer argc and character **argv
    @Out: none

    @Description: Main function of the algorithm
*/
int main(int argc, char **argv){
    pthread_t *pro_threads, *con_threads;
    pthread_attr_t pro_pthread_custom_attr, con_pthread_custom_attr;
    int i, M, N;


完全大写的名称通常用于宏。糟糕的非描述性名称。

    parm *p_pro, *p_con;

    puts("Please, enter the number of producer threads:");
    scanf("%d",&N);

    puts("Please, enter the number of consumer threads:");
    scanf("%d",&M);


我不知道谁和为什么建议初学者使用它。请改用argv。

    for(i=0;i<SIZE;i++){
        buffer[i] = 0;
    }


该缓冲区已被清零。可怕的间距与先前采用的间距不一致。

    // Allocate space for the threads

    pro_threads=(pthread_t *)malloc(N*sizeof(*pro_threads));


强制转换malloc是有害的。

    pthread_attr_init(&pro_pthread_custom_attr);
    con_threads=(pthread_t *)malloc(M*sizeof(*con_threads));
    pthread_attr_init(&con_pthread_custom_attr);

    // Initialize the mutex and condition variables

    pthread_mutex_init(&the_mutex, NULL); /* Initialize the mutex */
    pthread_cond_init(&condc, NULL); /* Initialize the consumer condition variable */
    pthread_cond_init(&condp, NULL); /* Initialize the producer condition variable */

    // Create the threads

    for (i=0; i<N; i++){
        p_pro[i].id=i;


p_pro未初始化。

        pthread_create(&pro_threads[i], &pro_pthread_custom_attr, producer, (void *)(p_pro+i));


缺少错误检查。 p_pro使用不一致。

    }

    for (i=0; i<M; i++){
        p_con[i].id=i;
        pthread_create(&con_threads[i], &con_pthread_custom_attr, consumer, (void *)(p_con+i));
    }

    // Wait for the threads to finish.
    // Otherwise main might run to the end
    // and kill the entire process when it exits.

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

        pthread_join(pro_threads[i], NULL);
    }

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

        pthread_join(con_threads[i], NULL);
    }

    // Cleanup -- would happen automatically at the end of program

    pthread_mutex_destroy(&the_mutex); /* Free up the_mutex */
    pthread_cond_destroy(&condc); /* Free up the consumer condition variable */
    pthread_cond_destroy(&condp); /* Free up the producer condition variable */
    free(p_pro);
    free(p_con);
    return 0;
}

09-04 16:46