消费者生产者代码未声明错误linux调试

时间:2015-11-08 21:08:04

标签: c linux debugging unix buffer

尝试通过执行gcc -o consumer.c -lpthread -lm在linux中编译我的代码,并且我收到编译错误,因为我已经声明了对于事物的未申报。大部分未声明似乎与缓冲区有关,这是我的第一个使用缓冲区的程序。以下是错误(编辑以反映更改)

typedef char buffer_item buffer[BUFFER_SIZE]; // asm or __attribute__ before "buffer"

both of these(expected ')' before 'item'
int insert_item(buffer_item item)
int insert_item(buffer_item item)


int remove_item(buffer_item *item)  //expected ')' before * token

以下是更改后的完整代码

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define RAND_DIVISOR 100000000
#define TRUE 1
#define BUFFER_SIZE 1000


pthread_mutex_t mutex; //mutex lock
sem_t full, empty; //semaphores
typedef char buffer_item buffer[BUFFER_SIZE];
int counter; //buffer counter

pthread_t tid1, tid2;       //Thread ID
pthread_attr_t attr; // thread attributes

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

void initializeData() {

   pthread_mutex_init(&mutex, NULL); //Create mutex lock
   sem_init(&full, 0, 0);  // Create the full semaphore and initialize to 0
   sem_init(&empty, 0, BUFFER_SIZE); // Create the empty semaphore and initialize to BUFFER_SIZE
   pthread_attr_init(&attr); //default attributes
   counter = 0;
}

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

   while(TRUE) {
      // random sleep time
      int rNum = rand() / RAND_DIVISOR;
      sleep(rNum);
     int item = rand()%100; // item is a random number between 1-100
      sem_wait(&empty); //get empty lock
      pthread_mutex_lock(&mutex); //get mutex lock

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

// Consumer Thread
void *consumer(void *param) {

   while(TRUE) {
      int rNum = rand() / RAND_DIVISOR; // sleep for a random period of time
      sleep(rNum);
      int item = rand()%100; // item is a random number between 1-100
      sem_wait(&full);// aquire the full lock */
      pthread_mutex_lock(&mutex);// aquire the mutex lock
      if(remove_item(&item)) {
         fprintf(stderr, "Consumer report error condition\n");
      }
      else {
         printf("consumer consumed %d\n", item);
      }
      pthread_mutex_unlock(&mutex);// release mutex lock
      sem_post(&empty); //signal empty
   }
}

int insert_item(buffer_item item)
{
   // add item as long as buffer isn't full
   if(counter < BUFFER_SIZE) {
      buffer[counter] = item;
      counter++;
      return 0;
   }
   else {
      return -1; //buffer full error
   }
}

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

int main(int argc, char *argv[]) {
   int i; //loop counter
   if(argc != 4) {
      fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
   }
   int mainSleepTime = atoi(argv[1]); // sleep time in seconds
   int numProd = atoi(argv[2]); // producer threads
   int numCons = atoi(argv[3]); // consumer threads

   initializeData(); //initialize app

   for(i = 0; i < numProd; i++) {
      pthread_create(&tid1,&attr,producer,NULL);
    }

   for(i = 0; i < numCons; i++) {
      pthread_create(&tid2,&attr,consumer,NULL);
   }

   // sleep in milliseconds
   //sleep(mainSleepTime);

   pthread_join(tid1, NULL);
   pthread_join(tid2, NULL);

   printf("Program Exiting\n");
   exit(0);
}

编辑:错误http://tinypic.com/r/xptzww/9

的最新代码和屏幕截图
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define RAND_DIVISOR 100000000
#define TRUE 1
#define BUFFER_SIZE 1000


pthread_mutex_t mutex; //mutex lock
sem_t full, empty; //semaphores
typedef char buffer_item;
int counter; //buffer counter

pthread_t tid1, tid2;       //Thread ID
pthread_attr_t attr; // thread attributes

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

void initializeData() {

   pthread_mutex_init(&mutex, NULL); //Create mutex lock
   sem_init(&full, 0, 0);  // Create the full semaphore and initialize to 0
   sem_init(&empty, 0, BUFFER_SIZE); // Create the empty semaphore and initialize to BUFFER_SIZE
   pthread_attr_init(&attr); //default attributes
   counter = 0;
}

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

   while(TRUE) {
      // random sleep time
      int rNum = rand() / RAND_DIVISOR;
      sleep(rNum);
     int item = rand()%100; // item is a random number between 1-100
      sem_wait(&empty); //get empty lock
      pthread_mutex_lock(&mutex); //get mutex lock

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

// Consumer Thread
void *consumer(void *param) {

   while(TRUE) {
      int rNum = rand() / RAND_DIVISOR; // sleep for a random period of time
      sleep(rNum);
      int item = rand()%100; // item is a random number between 1-100
      sem_wait(&full);// aquire the full lock */
      pthread_mutex_lock(&mutex);// aquire the mutex lock
      if(remove_item(&item)) {
         fprintf(stderr, "Consumer report error condition\n");
      }
      else {
         printf("consumer consumed %d\n", item);
      }
      pthread_mutex_unlock(&mutex);// release mutex lock
      sem_post(&empty); //signal empty
   }
}

int insert_item(buffer_item item){// add item as long as buffer isn't full
   if(counter < BUFFER_SIZE) {
      buffer[counter] = item;
      counter++;
      return 0;
   }
   else {
      return -1; //buffer full error
   }
}

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

int main(int argc, char *argv[]) {
   int i; //loop counter
   if(argc != 4) {
      fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
   }
   int mainSleepTime = atoi(argv[1]); // sleep time in seconds
   int numProd = atoi(argv[2]); // producer threads
   int numCons = atoi(argv[3]); // consumer threads

   initializeData(); //initialize app

   for(i = 0; i < numProd; i++) {
      pthread_create(&tid1,&attr,producer,NULL);
    }

   for(i = 0; i < numCons; i++) {
      pthread_create(&tid2,&attr,consumer,NULL);
   }

   // sleep in milliseconds
   //sleep(mainSleepTime);

   pthread_join(tid1, NULL);
   pthread_join(tid2, NULL);

   printf("Program Exiting\n");
   exit(0);
}

2 个答案:

答案 0 :(得分:2)

您已声明但未定义BUFFER_SIZE。 代替

   char BUFFER_SIZE;

   char BUFFER_SIZE = some_value;

其中some_value应该是1-255之间的任何值

行:

   char buffer_item buffer[BUFFER_SIZE];

应该是:

   char buffer_item[BUFFER_SIZE];

答案 1 :(得分:2)

变量声明应如下所示:

<type name> <variable name>;

没有标准类型buffer_item。如果要使用自定义类型名称,可以定义:

typedef char buffer_item;

我认为您希望char用于该类型,因为item应该保留从099的数字,而char类型就足够了为了那个原因。 在该行之后,名称buffer_itemchar的别名。因此,变量可以声明为buffer_item buffer[BUFFER_SIZE];buffer_item item;。 在这种情况下,像char buffer_item;这样的行是多余的。此外,您不需要在char之前写buffer_item,因为buffer_item已经是char的另一个名称,因此char buffer_item;char char;相同这没有任何意义。

可能在定义BUFFER_SIZE时应定义未知符号#define RAND_DIVISOR 100000000。通常,C中名称中的大写字母用于宏定义。因此,您可能希望在文件顶部设置该大小,例如:

#define BUFFER_SIZE 1000

在这种情况下,不再需要行char BUFFER_SIZE;

在声明之前使用函数insert_item。因此,要编译,您可以在producer()之前提出声明:

int insert_item(buffer_item item);

这应该足以编译代码。

针对新错误进行了更新

类型声明使用特殊关键字typedef来创建类型别名。这里声明buffer_item名称用作类型名称,它与char相同,应该是:

typedef char buffer_item;

缓冲区数组应在下面定义为:

buffer_item buffer[BUFFER_SIZE];

转发函数声明最后需要分号:

int insert_item(buffer_item item);

我不确定它会按预期工作,但现在应该编译。