0

안녕하세요 메신저 VM웨어를 사용하여 우분투 가상 이미지에 생산자/소비자 문제를하려고합니다. 나는 여기에서 코드를 가지고있다 : http://macboypro.wordpress.com/2009/05/25/producer-consumer-problem-using-cpthreadsbounded-buffer/하지만 나는 계속 "segmentation fault (core dumped)"를 계속 얻는다. 어떤 도움도 생산자/소비자 문제 (경계 버퍼 문제)에 대해 위대하거나 더 깨끗한 해결책이 될 수 있습니다. 생산자 소비자 세분화 결함 (코어 덤프)

을 Heres 사용하여 코드 메신저하지만 난 '#INCLUDE "buffer.h"'꺼내했다 오류로 인해 :

  /* buffer.h */ 
     typedef int buffer_item; 
     #define BUFFER_SIZE 5 

     /* main.c */ 

     #include <stdlib.h> 
     #include <stdio.h> 
     #include <pthread.h> 
     #include <semaphore.h> 
     #include "buffer.h" 

     #define RAND_DIVISOR 100000000 
     #define TRUE 1 

     /* The mutex lock */ 
     pthread_mutex_t mutex; 

     /* the semaphores */ 
     sem_t full, empty; 

     /* the buffer */ 
     buffer_item buffer[BUFFER_SIZE]; 

     /* buffer counter */ 
     int counter; 

     pthread_t tid;  //Thread ID 
     pthread_attr_t attr; //Set of thread attributes 

     void *producer(void *param); /* the producer thread */ 
     void *consumer(void *param); /* the consumer thread */ 

     void initializeData() { 

      /* Create the mutex lock */ 
      pthread_mutex_init(&mutex, NULL); 

      /* Create the full semaphore and initialize to 0 */ 
      sem_init(&full, 0, 0); 

      /* Create the empty semaphore and initialize to BUFFER_SIZE */ 
      sem_init(&empty, 0, BUFFER_SIZE); 

      /* Get the default attributes */ 
      pthread_attr_init(&attr); 

      /* init buffer */ 
      counter = 0; 
     } 

     /* Producer Thread */ 
     void *producer(void *param) { 
      buffer_item item; 

      while(TRUE) { 
       /* sleep for a random period of time */ 
       int rNum = rand()/RAND_DIVISOR; 
       sleep(rNum); 

       /* generate a random number */ 
       item = rand(); 

       /* acquire the empty lock */ 
       sem_wait(&empty); 
       /* acquire the mutex lock */ 
       pthread_mutex_lock(&mutex); 

       if(insert_item(item)) { 
       fprintf(stderr, " Producer report error condition\n"); 
       } 
       else { 
       printf("producer produced %d\n", item); 
       } 
       /* release the mutex lock */ 
       pthread_mutex_unlock(&mutex); 
       /* signal full */ 
       sem_post(&full); 
      } 
     } 

     /* Consumer Thread */ 
     void *consumer(void *param) { 
      buffer_item item; 

      while(TRUE) { 
       /* sleep for a random period of time */ 
       int rNum = rand()/RAND_DIVISOR; 
       sleep(rNum); 

       /* aquire the full lock */ 
       sem_wait(&full); 
       /* aquire the mutex lock */ 
       pthread_mutex_lock(&mutex); 
       if(remove_item(&item)) { 
       fprintf(stderr, "Consumer report error condition\n"); 
       } 
       else { 
       printf("consumer consumed %d\n", item); 
       } 
       /* release the mutex lock */ 
       pthread_mutex_unlock(&mutex); 
       /* signal empty */ 
       sem_post(&empty); 
      } 
     } 

     /* Add an item to the buffer */ 
     int insert_item(buffer_item item) { 
      /* When the buffer is not full add the item 
       and increment the counter*/ 
      if(counter < BUFFER_SIZE) { 
       buffer[counter] = item; 
       counter++; 
       return 0; 
      } 
      else { /* Error the buffer is full */ 
       return -1; 
      } 
     } 

     /* Remove an item from the buffer */ 
     int remove_item(buffer_item *item) { 
      /* When the buffer is not empty remove the item 
       and decrement the counter */ 
      if(counter > 0) { 
       *item = buffer[(counter-1)]; 
       counter--; 
       return 0; 
      } 
      else { /* Error buffer empty */ 
       return -1; 
      } 
     } 

     int main(int argc, char *argv[]) { 
      /* Loop counter */ 
      int i; 

      /* Verify the correct number of arguments were passed in */ 
      if(argc != 4) { 
       fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n"); 
      } 

      int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */ 
      int numProd = atoi(argv[2]); /* Number of producer threads */ 
      int numCons = atoi(argv[3]); /* Number of consumer threads */ 

      /* Initialize the app */ 
      initializeData(); 

      /* Create the producer threads */ 
      for(i = 0; i < numProd; i++) { 
       /* Create the thread */ 
       pthread_create(&tid,&attr,producer,NULL); 
      } 

      /* Create the consumer threads */ 
      for(i = 0; i < numCons; i++) { 
       /* Create the thread */ 
       pthread_create(&tid,&attr,consumer,NULL); 
      } 

      /* Sleep for the specified amount of time in milliseconds */ 
      sleep(mainSleepTime); 

      /* Exit the program */ 
      printf("Exit the program\n"); 
      exit(0); 
     } 

답변

1

귀하의 코드는 당신이에서 프로토 타입이없는 것을 제외하고, 올바른 것을 상단은을 위해 :

int insert_item(buffer_item item); 
int remove_item(buffer_item *item); 

나는 그 프로토 타입을 추가 한 후 독방 감금 오류를받지 않고 프로그램을 실행 할 수 있었다. 멀티 스레드 응용 프로그램에서 rand()을 사용하는 것은 다소 위험합니다. 여러 제작자가 코드를 실행했을 때 그들은 모두 동일한 임의의 시간을 자고 동일한 임의의 항목 번호를 생성했습니다.