1

我无法调试以下程序,因为我家里没有 linux。我无法执行该程序,但是当我在实验室会话中编译该程序时,我遇到了两个错误。谁能帮我吗?如果有人在调试后能够执行此程序,请发布输出。

代码如下:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#define BUFFER_SIZE 10
sem_t empty;
sem_t full;
pthread_mutex_t mutex;

int buffer[BUFFER_SIZE];

pthread_t ptid,ctid;
pthread_attr_t attr;

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

int counter;
int main()
{

  init();
  pthread_create(&ptid, &attr, producer, NULL);
  pthread_create(&ctid, &attr, consumer, NULL);
  pthread_join(ptid,NULL);
  pthread_join(ctid,NULL);

  return 0;
}

void init()
{
  pthread_mutex_init(&mutex, NULL);
  pthread_attr_init(&attr);
  sem_init(&full, 0, 0);
  sem_init(&empty, 0, BUFFER_SIZE);
  counter = 0;
  for(int j=0;j<BUFFER_SIZE;j++)
  {  buffer[j] = 0;}
}

void *producer(void *param)
{
int item;
  while(1)
  {
    item=item+1;
    sem_wait(&empty);
    pthread_mutex_lock(&mutex);

    if(insert_item(item))
    {
      perror("Producer error.");
      return 1;
    }
    else
    {
      printf("Producer produced %d\n", item);
    }

    pthread_mutex_unlock(&mutex);
    sem_post(&full);
  }
}

void *consumer(void *param)
{
  int item;
  while(1)
  {
sleep(5);
    sem_wait(&full);
    pthread_mutex_lock(&mutex);
    if(remove_item(item))
    {
      perror( "Consumer error.");
    }
    else
    {
      printf("Consumer consumed %d\n", item);
    }

    pthread_mutex_unlock(&mutex);
    sem_post(&empty);
  }
}
int insert_item(int item)
{
  if(counter < BUFFER_SIZE)
  {
    buffer[counter] = item;
    counter++;
    return 0;
  }
  else
    return -1;

}

int remove_item(int item)
{
  if(counter > 0)
  {
    item = buffer[(counter-1)];
    counter--;
    return ;
  }
  else
    return -1;

}

进行更改和更正后,我现在得到以下输出,这不是所需的.......

[root@localhost sree58]# gcc producerconsumer.c -lpthread -lrt
producerconsumer.c: In function ‘producer’:
producerconsumer.c:54: warning: return makes pointer from integer without a cast
[root@localhost sree58]# ./a.out
Producer produced 1804289383
Producer produced 846930886
Producer produced 1681692777
Producer produced 1714636915
Producer produced 1957747793
Producer produced 424238335
Producer produced 719885386
Producer produced 1649760492
Producer produced 596516649
Producer produced 1189641421
Consumer error.: Success
Producer produced 1025202362
Consumer error.: Illegal seek
Producer produced 1350490027
Consumer error.: Illegal seek
Producer produced 783368690
Consumer error.: Illegal seek
Producer produced 1102520059
Consumer error.: Illegal seek
Producer produced 2044897763
Consumer error.: Illegal seek
Producer produced 1967513926
Consumer error.: Illegal seek
Producer produced 1365180540
Consumer error.: Illegal seek
Producer produced 1540383426
Consumer error.: Illegal seek
Producer produced 304089172
Consumer error.: Illegal seek
Producer produced 1303455736
Consumer error.: Illegal seek
Producer produced 35005211
Consumer error.: Illegal seek
Producer produced 521595368
Consumer error.: Illegal seek
Producer produced 294702567
Consumer error.: Illegal seek
Producer produced 1726956429

请有人指导我......

4

2 回答 2

6

首先,让我们解决这个问题(帮助自己的工具):since i don't have linux at home

访问 Oracle 的站点并下载VirtualBox,然后访问您最喜欢的 Linux 发行版的站点并下载.iso它的映像(例如,如果您觉得 Minty,您可以在这里获取一个 iso )

然后您可以按照说明在虚拟机中设置和“安装”iso,瞧!你家里有 Linux。;)

其次,我将解决这个问题:i got two errors

真的吗?你是如何编译这个的?运行代码时,我看到大量警告和 1 个错误gcc -Wall。一个错误也告诉您如何在消息中修复它:

38:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
38:3: note: use option -std=c99 or -std=gnu99 to compile your code

添加该命令gcc -Wall -std=c99,现在您会看到由于缺少 pthread 库而导致的未定义引用:

undefined reference to `pthread_create'

通过将其链接到以下内容来修复:

gcc -Wall -std=c99 <yourfile>.c -lpthread

现在你所拥有的只是一堆(重要的)警告:

In function ‘main’:
22:3: warning: implicit declaration of function ‘init’ [-Wimplicit-function-declaration]
At top level:
31:6: warning: conflicting types for ‘init’ [enabled by default]
22:3: note: previous implicit declaration of ‘init’ was here

In function ‘producer’:
51:5: warning: implicit declaration of function ‘insert_item’ [-Wimplicit-function-declaration]
54:7: warning: return makes pointer from integer without a cast [enabled by default]

In function ‘consumer’:
71:1: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
74:5: warning: implicit declaration of function ‘remove_item’ [-Wimplicit-function-declaration]

In function ‘remove_item’:
106:5: warning: ‘return’ with no value, in function returning non-void [enabled by default]

In function ‘consumer’:
74:19: warning: ‘item’ is used uninitialized in this function [-Wuninitialized]
于 2013-03-07T16:36:06.440 回答
1

我在实验室会议上编译程序,我有两个错误.....谁能帮帮我????

1) 删除 for 循环初始声明for(int j=0;j<BUFFER_SIZE;j++);,或使用选项编译-std=c99

2) 与 pthread 库链接。

gcc  main.c -lpthread

还有一堆警告,注意修复它们。

- 编辑 -

在您的 remove_item() 中,更改返回;返回 0;

这是输出

Producer produced 4001537
Producer produced 4001538
Producer produced 4001539
Producer produced 4001540
Producer produced 4001541
Producer produced 4001542
Producer produced 4001543
Producer produced 4001544
Producer produced 4001545
Producer produced 4001546
Consumer consumed 4001536
Producer produced 4001547
Consumer consumed 4001536
Producer produced 4001548
Consumer consumed 4001536
Producer produced 4001549
Consumer consumed 4001536
Producer produced 4001550
Consumer consumed 4001536
Producer produced 4001551
Consumer consumed 4001536
Producer produced 4001552
Consumer consumed 4001536
Producer produced 4001553
Consumer consumed 4001536
Producer produced 4001554
Consumer consumed 4001536
Producer produced 4001555
Consumer consumed 4001536
;;;;;;;;
;;;;;;;;
;;;;;;;;
;;;;;;;;
于 2013-03-07T16:28:31.507 回答