簡體   English   中英

創建用戶輸入的多個線程時出現分段錯誤

[英]Segmentation fault while creating a number of threads entered by the user

我正在嘗試使用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:在函數'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消費者(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。全局變量的糟糕的非描述性名稱。 //----------------------------------------------------------------------------

/ * @功能: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到<MAX。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM