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



试图通过执行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 conditionn");
      }
      else {
         printf("producer produced %dn", 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 conditionn");
      }
      else {
         printf("consumer consumed %dn", 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 Exitingn");
   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 conditionn");
      }
      else {
         printf("producer produced %dn", 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 conditionn");
      }
      else {
         printf("consumer consumed %dn", 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 Exitingn");
   exit(0);
}

您已声明但未定义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];

变量声明应该看起来像:

<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;这样的线路是多余的。此外,您不需要在buffer_item之前写入char,因为buffer_item已经是char的另一个名称,所以char buffer_item;char char;相同,没有任何意义。

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

#define BUFFER_SIZE 1000

在这种情况下,不再需要线路CCD_ 20。

在声明之前使用函数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);

我不确定它是否会像预期的那样工作,但现在它应该被编译了。

最新更新