2015-06-08 38 views
0

我試圖解決使用pthreads和矢量形式的緩衝區的生產者 - 消費者問題。我希望能夠輸入生產者和消費者的線程數量。只要我輸入兩個值,就會出現分段錯誤。我正在使用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; 
} 
+0

你將不得不使用'gdb'並嘗試與隨機線程,看看是否會發生的問題,這使得它很難TAKS,所以你可以嘗試從代碼看着辦吧。 –

+1

[針對小程序的調試建議](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+0

@i_am_jorf,即使我很難承認,我是需要閱讀那篇文章的人的完美例子。謝謝。 o/ –

回答

0

這是一門大學課程嗎?

如果您只是要求它啓用警告,則問題由編譯器(gcc)報告。無論誰「教你」c都應該告訴你。

meh.c:在函數 'printState':meh.c:25:21:警告:未使用 參數的 'i'[-Wunused參數]空隙printState(int i)以{ ^ meh.c :在函數'producer'中:meh.c:47:22:warning:未使用參數'ptr'[-Wunused-parameter] void * producer(void ptr)^ meh.c:函數'consumer': meh.c:85:12:警告:未使用的變量'j'[-Wunused-variable] int i,j;在主函數'main'中:meh.c:118:在默認情況下,meh.c:84:22:警告:未使用的參數'ptr'[-Wunused-parameter] void consumer(void * ptr){ 14:warning:未使用的參數'argc'[-Wunused-parameter] int main(int argc,char ** argv){mell.c:118:27:warning:未使用的參數'argv'[-Wunused-parameter ] int main(int argc,char ** argv)^ meh.c:150:14:warning:'p_pro'可以在此函數中未初始化使用[-Wmaybe-uninitialized] p_pro [i] .id = i ; ^ meh.c:155:14:警告:可能在此函數中未初始化使用'p_con'[-Wmaybe-uninitialized] p_con [i] .id = i;

但是,即使使用標準方法(如將printfs放在這裏和那裏以縮小崩潰位置),也可以輕鬆診斷問題。

因此,我很困惑弄清楚什麼是錯的。

該代碼有一些微不足道的錯誤,即使修復了段錯誤也無法正常工作。我在處理一般問題時忽略了它們。

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

成語是使用'我'作爲循環索引。

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

這就是你發現的一切嗎? :) –

+0

所以失敗的原因是,p_pro沒有initalized,所有其他評論風格相關? – pm100

+0

我忘了下面添加到該算法的頂部: /* * 解決生產者消費者問題 *使用Ptheads,互斥和條件變量 *從的Tanenbaum,現代操作系統,第三版。 */ 所以,嘿,Tanembaum是很多東西的錯。 但是,是的,我可以看到你在Main上找到的錯誤。我爲此感謝你。 要在這裏檢查它,然後評論結果。 –