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

标签 c linux debugging unix buffer

尝试通过执行 gcc -o consumer.c -lpthread -lm 在 Linux 中编译我的代码,但我收到有关未声明的内容的编译错误,据我所知,已声明。大多数未声明的似乎与缓冲区有关,这是我第一个使用缓冲区的程序。这是错误(已编辑以反射(reflect)更改)

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

最佳答案

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

关于消费者生产者代码未声明的错误linux调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33599060/

相关文章:

arrays - const 结构成员的 typedef 中的灵活数组大小

gdb 可以调试 gcc 风格的嵌套 C 函数吗?

linux - Git packed-refs 别名打破了浅获取

linux - Linux 内核中的主要页面错误处理程序

c++ - 获取不带 -g 标志的 gdb 可读符号?

eclipse - 如何在 Eclipse 中调试 JComboBox 的 actionEvent 的处理并避免锁定窗口系统?

c++ - std::signal 和 std::raise 线程安全吗?

c - 如何使用 `switch` 语句比较 C 中的字符串?

linux - 如何在 Linux 中以编程方式反转屏幕颜色

python - pydev断点不起作用