创建用户输入的多个线程时出现分段错误

时间:2015-06-08 18:52:16

标签: c multithreading pthreads

我正在尝试使用pthreads和向量形式的缓冲区来解决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;
}

1 个答案:

答案 0 :(得分:0)

这是大学课程吗?

如果只是要求它启用警告,编译器(gcc)会报告此问题。谁是&#34;教学&#34;你应该告诉你的。

  

meh.c:在函数'printState'中:meh.c:25:21:警告:未使用   参数'i'[-Wunused-parameter] void printState(int i){                        ^ meh.c:在函数'producer'中:meh.c:47:22:警告:未使用的参数'ptr'[-Wunused-parameter] void *   producer(void ptr){                         ^ meh.c:在函数'consumer'中: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'可以在此函数中未初始化使用[-Wyybe-uninitialized]            p_pro [I] .ID = I;                 ^ meh.c:155:14:警告:'p_con'可能在此函数中未初始化使用[-Wyybe-uninitialized]            p_con [I] .ID = I;

但是,即使使用标准方法(例如将printfs放在此处以缩小崩溃站点),也可以轻松诊断问题。

因此,我很困惑弄清楚什么是错误的问题。

代码有一些微不足道的错误,即使修复了segfault也无法正常工作。我在处理一般问题时省略了它们。

d$diffS <- unname(unlist(using_by))

它已经是0.全局变量的非描述性名称。    #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:整数i         @Out:无

//----------------------------------------------------------------------------

未使用的命名错误的论点。

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

成语是使用&#39; i&#39;作为循环索引。

    int j;

&#39; *&#39;的意外放置。放置类型名称可怕。

    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){

&#39;我&#39;在循环中没有使用,所以实际值无关紧要。成语是从0到&lt; MAX。

    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;

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

    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;
    } 

施放malloc是有害的。

    // Allocate space for the threads

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

p_pro未初始化。

    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));